reducer.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { LoadingState } from '@grafana/data';
  2. import { reducerTester } from '../../../../../test/core/redux/reducerTester';
  3. import { LibraryElementDTO, LibraryElementKind } from '../../types';
  4. import {
  5. changePage,
  6. initialLibraryPanelsViewState,
  7. initSearch,
  8. libraryPanelsViewReducer,
  9. LibraryPanelsViewState,
  10. searchCompleted,
  11. } from './reducer';
  12. describe('libraryPanelsViewReducer', () => {
  13. describe('when initSearch is dispatched', () => {
  14. it('then the state should be correct', () => {
  15. reducerTester<LibraryPanelsViewState>()
  16. .givenReducer(libraryPanelsViewReducer, { ...initialLibraryPanelsViewState })
  17. .whenActionIsDispatched(initSearch())
  18. .thenStateShouldEqual({
  19. ...initialLibraryPanelsViewState,
  20. loadingState: LoadingState.Loading,
  21. });
  22. });
  23. });
  24. describe('when searchCompleted is dispatched', () => {
  25. it('then the state should be correct', () => {
  26. const payload = {
  27. perPage: 10,
  28. page: 3,
  29. libraryPanels: getLibraryPanelMocks(2),
  30. totalCount: 200,
  31. };
  32. reducerTester<LibraryPanelsViewState>()
  33. .givenReducer(libraryPanelsViewReducer, { ...initialLibraryPanelsViewState })
  34. .whenActionIsDispatched(searchCompleted(payload))
  35. .thenStateShouldEqual({
  36. ...initialLibraryPanelsViewState,
  37. perPage: 10,
  38. page: 3,
  39. libraryPanels: payload.libraryPanels,
  40. totalCount: 200,
  41. loadingState: LoadingState.Done,
  42. numberOfPages: 20,
  43. });
  44. });
  45. describe('and page is greater than the current number of pages', () => {
  46. it('then the state should be correct', () => {
  47. const payload = {
  48. perPage: 10,
  49. page: 21,
  50. libraryPanels: getLibraryPanelMocks(2),
  51. totalCount: 200,
  52. };
  53. reducerTester<LibraryPanelsViewState>()
  54. .givenReducer(libraryPanelsViewReducer, { ...initialLibraryPanelsViewState })
  55. .whenActionIsDispatched(searchCompleted(payload))
  56. .thenStateShouldEqual({
  57. ...initialLibraryPanelsViewState,
  58. perPage: 10,
  59. page: 20,
  60. libraryPanels: payload.libraryPanels,
  61. totalCount: 200,
  62. loadingState: LoadingState.Done,
  63. numberOfPages: 20,
  64. });
  65. });
  66. });
  67. });
  68. describe('when changePage is dispatched', () => {
  69. it('then the state should be correct', () => {
  70. reducerTester<LibraryPanelsViewState>()
  71. .givenReducer(libraryPanelsViewReducer, { ...initialLibraryPanelsViewState })
  72. .whenActionIsDispatched(changePage({ page: 42 }))
  73. .thenStateShouldEqual({
  74. ...initialLibraryPanelsViewState,
  75. page: 42,
  76. });
  77. });
  78. });
  79. });
  80. function getLibraryPanelMocks(count: number): LibraryElementDTO[] {
  81. const mocks: LibraryElementDTO[] = [];
  82. for (let i = 0; i < count; i++) {
  83. mocks.push(
  84. mockLibraryPanel({
  85. uid: i.toString(10),
  86. id: i,
  87. name: `Test Panel ${i}`,
  88. })
  89. );
  90. }
  91. return mocks;
  92. }
  93. function mockLibraryPanel({
  94. uid = '1',
  95. id = 1,
  96. orgId = 1,
  97. folderId = 0,
  98. name = 'Test Panel',
  99. model = { type: 'text', title: 'Test Panel' },
  100. meta = {
  101. folderName: 'General',
  102. folderUid: '',
  103. connectedDashboards: 0,
  104. created: '2021-01-01T00:00:00',
  105. createdBy: { id: 1, name: 'User X', avatarUrl: '/avatar/abc' },
  106. updated: '2021-01-02T00:00:00',
  107. updatedBy: { id: 2, name: 'User Y', avatarUrl: '/avatar/xyz' },
  108. },
  109. version = 1,
  110. description = 'a description',
  111. type = 'text',
  112. }: Partial<LibraryElementDTO> = {}): LibraryElementDTO {
  113. return {
  114. uid,
  115. id,
  116. orgId,
  117. folderId,
  118. name,
  119. kind: LibraryElementKind.Panel,
  120. model,
  121. version,
  122. meta,
  123. description,
  124. type,
  125. };
  126. }