Skip to content

Instantly share code, notes, and snippets.

@wbuchwalter
Last active September 11, 2015 18:17
Show Gist options
  • Save wbuchwalter/3108d6ff2137e4399cec to your computer and use it in GitHub Desktop.
Save wbuchwalter/3108d6ff2137e4399cec to your computer and use it in GitHub Desktop.
export default class NgReduxFake {
private selectedState: any;
private target: any;
private mapState: Function;
push(selectedState: any) {
if (!_.isPlainObject(selectedState)) {
throw 'selectedState must be a plain object';
}
this.selectedState = selectedState;
this.updateTarget();
}
connect(mapState: Function) {
this.mapState = mapState;
return (target) => {
this.target = target;
this.updateTarget();
};
}
updateTarget() {
if (!this.target || !this.mapState || !this.selectedState) {
return;
}
if (_.isFunction(this.target)) {
this.target(this.mapState(this.selectedState));
return;
}
_.assign(this.target, this.mapState(this.selectedState));
}
}
/*usage*/
let mockState;
let sut;
let ngRedux;
beforeEach(() => {
mockState = {baz: {
foo: true,
bar: false
}};
ngRedux = new NgReduxFake();
ngRedux.push(mockState);
sut = new myController(ngRedux);
});
it('Should update foo', () => {
mockState.baz.foo = false;
ngRedux.push(mockState);
assert.equal(sut.foo, mockState.baz.foo);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment