reducers.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { LoadingState } from '@grafana/data';
  2. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  3. import { LibraryElementDTO } from '../../library-panels/types';
  4. import {
  5. clearDashboard,
  6. DashboardSource,
  7. DataSourceInput,
  8. importDashboardReducer,
  9. ImportDashboardState,
  10. initialImportDashboardState,
  11. InputType,
  12. LibraryPanelInput,
  13. LibraryPanelInputState,
  14. setGcomDashboard,
  15. setInputs,
  16. setJsonDashboard,
  17. setLibraryPanelInputs,
  18. } from './reducers';
  19. describe('importDashboardReducer', () => {
  20. describe('when setGcomDashboard action is dispatched', () => {
  21. it('then resulting state should be correct', () => {
  22. reducerTester<ImportDashboardState>()
  23. .givenReducer(importDashboardReducer, { ...initialImportDashboardState })
  24. .whenActionIsDispatched(
  25. setGcomDashboard({ json: { id: 1, title: 'Imported' }, updatedAt: '2001-01-01', orgName: 'Some Org' })
  26. )
  27. .thenStateShouldEqual({
  28. ...initialImportDashboardState,
  29. dashboard: {
  30. title: 'Imported',
  31. id: null,
  32. },
  33. meta: { updatedAt: '2001-01-01', orgName: 'Some Org' },
  34. source: DashboardSource.Gcom,
  35. state: LoadingState.Done,
  36. });
  37. });
  38. });
  39. describe('when setJsonDashboard action is dispatched', () => {
  40. it('then resulting state should be correct', () => {
  41. reducerTester<ImportDashboardState>()
  42. .givenReducer(importDashboardReducer, { ...initialImportDashboardState, source: DashboardSource.Gcom })
  43. .whenActionIsDispatched(setJsonDashboard({ id: 1, title: 'Imported' }))
  44. .thenStateShouldEqual({
  45. ...initialImportDashboardState,
  46. dashboard: {
  47. title: 'Imported',
  48. id: null,
  49. },
  50. source: DashboardSource.Json,
  51. state: LoadingState.Done,
  52. });
  53. });
  54. });
  55. describe('when clearDashboard action is dispatched', () => {
  56. it('then resulting state should be correct', () => {
  57. reducerTester<ImportDashboardState>()
  58. .givenReducer(importDashboardReducer, {
  59. ...initialImportDashboardState,
  60. dashboard: {
  61. title: 'Imported',
  62. id: null,
  63. },
  64. state: LoadingState.Done,
  65. })
  66. .whenActionIsDispatched(clearDashboard())
  67. .thenStateShouldEqual({
  68. ...initialImportDashboardState,
  69. dashboard: {},
  70. state: LoadingState.NotStarted,
  71. });
  72. });
  73. });
  74. describe('when setInputs action is dispatched', () => {
  75. it('then resulting state should be correct', () => {
  76. reducerTester<ImportDashboardState>()
  77. .givenReducer(importDashboardReducer, { ...initialImportDashboardState })
  78. .whenActionIsDispatched(
  79. setInputs([
  80. { type: InputType.DataSource },
  81. { type: InputType.Constant },
  82. { type: InputType.LibraryPanel },
  83. { type: 'temp' },
  84. ])
  85. )
  86. .thenStateShouldEqual({
  87. ...initialImportDashboardState,
  88. inputs: {
  89. dataSources: [{ type: InputType.DataSource }] as DataSourceInput[],
  90. constants: [{ type: InputType.Constant }] as DataSourceInput[],
  91. libraryPanels: [],
  92. },
  93. });
  94. });
  95. });
  96. describe('when setLibraryPanelInputs action is dispatched', () => {
  97. it('then resulting state should be correct', () => {
  98. reducerTester<ImportDashboardState>()
  99. .givenReducer(importDashboardReducer, {
  100. ...initialImportDashboardState,
  101. inputs: {
  102. dataSources: [{ type: InputType.DataSource }] as DataSourceInput[],
  103. constants: [{ type: InputType.Constant }] as DataSourceInput[],
  104. libraryPanels: [{ model: { uid: 'asasAHSJ' } }] as LibraryPanelInput[],
  105. },
  106. })
  107. .whenActionIsDispatched(
  108. setLibraryPanelInputs([
  109. {
  110. model: { uid: 'sadjahsdk', name: 'A name', type: 'text' } as LibraryElementDTO,
  111. state: LibraryPanelInputState.Exists,
  112. },
  113. ])
  114. )
  115. .thenStateShouldEqual({
  116. ...initialImportDashboardState,
  117. inputs: {
  118. dataSources: [{ type: InputType.DataSource }] as DataSourceInput[],
  119. constants: [{ type: InputType.Constant }] as DataSourceInput[],
  120. libraryPanels: [
  121. {
  122. model: { uid: 'sadjahsdk', name: 'A name', type: 'text' } as LibraryElementDTO,
  123. state: LibraryPanelInputState.Exists,
  124. },
  125. ],
  126. },
  127. });
  128. });
  129. });
  130. });