useCombinedReducer.spec.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. useCombinedReducer({first: jest.fn()}, {first: 'initial'})
  26. );
  27. expect(result.current[0]).toEqual({first: 'initial'});
  28. });
  29. it('updates state', () => {
  30. const {result} = reactHooks.renderHook(() =>
  31. useCombinedReducer({first: (state, action) => state + action}, {first: 'initial'})
  32. );
  33. reactHooks.act(() => result.current[1]('_action'));
  34. expect(result.current[0]).toEqual({first: 'initial_action'});
  35. });
  36. it('doesnt keep old state around', () => {
  37. const {result} = reactHooks.renderHook(() =>
  38. useCombinedReducer({first: (state, action) => state + action}, {first: 'initial'})
  39. );
  40. reactHooks.act(() => result.current[1]('_action'));
  41. reactHooks.act(() => result.current[1]('_action'));
  42. expect(result.current[0]).toEqual({first: 'initial_action_action'});
  43. });
  44. });