reducerFactory.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { reducerFactory } from './reducerFactory';
  2. import { actionCreatorFactory, ActionOf } from './actionCreatorFactory';
  3. interface DummyReducerState {
  4. n: number;
  5. s: string;
  6. b: boolean;
  7. o: {
  8. n: number;
  9. s: string;
  10. b: boolean;
  11. };
  12. }
  13. const dummyReducerIntialState: DummyReducerState = {
  14. n: 1,
  15. s: 'One',
  16. b: true,
  17. o: {
  18. n: 2,
  19. s: 'two',
  20. b: false,
  21. },
  22. };
  23. const dummyActionCreator = actionCreatorFactory<DummyReducerState>('dummy').create();
  24. const dummyReducer = reducerFactory(dummyReducerIntialState)
  25. .addMapper({
  26. filter: dummyActionCreator,
  27. mapper: (state, action) => ({ ...state, ...action.payload }),
  28. })
  29. .create();
  30. describe('reducerFactory', () => {
  31. describe('given it is created with a defined handler', () => {
  32. describe('when reducer is called with no state', () => {
  33. describe('and with an action that the handler can not handle', () => {
  34. it('then the resulting state should be intial state', () => {
  35. const result = dummyReducer(undefined as DummyReducerState, {} as ActionOf<any>);
  36. expect(result).toEqual(dummyReducerIntialState);
  37. });
  38. });
  39. describe('and with an action that the handler can handle', () => {
  40. it('then the resulting state should correct', () => {
  41. const payload = { n: 10, s: 'ten', b: false, o: { n: 20, s: 'twenty', b: true } };
  42. const result = dummyReducer(undefined as DummyReducerState, dummyActionCreator(payload));
  43. expect(result).toEqual(payload);
  44. });
  45. });
  46. });
  47. describe('when reducer is called with a state', () => {
  48. describe('and with an action that the handler can not handle', () => {
  49. it('then the resulting state should be intial state', () => {
  50. const result = dummyReducer(dummyReducerIntialState, {} as ActionOf<any>);
  51. expect(result).toEqual(dummyReducerIntialState);
  52. });
  53. });
  54. describe('and with an action that the handler can handle', () => {
  55. it('then the resulting state should correct', () => {
  56. const payload = { n: 10, s: 'ten', b: false, o: { n: 20, s: 'twenty', b: true } };
  57. const result = dummyReducer(dummyReducerIntialState, dummyActionCreator(payload));
  58. expect(result).toEqual(payload);
  59. });
  60. });
  61. });
  62. });
  63. describe('given a handler is added', () => {
  64. describe('when a handler with the same creator is added', () => {
  65. it('then is should throw', () => {
  66. const faultyReducer = reducerFactory(dummyReducerIntialState).addMapper({
  67. filter: dummyActionCreator,
  68. mapper: (state, action) => {
  69. return { ...state, ...action.payload };
  70. },
  71. });
  72. expect(() => {
  73. faultyReducer.addMapper({
  74. filter: dummyActionCreator,
  75. mapper: state => {
  76. return state;
  77. },
  78. });
  79. }).toThrow();
  80. });
  81. });
  82. });
  83. });