Last active
September 11, 2015 18:17
-
-
Save wbuchwalter/3108d6ff2137e4399cec to your computer and use it in GitHub Desktop.
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
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