reducers.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { createAction } from '@reduxjs/toolkit';
  2. import { VariableType } from '@grafana/data';
  3. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  4. import { VariableAdapter, variableAdapters } from '../adapters';
  5. import { initialVariableModelState, QueryVariableModel } from '../types';
  6. import { toVariablePayload } from '../utils';
  7. import { VariablePayload, VariablesState } from './types';
  8. import { cleanVariables, variablesReducer } from './variablesReducer';
  9. const variableAdapter: VariableAdapter<QueryVariableModel> = {
  10. id: 'mock' as unknown as VariableType,
  11. name: 'Mock label',
  12. description: 'Mock description',
  13. dependsOn: jest.fn(),
  14. updateOptions: jest.fn(),
  15. initialState: {} as QueryVariableModel,
  16. reducer: jest.fn().mockReturnValue({}),
  17. getValueForUrl: jest.fn(),
  18. getSaveModel: jest.fn(),
  19. picker: null as any,
  20. editor: null as any,
  21. setValue: jest.fn(),
  22. setValueFromUrl: jest.fn(),
  23. };
  24. variableAdapters.setInit(() => [{ ...variableAdapter }]);
  25. describe('variablesReducer', () => {
  26. describe('when cleanUpDashboard is dispatched', () => {
  27. it('then all variables except global variables should be removed', () => {
  28. const initialState: VariablesState = {
  29. '0': {
  30. ...initialVariableModelState,
  31. id: '0',
  32. index: 0,
  33. type: 'query',
  34. name: 'Name-0',
  35. label: 'Label-0',
  36. },
  37. '1': {
  38. ...initialVariableModelState,
  39. id: '1',
  40. index: 1,
  41. type: 'query',
  42. name: 'Name-1',
  43. label: 'Label-1',
  44. global: true,
  45. },
  46. '2': {
  47. ...initialVariableModelState,
  48. id: '2',
  49. index: 2,
  50. type: 'query',
  51. name: 'Name-2',
  52. label: 'Label-2',
  53. },
  54. '3': {
  55. ...initialVariableModelState,
  56. id: '3',
  57. index: 3,
  58. type: 'query',
  59. name: 'Name-3',
  60. label: 'Label-3',
  61. global: true,
  62. },
  63. };
  64. reducerTester<VariablesState>()
  65. .givenReducer(variablesReducer, initialState)
  66. .whenActionIsDispatched(cleanVariables())
  67. .thenStateShouldEqual({
  68. '1': {
  69. ...initialVariableModelState,
  70. id: '1',
  71. index: 1,
  72. type: 'query',
  73. name: 'Name-1',
  74. label: 'Label-1',
  75. global: true,
  76. },
  77. '3': {
  78. ...initialVariableModelState,
  79. id: '3',
  80. index: 3,
  81. type: 'query',
  82. name: 'Name-3',
  83. label: 'Label-3',
  84. global: true,
  85. },
  86. });
  87. });
  88. });
  89. describe('when any action is dispatched with a type prop that is registered in variableAdapters', () => {
  90. it('then the reducer for that variableAdapter should be invoked', () => {
  91. const initialState: VariablesState = {
  92. '0': {
  93. ...initialVariableModelState,
  94. id: '0',
  95. index: 0,
  96. type: 'query',
  97. name: 'Name-0',
  98. label: 'Label-0',
  99. },
  100. };
  101. variableAdapters.get('mock').reducer = jest.fn().mockReturnValue(initialState);
  102. const mockAction = createAction<VariablePayload>('mockAction');
  103. reducerTester<VariablesState>()
  104. .givenReducer(variablesReducer, initialState)
  105. .whenActionIsDispatched(mockAction(toVariablePayload({ type: 'mock' as unknown as VariableType, id: '0' })))
  106. .thenStateShouldEqual(initialState);
  107. expect(variableAdapters.get('mock').reducer).toHaveBeenCalledTimes(1);
  108. expect(variableAdapters.get('mock').reducer).toHaveBeenCalledWith(
  109. initialState,
  110. mockAction(toVariablePayload({ type: 'mock' as unknown as VariableType, id: '0' }))
  111. );
  112. });
  113. });
  114. describe('when any action is dispatched with a type prop that is not registered in variableAdapters', () => {
  115. it('then the reducer for that variableAdapter should be invoked', () => {
  116. const initialState: VariablesState = {
  117. '0': {
  118. ...initialVariableModelState,
  119. id: '0',
  120. index: 0,
  121. type: 'query',
  122. name: 'Name-0',
  123. label: 'Label-0',
  124. },
  125. };
  126. variableAdapters.get('mock').reducer = jest.fn().mockReturnValue(initialState);
  127. const mockAction = createAction<VariablePayload>('mockAction');
  128. reducerTester<VariablesState>()
  129. .givenReducer(variablesReducer, initialState)
  130. .whenActionIsDispatched(mockAction(toVariablePayload({ type: 'adhoc', id: '0' })))
  131. .thenStateShouldEqual(initialState);
  132. expect(variableAdapters.get('mock').reducer).toHaveBeenCalledTimes(0);
  133. });
  134. });
  135. describe('when any action is dispatched missing type prop', () => {
  136. it('then the reducer for that variableAdapter should be invoked', () => {
  137. const initialState: VariablesState = {
  138. '0': {
  139. ...initialVariableModelState,
  140. id: '0',
  141. index: 0,
  142. type: 'query',
  143. name: 'Name-0',
  144. label: 'Label-0',
  145. },
  146. };
  147. variableAdapters.get('mock').reducer = jest.fn().mockReturnValue(initialState);
  148. const mockAction = createAction<string>('mockAction');
  149. reducerTester<VariablesState>()
  150. .givenReducer(variablesReducer, initialState)
  151. .whenActionIsDispatched(mockAction('mocked'))
  152. .thenStateShouldEqual(initialState);
  153. expect(variableAdapters.get('mock').reducer).toHaveBeenCalledTimes(0);
  154. });
  155. });
  156. });