actions.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { DataSourceInstanceSettings } from '@grafana/data';
  2. import { reduxTester } from '../../../../test/core/redux/reduxTester';
  3. import { getMockPlugin } from '../../plugins/__mocks__/pluginMocks';
  4. import { variableAdapters } from '../adapters';
  5. import { changeVariableEditorExtended } from '../editor/reducer';
  6. import { datasourceBuilder } from '../shared/testing/builders';
  7. import { getDataSourceInstanceSetting } from '../shared/testing/helpers';
  8. import { getRootReducer, RootReducerType } from '../state/helpers';
  9. import { toKeyedAction } from '../state/keyedVariablesReducer';
  10. import { addVariable, setCurrentVariableValue } from '../state/sharedReducer';
  11. import { toKeyedVariableIdentifier, toVariablePayload } from '../utils';
  12. import {
  13. DataSourceVariableActionDependencies,
  14. initDataSourceVariableEditor,
  15. updateDataSourceVariableOptions,
  16. } from './actions';
  17. import { createDataSourceVariableAdapter } from './adapter';
  18. import { createDataSourceOptions } from './reducer';
  19. interface Args {
  20. sources?: DataSourceInstanceSettings[];
  21. query?: string;
  22. regex?: string;
  23. }
  24. function getTestContext({ sources = [], query, regex }: Args = {}) {
  25. const getListMock = jest.fn().mockReturnValue(sources);
  26. const getDatasourceSrvMock = jest.fn().mockReturnValue({ getList: getListMock });
  27. const dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrvMock };
  28. const datasource = datasourceBuilder().withId('0').withRootStateKey('key').withQuery(query).withRegEx(regex).build();
  29. return { getListMock, getDatasourceSrvMock, dependencies, datasource };
  30. }
  31. describe('data source actions', () => {
  32. variableAdapters.setInit(() => [createDataSourceVariableAdapter()]);
  33. describe('when updateDataSourceVariableOptions is dispatched', () => {
  34. describe('and there is no regex', () => {
  35. it('then the correct actions are dispatched', async () => {
  36. const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
  37. const sources: DataSourceInstanceSettings[] = [
  38. getDataSourceInstanceSetting('first-name', meta),
  39. getDataSourceInstanceSetting('second-name', meta),
  40. ];
  41. const { datasource, dependencies, getListMock, getDatasourceSrvMock } = getTestContext({
  42. sources,
  43. query: 'mock-data-id',
  44. });
  45. const tester = await reduxTester<RootReducerType>()
  46. .givenRootReducer(getRootReducer())
  47. .whenActionIsDispatched(
  48. toKeyedAction(
  49. 'key',
  50. addVariable(toVariablePayload(datasource, { global: false, index: 0, model: datasource }))
  51. )
  52. )
  53. .whenAsyncActionIsDispatched(
  54. updateDataSourceVariableOptions(toKeyedVariableIdentifier(datasource), dependencies),
  55. true
  56. );
  57. tester.thenDispatchedActionsShouldEqual(
  58. toKeyedAction(
  59. 'key',
  60. createDataSourceOptions(
  61. toVariablePayload(
  62. { type: 'datasource', id: '0' },
  63. {
  64. sources,
  65. regex: undefined as unknown as RegExp,
  66. }
  67. )
  68. )
  69. ),
  70. toKeyedAction(
  71. 'key',
  72. setCurrentVariableValue(
  73. toVariablePayload(
  74. { type: 'datasource', id: '0' },
  75. { option: { text: 'first-name', value: 'first-name', selected: false } }
  76. )
  77. )
  78. )
  79. );
  80. expect(getListMock).toHaveBeenCalledTimes(1);
  81. expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: false });
  82. expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
  83. });
  84. });
  85. describe('and there is a regex', () => {
  86. it('then the correct actions are dispatched', async () => {
  87. const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
  88. const sources: DataSourceInstanceSettings[] = [
  89. getDataSourceInstanceSetting('first-name', meta),
  90. getDataSourceInstanceSetting('second-name', meta),
  91. ];
  92. const { datasource, dependencies, getListMock, getDatasourceSrvMock } = getTestContext({
  93. sources,
  94. query: 'mock-data-id',
  95. regex: '/.*(second-name).*/',
  96. });
  97. const tester = await reduxTester<RootReducerType>()
  98. .givenRootReducer(getRootReducer())
  99. .whenActionIsDispatched(
  100. toKeyedAction(
  101. 'key',
  102. addVariable(toVariablePayload(datasource, { global: false, index: 0, model: datasource }))
  103. )
  104. )
  105. .whenAsyncActionIsDispatched(
  106. updateDataSourceVariableOptions(toKeyedVariableIdentifier(datasource), dependencies),
  107. true
  108. );
  109. tester.thenDispatchedActionsShouldEqual(
  110. toKeyedAction(
  111. 'key',
  112. createDataSourceOptions(
  113. toVariablePayload(
  114. { type: 'datasource', id: '0' },
  115. {
  116. sources,
  117. regex: /.*(second-name).*/,
  118. }
  119. )
  120. )
  121. ),
  122. toKeyedAction(
  123. 'key',
  124. setCurrentVariableValue(
  125. toVariablePayload(
  126. { type: 'datasource', id: '0' },
  127. { option: { text: 'second-name', value: 'second-name', selected: false } }
  128. )
  129. )
  130. )
  131. );
  132. expect(getListMock).toHaveBeenCalledTimes(1);
  133. expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: false });
  134. expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
  135. });
  136. });
  137. });
  138. describe('when initDataSourceVariableEditor is dispatched', () => {
  139. it('then the correct actions are dispatched', () => {
  140. const meta = getMockPlugin({ name: 'mock-data-name', id: 'mock-data-id' });
  141. const sources: DataSourceInstanceSettings[] = [
  142. getDataSourceInstanceSetting('first-name', meta),
  143. getDataSourceInstanceSetting('second-name', meta),
  144. ];
  145. const { dependencies, getListMock, getDatasourceSrvMock } = getTestContext({ sources });
  146. reduxTester<RootReducerType>()
  147. .givenRootReducer(getRootReducer())
  148. .whenActionIsDispatched(initDataSourceVariableEditor('key', dependencies))
  149. .thenDispatchedActionsShouldEqual(
  150. toKeyedAction(
  151. 'key',
  152. changeVariableEditorExtended({
  153. dataSourceTypes: [
  154. { text: '', value: '' },
  155. { text: 'mock-data-name', value: 'mock-data-id' },
  156. ],
  157. })
  158. )
  159. );
  160. expect(getListMock).toHaveBeenCalledTimes(1);
  161. expect(getListMock).toHaveBeenCalledWith({ metrics: true, variables: true });
  162. expect(getDatasourceSrvMock).toHaveBeenCalledTimes(1);
  163. });
  164. });
  165. });