How to Test NGRX Selectors in Angular


angular ngrx

I had to test NGRX selectors in the Angular application and found how we can do this.

Suppose, you have the simillar selector in your app:

import { createSelector } from '@ngrx/store';
import * as fromFeature from '../../reducers/root';

export const getAppState = createSelector(
  fromFeature.getUserState,
  (state: fromFeature.UserState) => state && state.appState
);

createSelector functions will return a selector entity and it will contains a .projector method. We can use this method for testing:

describe('App State Selectors', () => {
  it('should return app state', () => {
    expect(fromAppStateSelectors.getAppState.projector().toBe(undefined));
  });
});

Don’t forget to use the appropriate Test framework, if you want to run the code above.

Note: I used NGRX v10.0.1 for these pieces of code.

comments powered by Disqus