useCombinedReducer.spec.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  2. import {makeCombinedReducers, useCombinedReducer} from 'sentry/utils/useCombinedReducer';
  3. describe('makeCombinedReducers', () => {
  4. it('calls reducer only with subset of state', () => {
  5. const mockFirstReducer = jest.fn();
  6. const reducer = makeCombinedReducers({first: mockFirstReducer, second: jest.fn()});
  7. reducer({first: 'first', second: ''}, 'action');
  8. expect(mockFirstReducer).toHaveBeenCalledWith('first', 'action');
  9. });
  10. it('calls all reducers with action', () => {
  11. const mockFirstReducer = jest.fn();
  12. const mockSecondReducer = jest.fn();
  13. const reducer = makeCombinedReducers({
  14. first: mockFirstReducer,
  15. second: mockSecondReducer,
  16. });
  17. reducer({first: 'first', second: 'second'}, 'action');
  18. expect(mockFirstReducer).toHaveBeenCalledWith('first', 'action');
  19. expect(mockSecondReducer).toHaveBeenCalledWith('second', 'action');
  20. });
  21. });
  22. describe('useCombinedReducer', () => {
  23. it('initializes with init state', () => {
  24. const {result} = reactHooks.renderHook(
  25. (args: Parameters<typeof useCombinedReducer>) =>
  26. useCombinedReducer(args[0], args[1]),
  27. {initialProps: [{first: jest.fn()}, {first: 'initial'}]}
  28. );
  29. expect(result.current[0]).toEqual({first: 'initial'});
  30. });
  31. it('updates state', () => {
  32. const {result} = reactHooks.renderHook(
  33. (args: Parameters<typeof useCombinedReducer>) =>
  34. useCombinedReducer(args[0], args[1]),
  35. {initialProps: [{first: (state, action) => state + action}, {first: 'initial'}]}
  36. );
  37. reactHooks.act(() => result.current[1]('_action'));
  38. expect(result.current[0]).toEqual({first: 'initial_action'});
  39. });
  40. it('doesnt keep old state around', () => {
  41. const {result} = reactHooks.renderHook(
  42. (args: Parameters<typeof useCombinedReducer>) =>
  43. useCombinedReducer(args[0], args[1]),
  44. {initialProps: [{first: (state, action) => state + action}, {first: 'initial'}]}
  45. );
  46. reactHooks.act(() => result.current[1]('_action'));
  47. reactHooks.act(() => result.current[1]('_action'));
  48. expect(result.current[0]).toEqual({first: 'initial_action_action'});
  49. });
  50. });