I followed the official documentation on testing ngxs selectors (https://ngxs.gitbook.io/ngxs/recipes/unit-testing#testing-selectors), however it doesn't cover how to unittest dynamic selectors created with createSelector
.
My normal selector just gets the state as an argument so I can easly test it by passing a prepared state and comparing the output.
@Selector()
static nachweise(state: NachweisStateModel) {
return state.nachweise;
}
//Setup state
const state = {...};
//Compare expectations
expect(NachweisState.nachweise(state)).toEqual(...);
My dynamic selector looks like this:
@Selector()
static nachweisById(id: string) {
return createSelector([NachweisState], state => {
return state.nachweise.find(nachweis => nachweis.id === id);
});
}
The only parameter it gets is the id by which it selects, but not the state. The State is automagically passed in by specifying it as the first parameter to createSelector
and I don't know how I should test this selector.
It seems that the documentation has been updated:
it('should select requested animal names from state', () => {
const zooState = {
animals: [
{ type: 'zebra', name: 'Andy'},
{ type: 'panda', name: 'Betty'},
{ type: 'zebra', name: 'Crystal'},
{ type: 'panda', name: 'Donny'},
]
};
const value = ZooSelectors.animalNames('zebra')(zooState);
expect(value).toEqual(['Andy', 'Crystal']);
});