Test Sidenav Reducer
Create a test for the sidenav reducer:
- Assert an unknown action to result in no changes to the state object.
- Assert the
HideSidenavaction to result in theopenedboolean value set tofalse. - Assert the
ShowSidenavaction to result in theopenedboolean value set totrue.
Create a new file at: src/app/state/sidenav/sidenav.reducer.spec.ts
Unknown Action
First assert an unknown action does not mutate the state of our application:
import { initialState, reducer } from './sidenav.reducer';
describe('unknown action', () => {
it('should return the initial state', () => {
const action = {type: 'NOOP'} as any;
const result = reducer(initialState, action);
expect(result).toBe(initialState);
});
});- Create an action object with a
typestring property that is invalid. - Execute the
reducer()function specifying theinitialStateand theaction. - Expect the
resultto be theinitialStateobject.
HideSidenav
Assert the HideSidenav action:
import { HideSidenav } from './sidenav.actions';
import { initialState, reducer } from './sidenav.reducer';
describe('HideSidenav', () => {
it('should set the opened property to false', () => {
const action = new HideSidenav();
const result = reducer(initialState, action);
expect(result).toEqual({
...initialState,
opened: false
});
});
});- Create the new
HideSidenavaction. - Execute the
reducer()function specifying theinitialStateand theaction. - Expect the
resultto equal a new object where theopenedvalue isfalse.
ShowSidenav
Assert the ShowSidenav action:
import { HideSidenav, ShowSidenav } from './sidenav.actions';
import { initialState, reducer } from './sidenav.reducer';
describe('ShowSidenav', () => {
it('should set the opened property to true', () => {
const action = new ShowSidenav();
const result = reducer(initialState, action);
expect(result).toEqual({
...initialState,
opened: true
});
});
});- Create the new
ShowSidenavaction. - Execute the
reducer()function specifying theinitialStateand theaction. - Expect the
resultto equal a new object where theopenedvalue istrue.
Execute Tests
npm test
yarn testOr, use watch mode:
npm run test:watch
yarn test:watchWhen in watch mode you can:
- Press a to run all tests.
- Press f to run only failed tests.
- Press p to filter by a filename regex pattern.
- Press t to filter by a test name regex pattern.
- Press q to quit watch mode.
- Press Enter to trigger a test run.