root.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { reducerTester } from '../../../test/core/redux/reducerTester';
  2. import { describe, expect } from '../../../test/lib/common';
  3. import { initialTeamsState, teamsLoaded } from '../../features/teams/state/reducers';
  4. import { Team } from '../../types';
  5. import { StoreState } from '../../types/store';
  6. import { cleanUpAction } from '../actions/cleanUp';
  7. import { createRootReducer, recursiveCleanState } from './root';
  8. jest.mock('@grafana/runtime', () => ({
  9. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  10. config: {
  11. bootData: {
  12. navTree: [],
  13. user: {},
  14. },
  15. },
  16. }));
  17. describe('recursiveCleanState', () => {
  18. describe('when called with an existing state selector', () => {
  19. it('then it should clear that state slice in state', () => {
  20. const state = {
  21. teams: { teams: [{ id: 1 }, { id: 2 }] },
  22. };
  23. // Choosing a deeper state selector here just to test recursive behaviour
  24. // This should be same state slice that matches the state slice of a reducer like state.teams
  25. const stateSelector = state.teams.teams[0];
  26. recursiveCleanState(state, stateSelector);
  27. expect(state.teams.teams[0]).not.toBeDefined();
  28. expect(state.teams.teams[1]).toBeDefined();
  29. });
  30. });
  31. describe('when called with a non existing state selector', () => {
  32. it('then it should not clear that state slice in state', () => {
  33. const state = {
  34. teams: { teams: [{ id: 1 }, { id: 2 }] },
  35. };
  36. // Choosing a deeper state selector here just to test recursive behaviour
  37. // This should be same state slice that matches the state slice of a reducer like state.teams
  38. const stateSelector = state.teams.teams[2];
  39. recursiveCleanState(state, stateSelector);
  40. expect(state.teams.teams[0]).toBeDefined();
  41. expect(state.teams.teams[1]).toBeDefined();
  42. });
  43. });
  44. });
  45. describe('rootReducer', () => {
  46. const rootReducer = createRootReducer();
  47. describe('when called with any action except cleanUpAction', () => {
  48. it('then it should not clean state', () => {
  49. const teams = [{ id: 1 } as Team];
  50. const state = {
  51. teams: { ...initialTeamsState },
  52. } as StoreState;
  53. reducerTester<StoreState>()
  54. .givenReducer(rootReducer, state)
  55. .whenActionIsDispatched(teamsLoaded(teams))
  56. .thenStatePredicateShouldEqual((resultingState) => {
  57. expect(resultingState.teams).toEqual({
  58. hasFetched: true,
  59. searchQuery: '',
  60. searchPage: 1,
  61. teams,
  62. });
  63. return true;
  64. });
  65. });
  66. });
  67. describe('when called with cleanUpAction', () => {
  68. it('then it should clean state', () => {
  69. const teams = [{ id: 1 }] as Team[];
  70. const state: StoreState = {
  71. teams: {
  72. hasFetched: true,
  73. searchQuery: '',
  74. searchPage: 1,
  75. teams,
  76. },
  77. } as StoreState;
  78. reducerTester<StoreState>()
  79. .givenReducer(rootReducer, state, false, true)
  80. .whenActionIsDispatched(cleanUpAction({ stateSelector: (storeState: StoreState) => storeState.teams }))
  81. .thenStatePredicateShouldEqual((resultingState) => {
  82. expect(resultingState.teams).toEqual({ ...initialTeamsState });
  83. return true;
  84. });
  85. });
  86. });
  87. });