reducerTester.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { reducerFactory, actionCreatorFactory } from 'app/core/redux';
  2. import { reducerTester } from './reducerTester';
  3. interface DummyState {
  4. data: string[];
  5. }
  6. const initialState: DummyState = {
  7. data: [],
  8. };
  9. const dummyAction = actionCreatorFactory<string>('dummyAction').create();
  10. const mutatingReducer = reducerFactory(initialState)
  11. .addMapper({
  12. filter: dummyAction,
  13. mapper: (state, action) => {
  14. state.data.push(action.payload);
  15. return state;
  16. },
  17. })
  18. .create();
  19. const okReducer = reducerFactory(initialState)
  20. .addMapper({
  21. filter: dummyAction,
  22. mapper: (state, action) => {
  23. return {
  24. ...state,
  25. data: state.data.concat(action.payload),
  26. };
  27. },
  28. })
  29. .create();
  30. describe('reducerTester', () => {
  31. describe('when reducer mutates state', () => {
  32. it('then it should throw', () => {
  33. expect(() => {
  34. reducerTester()
  35. .givenReducer(mutatingReducer, initialState)
  36. .whenActionIsDispatched(dummyAction('some string'))
  37. .thenStateShouldEqual({ ...initialState, data: ['some string'] });
  38. }).toThrow();
  39. });
  40. });
  41. describe('when reducer does not mutate state', () => {
  42. it('then it should not throw', () => {
  43. expect(() => {
  44. reducerTester()
  45. .givenReducer(okReducer, initialState)
  46. .whenActionIsDispatched(dummyAction('some string'))
  47. .thenStateShouldEqual({ ...initialState, data: ['some string'] });
  48. }).not.toThrow();
  49. });
  50. });
  51. });