reducers.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { FolderDTO, FolderState, OrgRole, PermissionLevel } from 'app/types';
  2. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  3. import {
  4. folderReducer,
  5. initialState,
  6. loadFolder,
  7. loadFolderPermissions,
  8. setCanViewFolderPermissions,
  9. setFolderTitle,
  10. } from './reducers';
  11. function getTestFolder(): FolderDTO {
  12. return {
  13. id: 1,
  14. title: 'test folder',
  15. uid: 'asd',
  16. url: 'url',
  17. canSave: true,
  18. canEdit: true,
  19. canAdmin: true,
  20. canDelete: true,
  21. version: 0,
  22. };
  23. }
  24. describe('folder reducer', () => {
  25. describe('when loadFolder is dispatched', () => {
  26. it('should load folder and set hasChanged to false', () => {
  27. reducerTester<FolderState>()
  28. .givenReducer(folderReducer, { ...initialState, hasChanged: true })
  29. .whenActionIsDispatched(loadFolder(getTestFolder()))
  30. .thenStateShouldEqual({
  31. ...initialState,
  32. hasChanged: false,
  33. ...getTestFolder(),
  34. });
  35. });
  36. });
  37. describe('when setFolderTitle is dispatched', () => {
  38. describe('and title has length', () => {
  39. it('then state should be correct', () => {
  40. reducerTester<FolderState>()
  41. .givenReducer(folderReducer, { ...initialState })
  42. .whenActionIsDispatched(setFolderTitle('ready'))
  43. .thenStateShouldEqual({
  44. ...initialState,
  45. hasChanged: true,
  46. title: 'ready',
  47. });
  48. });
  49. });
  50. describe('and title has no length', () => {
  51. it('then state should be correct', () => {
  52. reducerTester<FolderState>()
  53. .givenReducer(folderReducer, { ...initialState })
  54. .whenActionIsDispatched(setFolderTitle(''))
  55. .thenStateShouldEqual({
  56. ...initialState,
  57. hasChanged: false,
  58. title: '',
  59. });
  60. });
  61. });
  62. });
  63. describe('when loadFolderPermissions is dispatched', () => {
  64. it('then state should be correct', () => {
  65. reducerTester<FolderState>()
  66. .givenReducer(folderReducer, { ...initialState })
  67. .whenActionIsDispatched(
  68. loadFolderPermissions([
  69. { id: 2, dashboardId: 1, role: OrgRole.Viewer, permission: PermissionLevel.View },
  70. { id: 3, dashboardId: 1, role: OrgRole.Editor, permission: PermissionLevel.Edit },
  71. {
  72. id: 4,
  73. dashboardId: 10,
  74. permission: PermissionLevel.View,
  75. teamId: 1,
  76. team: 'MyTestTeam',
  77. inherited: true,
  78. },
  79. {
  80. id: 5,
  81. dashboardId: 1,
  82. permission: PermissionLevel.View,
  83. userId: 1,
  84. userLogin: 'MyTestUser',
  85. },
  86. {
  87. id: 6,
  88. dashboardId: 1,
  89. permission: PermissionLevel.Edit,
  90. teamId: 2,
  91. team: 'MyTestTeam2',
  92. },
  93. ])
  94. )
  95. .thenStateShouldEqual({
  96. ...initialState,
  97. permissions: [
  98. {
  99. dashboardId: 10,
  100. id: 4,
  101. inherited: true,
  102. name: 'MyTestTeam',
  103. permission: 1,
  104. sortRank: 120,
  105. team: 'MyTestTeam',
  106. teamId: 1,
  107. },
  108. {
  109. dashboardId: 1,
  110. icon: 'fa fa-fw fa-street-view',
  111. id: 3,
  112. name: 'Editor',
  113. permission: 2,
  114. role: OrgRole.Editor,
  115. sortRank: 31,
  116. },
  117. {
  118. dashboardId: 1,
  119. icon: 'fa fa-fw fa-street-view',
  120. id: 2,
  121. name: 'Viewer',
  122. permission: 1,
  123. role: OrgRole.Viewer,
  124. sortRank: 30,
  125. },
  126. {
  127. dashboardId: 1,
  128. id: 6,
  129. name: 'MyTestTeam2',
  130. permission: 2,
  131. sortRank: 20,
  132. team: 'MyTestTeam2',
  133. teamId: 2,
  134. },
  135. {
  136. dashboardId: 1,
  137. id: 5,
  138. name: 'MyTestUser',
  139. permission: 1,
  140. sortRank: 10,
  141. userId: 1,
  142. userLogin: 'MyTestUser',
  143. },
  144. ],
  145. });
  146. });
  147. });
  148. describe('setCanViewFolderPermissions', () => {
  149. it('should set the canViewFolderPermissions value', () => {
  150. reducerTester<FolderState>()
  151. .givenReducer(folderReducer, { ...initialState })
  152. .whenActionIsDispatched(setCanViewFolderPermissions(true))
  153. .thenStateShouldEqual({
  154. ...initialState,
  155. canViewFolderPermissions: true,
  156. });
  157. });
  158. });
  159. });