reducer.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { cloneDeep } from 'lodash';
  2. import { DataSourceInstanceSettings } from '@grafana/data';
  3. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  4. import { getMockPlugins } from '../../plugins/__mocks__/pluginMocks';
  5. import { getDataSourceInstanceSetting } from '../shared/testing/helpers';
  6. import { getVariableTestContext } from '../state/helpers';
  7. import { VariablesState } from '../state/types';
  8. import { DataSourceVariableModel } from '../types';
  9. import { toVariablePayload } from '../utils';
  10. import { createDataSourceVariableAdapter } from './adapter';
  11. import { createDataSourceOptions, dataSourceVariableReducer } from './reducer';
  12. describe('dataSourceVariableReducer', () => {
  13. const adapter = createDataSourceVariableAdapter();
  14. describe('when createDataSourceOptions is dispatched', () => {
  15. const plugins = getMockPlugins(3);
  16. const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p));
  17. it.each`
  18. query | regex | includeAll | expected
  19. ${sources[1].meta.id} | ${undefined} | ${false} | ${[{ text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false }]}
  20. ${'not-found-plugin'} | ${undefined} | ${false} | ${[{ text: 'No data sources found', value: '', selected: false }]}
  21. ${sources[1].meta.id} | ${/.*(pretty cool plugin-1).*/} | ${false} | ${[{ text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false }]}
  22. ${sources[1].meta.id} | ${/.*(pretty cool plugin-2).*/} | ${false} | ${[{ text: 'No data sources found', value: '', selected: false }]}
  23. ${sources[1].meta.id} | ${undefined} | ${true} | ${[{ text: 'All', value: '$__all', selected: false }, { text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false }]}
  24. ${'not-found-plugin'} | ${undefined} | ${true} | ${[{ text: 'All', value: '$__all', selected: false }, { text: 'No data sources found', value: '', selected: false }]}
  25. ${sources[1].meta.id} | ${/.*(pretty cool plugin-1).*/} | ${true} | ${[{ text: 'All', value: '$__all', selected: false }, { text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false }]}
  26. ${sources[1].meta.id} | ${/.*(pretty cool plugin-2).*/} | ${true} | ${[{ text: 'All', value: '$__all', selected: false }, { text: 'No data sources found', value: '', selected: false }]}
  27. `(
  28. "when called with query: '$query' and regex: '$regex' and includeAll: '$includeAll' then state should be correct",
  29. ({ query, regex, includeAll, expected }) => {
  30. const { initialState } = getVariableTestContext<DataSourceVariableModel>(adapter, { query, includeAll });
  31. const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex });
  32. reducerTester<VariablesState>()
  33. .givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
  34. .whenActionIsDispatched(createDataSourceOptions(payload))
  35. .thenStateShouldEqual({
  36. ...initialState,
  37. ['0']: {
  38. ...initialState['0'],
  39. options: expected,
  40. } as unknown as DataSourceVariableModel,
  41. });
  42. }
  43. );
  44. });
  45. describe('when createDataSourceOptions is dispatched and item is default data source', () => {
  46. it('then the state should include an extra default option', () => {
  47. const plugins = getMockPlugins(3);
  48. const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p));
  49. sources[1].isDefault = true;
  50. const { initialState } = getVariableTestContext<DataSourceVariableModel>(adapter, {
  51. query: sources[1].meta.id,
  52. includeAll: false,
  53. });
  54. const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: undefined });
  55. reducerTester<VariablesState>()
  56. .givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
  57. .whenActionIsDispatched(createDataSourceOptions(payload))
  58. .thenStateShouldEqual({
  59. ...initialState,
  60. ['0']: {
  61. ...initialState['0'],
  62. options: [
  63. { text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false },
  64. { text: 'default', value: 'default', selected: false },
  65. ],
  66. } as unknown as DataSourceVariableModel,
  67. });
  68. });
  69. });
  70. describe('when createDataSourceOptions is dispatched with default in the regex and item is default data source', () => {
  71. it('then the state should include an extra default option', () => {
  72. const plugins = getMockPlugins(3);
  73. const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p));
  74. sources[1].isDefault = true;
  75. const { initialState } = getVariableTestContext<DataSourceVariableModel>(adapter, {
  76. query: sources[1].meta.id,
  77. includeAll: false,
  78. });
  79. const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: /default/ });
  80. reducerTester<VariablesState>()
  81. .givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
  82. .whenActionIsDispatched(createDataSourceOptions(payload))
  83. .thenStateShouldEqual({
  84. ...initialState,
  85. ['0']: {
  86. ...initialState['0'],
  87. options: [{ text: 'default', value: 'default', selected: false }],
  88. } as unknown as DataSourceVariableModel,
  89. });
  90. });
  91. });
  92. describe('when createDataSourceOptions is dispatched without default in the regex and item is default data source', () => {
  93. it('then the state not should include an extra default option', () => {
  94. const plugins = getMockPlugins(3);
  95. const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p));
  96. sources[1].isDefault = true;
  97. const { initialState } = getVariableTestContext<DataSourceVariableModel>(adapter, {
  98. query: sources[1].meta.id,
  99. includeAll: false,
  100. });
  101. const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: /pretty/ });
  102. reducerTester<VariablesState>()
  103. .givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
  104. .whenActionIsDispatched(createDataSourceOptions(payload))
  105. .thenStateShouldEqual({
  106. ...initialState,
  107. ['0']: {
  108. ...initialState['0'],
  109. options: [{ text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false }],
  110. } as unknown as DataSourceVariableModel,
  111. });
  112. });
  113. });
  114. describe('when createDataSourceOptions is dispatched without the regex and item is default data source', () => {
  115. it('then the state should include an extra default option', () => {
  116. const plugins = getMockPlugins(3);
  117. const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p));
  118. sources[1].isDefault = true;
  119. const { initialState } = getVariableTestContext<DataSourceVariableModel>(adapter, {
  120. query: sources[1].meta.id,
  121. includeAll: false,
  122. });
  123. const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: undefined });
  124. reducerTester<VariablesState>()
  125. .givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
  126. .whenActionIsDispatched(createDataSourceOptions(payload))
  127. .thenStateShouldEqual({
  128. ...initialState,
  129. ['0']: {
  130. ...initialState['0'],
  131. options: [
  132. { text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false },
  133. { text: 'default', value: 'default', selected: false },
  134. ],
  135. } as unknown as DataSourceVariableModel,
  136. });
  137. });
  138. });
  139. });