reducerFactory.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { reducerFactory } from './reducerFactory';
  2. import { actionCreatorFactory, GrafanaAction } 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. .addHandler({
  26. creator: dummyActionCreator,
  27. handler: ({ state, action }) => {
  28. return { ...state, ...action.payload };
  29. },
  30. })
  31. .create();
  32. describe('reducerFactory', () => {
  33. describe('given it is created with a defined handler', () => {
  34. describe('when reducer is called with no state', () => {
  35. describe('and with an action that the handler can not handle', () => {
  36. it('then the resulting state should be intial state', () => {
  37. const result = dummyReducer(undefined as DummyReducerState, {} as GrafanaAction<any>);
  38. expect(result).toEqual(dummyReducerIntialState);
  39. });
  40. });
  41. describe('and with an action that the handler can handle', () => {
  42. it('then the resulting state should correct', () => {
  43. const payload = { n: 10, s: 'ten', b: false, o: { n: 20, s: 'twenty', b: true } };
  44. const result = dummyReducer(undefined as DummyReducerState, dummyActionCreator(payload));
  45. expect(result).toEqual(payload);
  46. });
  47. });
  48. });
  49. describe('when reducer is called with a state', () => {
  50. describe('and with an action that the handler can not handle', () => {
  51. it('then the resulting state should be intial state', () => {
  52. const result = dummyReducer(dummyReducerIntialState, {} as GrafanaAction<any>);
  53. expect(result).toEqual(dummyReducerIntialState);
  54. });
  55. });
  56. describe('and with an action that the handler can handle', () => {
  57. it('then the resulting state should correct', () => {
  58. const payload = { n: 10, s: 'ten', b: false, o: { n: 20, s: 'twenty', b: true } };
  59. const result = dummyReducer(dummyReducerIntialState, dummyActionCreator(payload));
  60. expect(result).toEqual(payload);
  61. });
  62. });
  63. });
  64. });
  65. describe('given a handler is added', () => {
  66. describe('when a handler with the same creator is added', () => {
  67. it('then is should throw', () => {
  68. const faultyReducer = reducerFactory(dummyReducerIntialState).addHandler({
  69. creator: dummyActionCreator,
  70. handler: ({ state, action }) => {
  71. return { ...state, ...action.payload };
  72. },
  73. });
  74. expect(() => {
  75. faultyReducer.addHandler({
  76. creator: dummyActionCreator,
  77. handler: ({ state }) => {
  78. return state;
  79. },
  80. });
  81. }).toThrow();
  82. });
  83. });
  84. });
  85. });