reducers.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { reducerTester } from 'test/core/redux/reducerTester';
  2. import { PluginMeta, PluginMetaInfo, PluginType, LayoutModes } from '@grafana/data';
  3. import { DataSourceSettingsState, DataSourcesState } from 'app/types';
  4. import { getMockDataSource, getMockDataSources } from '../__mocks__/dataSourcesMocks';
  5. import { GenericDataSourcePlugin } from '../settings/PluginSettings';
  6. import {
  7. dataSourceLoaded,
  8. dataSourceMetaLoaded,
  9. dataSourcePluginsLoad,
  10. dataSourcePluginsLoaded,
  11. dataSourceSettingsReducer,
  12. dataSourcesLoaded,
  13. dataSourcesReducer,
  14. initDataSourceSettingsFailed,
  15. initDataSourceSettingsSucceeded,
  16. initialDataSourceSettingsState,
  17. initialState,
  18. setDataSourceName,
  19. setDataSourcesLayoutMode,
  20. setDataSourcesSearchQuery,
  21. setDataSourceTypeSearchQuery,
  22. setIsDefault,
  23. } from './reducers';
  24. const mockPlugin = () =>
  25. ({
  26. defaultNavUrl: 'defaultNavUrl',
  27. enabled: true,
  28. hasUpdate: true,
  29. id: 'id',
  30. info: {} as PluginMetaInfo,
  31. latestVersion: 'latestVersion',
  32. name: 'name',
  33. pinned: true,
  34. type: PluginType.datasource,
  35. module: 'path/to/module',
  36. } as PluginMeta);
  37. describe('dataSourcesReducer', () => {
  38. describe('when dataSourcesLoaded is dispatched', () => {
  39. it('then state should be correct', () => {
  40. const dataSources = getMockDataSources(1);
  41. reducerTester<DataSourcesState>()
  42. .givenReducer(dataSourcesReducer, initialState)
  43. .whenActionIsDispatched(dataSourcesLoaded(dataSources))
  44. .thenStateShouldEqual({ ...initialState, hasFetched: true, dataSources, dataSourcesCount: 1 });
  45. });
  46. });
  47. describe('when dataSourceLoaded is dispatched', () => {
  48. it('then state should be correct', () => {
  49. const dataSource = getMockDataSource();
  50. reducerTester<DataSourcesState>()
  51. .givenReducer(dataSourcesReducer, initialState)
  52. .whenActionIsDispatched(dataSourceLoaded(dataSource))
  53. .thenStateShouldEqual({ ...initialState, dataSource });
  54. });
  55. });
  56. describe('when setDataSourcesSearchQuery is dispatched', () => {
  57. it('then state should be correct', () => {
  58. reducerTester<DataSourcesState>()
  59. .givenReducer(dataSourcesReducer, initialState)
  60. .whenActionIsDispatched(setDataSourcesSearchQuery('some query'))
  61. .thenStateShouldEqual({ ...initialState, searchQuery: 'some query' });
  62. });
  63. });
  64. describe('when setDataSourcesLayoutMode is dispatched', () => {
  65. it('then state should be correct', () => {
  66. const layoutMode: LayoutModes = LayoutModes.Grid;
  67. reducerTester<DataSourcesState>()
  68. .givenReducer(dataSourcesReducer, initialState)
  69. .whenActionIsDispatched(setDataSourcesLayoutMode(layoutMode))
  70. .thenStateShouldEqual({ ...initialState, layoutMode: LayoutModes.Grid });
  71. });
  72. });
  73. describe('when dataSourcePluginsLoad is dispatched', () => {
  74. it('then state should be correct', () => {
  75. const state: DataSourcesState = { ...initialState, plugins: [mockPlugin()] };
  76. reducerTester<DataSourcesState>()
  77. .givenReducer(dataSourcesReducer, state)
  78. .whenActionIsDispatched(dataSourcePluginsLoad())
  79. .thenStateShouldEqual({ ...initialState, isLoadingDataSources: true });
  80. });
  81. });
  82. describe('when dataSourcePluginsLoaded is dispatched', () => {
  83. it('then state should be correct', () => {
  84. const dataSourceTypes = [mockPlugin()];
  85. const state: DataSourcesState = { ...initialState, isLoadingDataSources: true };
  86. reducerTester<DataSourcesState>()
  87. .givenReducer(dataSourcesReducer, state)
  88. .whenActionIsDispatched(dataSourcePluginsLoaded({ plugins: dataSourceTypes, categories: [] }))
  89. .thenStateShouldEqual({ ...initialState, plugins: dataSourceTypes, isLoadingDataSources: false });
  90. });
  91. });
  92. describe('when setDataSourceTypeSearchQuery is dispatched', () => {
  93. it('then state should be correct', () => {
  94. reducerTester<DataSourcesState>()
  95. .givenReducer(dataSourcesReducer, initialState)
  96. .whenActionIsDispatched(setDataSourceTypeSearchQuery('type search query'))
  97. .thenStateShouldEqual({ ...initialState, dataSourceTypeSearchQuery: 'type search query' });
  98. });
  99. });
  100. describe('when dataSourceMetaLoaded is dispatched', () => {
  101. it('then state should be correct', () => {
  102. const dataSourceMeta = mockPlugin();
  103. reducerTester<DataSourcesState>()
  104. .givenReducer(dataSourcesReducer, initialState)
  105. .whenActionIsDispatched(dataSourceMetaLoaded(dataSourceMeta))
  106. .thenStateShouldEqual({ ...initialState, dataSourceMeta });
  107. });
  108. });
  109. describe('when setDataSourceName is dispatched', () => {
  110. it('then state should be correct', () => {
  111. reducerTester<DataSourcesState>()
  112. .givenReducer(dataSourcesReducer, initialState)
  113. .whenActionIsDispatched(setDataSourceName('some name'))
  114. .thenStateShouldEqual({ ...initialState, dataSource: { name: 'some name' } } as DataSourcesState);
  115. });
  116. });
  117. describe('when setIsDefault is dispatched', () => {
  118. it('then state should be correct', () => {
  119. reducerTester<DataSourcesState>()
  120. .givenReducer(dataSourcesReducer, initialState)
  121. .whenActionIsDispatched(setIsDefault(true))
  122. .thenStateShouldEqual({ ...initialState, dataSource: { isDefault: true } } as DataSourcesState);
  123. });
  124. });
  125. });
  126. describe('dataSourceSettingsReducer', () => {
  127. describe('when initDataSourceSettingsSucceeded is dispatched', () => {
  128. it('then state should be correct', () => {
  129. reducerTester<DataSourceSettingsState>()
  130. .givenReducer(dataSourceSettingsReducer, { ...initialDataSourceSettingsState })
  131. .whenActionIsDispatched(initDataSourceSettingsSucceeded({} as GenericDataSourcePlugin))
  132. .thenStateShouldEqual({
  133. ...initialDataSourceSettingsState,
  134. plugin: {} as GenericDataSourcePlugin,
  135. loading: false,
  136. });
  137. });
  138. });
  139. describe('when initDataSourceSettingsFailed is dispatched', () => {
  140. it('then state should be correct', () => {
  141. reducerTester<DataSourceSettingsState>()
  142. .givenReducer(dataSourceSettingsReducer, {
  143. ...initialDataSourceSettingsState,
  144. plugin: {} as GenericDataSourcePlugin,
  145. })
  146. .whenActionIsDispatched(initDataSourceSettingsFailed(new Error('Some error')))
  147. .thenStatePredicateShouldEqual((resultingState) => {
  148. expect(resultingState).toEqual({
  149. testingStatus: {},
  150. loadError: 'Some error',
  151. loading: false,
  152. plugin: null,
  153. });
  154. return true;
  155. });
  156. });
  157. });
  158. });