Created
January 27, 2017 14:03
-
-
Save jimbol/dbe47109860bb6958527568ffaa21114 to your computer and use it in GitHub Desktop.
Selector dependency injection
This file contains hidden or 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
// It gets hard to test combined selectors after a while. | |
// The dependency chains run deep. To get around this we | |
// can test the callback function seperately from the rest | |
// of the selector, but that leaves a testing gap. | |
// In order to test that createSelector gets called with the | |
// right stuff, we could do something like this | |
// WARNING UNTESTED CODE | |
// In selector | |
import { selectedFooIds, foosHash } from './selectors'; | |
import { lookUpByKey } from './transformers'; | |
export const selectorHash = { | |
getSelectedFoos: () => createSelector(selectedFooIds, foosHash, lookUpByKey) | |
}; | |
export default build(selectorHash); | |
// Generally utility to call each member of selecorHash | |
function build(hash) { | |
return Object.keys(hash) | |
.reduce(function(output, key) { | |
output[key] = hash[key](); | |
return output; | |
}, {}); | |
} | |
///// | |
// In test | |
// Inside your first describe | |
let selectors; | |
let createSelectorSpy; | |
beforeEach(() => { | |
createSelectorSpy = sinon.stub(reselect.createSelector); | |
selectors = selectorHash.getSelectedFoos() | |
}); | |
it('calls createSelector with correct values', () => { | |
expect(createSelectorSpy) | |
.to.have.been.calledWith(selectedFooIds, foosHash, lookUpByKey); | |
}); | |
// now we can test what createSelectorSpy gets called with |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment