-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lots more tests and some other changes
- Loading branch information
Showing
11 changed files
with
235 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//@flow | ||
import type {Store} from 'redux'; | ||
import * as Immutable from 'immutable'; | ||
import * as actions from './actions'; | ||
|
||
export class Subscription<S, P, R> { | ||
|
||
paths: (state: S, props: P) => string[]; | ||
value: (state: S, props: P) => R; | ||
|
||
constructor(props: {paths: (state: S, props: P) => string[], value: (state: S, props: P) => R}) { | ||
this.paths = props.paths; | ||
this.value = props.value; | ||
} | ||
|
||
fetchNow(store: Store<*, *>, props: P): Promise<R> { | ||
return new Promise((resolve) => { | ||
let paths = Immutable.Set(this.paths(store.getState(), props)); | ||
store.dispatch( | ||
actions.fetchValues(paths.toJS(), () => { | ||
let newPaths = Immutable.Set(this.paths(store.getState(), props)); | ||
if (newPaths.subtract(paths).size == 0) { | ||
resolve(this.value(store.getState(), props)); | ||
} else { | ||
this.fetchNow(store, props).then(resolve); | ||
} | ||
}) | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,72 @@ | ||
//@flow | ||
import * as reduxFirebaseMirror from '../index'; | ||
import thunkMiddleware from 'redux-thunk'; | ||
import {createStore, applyMiddleware} from 'redux'; | ||
import * as Immutable from 'immutable'; | ||
import reduxFirebaseMirror, {subscribeToValues, unsubscribeFromValues, fetchValues} from '../index'; | ||
import * as actions from '../actions'; | ||
|
||
describe("The redux-firebase-mirror module", () => { | ||
it("should export all the functions from the actions module", () => { | ||
Object.keys(actions).forEach(key => expect(reduxFirebaseMirror[key]).toBe(actions[key])); | ||
expect(subscribeToValues).toBe(actions.subscribeToValues); | ||
expect(unsubscribeFromValues).toBe(actions.unsubscribeFromValues); | ||
expect(fetchValues).toBe(actions.fetchValues); | ||
}); | ||
|
||
describe("the default exported function", () => { | ||
let api, store; | ||
beforeEach(() => { | ||
api = reduxFirebaseMirror({ | ||
getFirebaseState: state => state, | ||
}); | ||
store = createStore( | ||
api.reducer, | ||
Immutable.Map(), | ||
applyMiddleware(thunkMiddleware) | ||
); | ||
}); | ||
|
||
it("should return some selectors", () => { | ||
expect(api.selectors.getKeysAtPath).toEqual(jasmine.any(Function)); | ||
expect(api.selectors.getValueAtPath).toEqual(jasmine.any(Function)); | ||
}); | ||
|
||
describe("the getKeysAtPath selector", () => { | ||
it("should return an empty list for paths that have not been fetched yet", () => { | ||
expect(api.selectors.getKeysAtPath(store.getState(), 'foo')).toEqual([]); | ||
expect(api.selectors.getKeysAtPath(store.getState(), 'foo/bar/baz')).toEqual([]); | ||
}); | ||
|
||
it("should return a list of keys for a path that has been fetched", () => { | ||
store.dispatch({ | ||
type: 'FIREBASE/RECEIVE_SNAPSHOT', | ||
path: 'foo/bar/baz', | ||
value: 1, | ||
}); | ||
expect(api.selectors.getKeysAtPath(store.getState(), 'foo')).toEqual(['bar']); | ||
expect(api.selectors.getKeysAtPath(store.getState(), 'foo/bar')).toEqual(['baz']); | ||
expect(api.selectors.getKeysAtPath(store.getState(), 'foo/bar/baz')).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe("the getValueAtPath selector", () => { | ||
it("should return undefined for paths that have not been fetched yet", () => { | ||
expect(api.selectors.getValueAtPath(store.getState(), 'foo')).toBeUndefined(); | ||
expect(api.selectors.getValueAtPath(store.getState(), 'foo/bar/baz')).toBeUndefined(); | ||
}); | ||
it("should return the value when it has been fetched", () => { | ||
store.dispatch({ | ||
type: 'FIREBASE/RECEIVE_SNAPSHOT', | ||
path: 'foo/bar/baz', | ||
value: 1, | ||
}); | ||
const foo = api.selectors.getValueAtPath(store.getState(), 'foo'); | ||
if (foo instanceof Immutable.Map) { | ||
expect(foo.toJS()).toEqual({bar:{baz:1}}); | ||
} else { | ||
throw new Error("expected to get a map"); | ||
} | ||
expect(api.selectors.getValueAtPath(store.getState(), 'foo/bar/baz')).toEqual(1); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//@flow | ||
import * as Immutable from 'immutable'; | ||
import type {JSONType, StorageAPI} from './types'; | ||
|
||
type MirrorType = Immutable.Map<string, mixed>; | ||
type ValueType = mixed; | ||
|
||
const immutableStorage: StorageAPI<MirrorType, ValueType> = { | ||
getKeysAtPath(mirror: MirrorType, path: string): string[] { | ||
const map = mirror.getIn(path.split('/'), Immutable.Map()); | ||
if (!(map instanceof Immutable.Map)) { | ||
return []; | ||
} | ||
return map.keySeq().toArray(); | ||
}, | ||
|
||
getValueAtPath(mirror: MirrorType, path: string): ValueType { | ||
return mirror.getIn(path.split('/')); | ||
}, | ||
|
||
setValues(mirror: MirrorType, values: {[path: string]: JSONType}): MirrorType { | ||
return mirror.withMutations((mirror) => { | ||
Object.entries(values).forEach(([path, value]) => { | ||
mirror.setIn(path.split('/'), Immutable.fromJS(value)); | ||
}); | ||
return mirror; | ||
}); | ||
}, | ||
|
||
getInitialMirror(): MirrorType { | ||
return Immutable.Map(); | ||
}, | ||
}; | ||
|
||
export default immutableStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.