reducers.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Action, ActionTypes } from './actions';
  2. import { FolderDTO, OrgRole, PermissionLevel, FolderState } from 'app/types';
  3. import { inititalState, folderReducer } from './reducers';
  4. function getTestFolder(): FolderDTO {
  5. return {
  6. id: 1,
  7. title: 'test folder',
  8. uid: 'asd',
  9. url: 'url',
  10. canSave: true,
  11. version: 0,
  12. };
  13. }
  14. describe('folder reducer', () => {
  15. describe('loadFolder', () => {
  16. it('should load folder and set hasChanged to false', () => {
  17. const folder = getTestFolder();
  18. const action: Action = {
  19. type: ActionTypes.LoadFolder,
  20. payload: folder,
  21. };
  22. const state = folderReducer(inititalState, action);
  23. expect(state.hasChanged).toEqual(false);
  24. expect(state.title).toEqual('test folder');
  25. });
  26. });
  27. describe('detFolderTitle', () => {
  28. it('should set title', () => {
  29. const action: Action = {
  30. type: ActionTypes.SetFolderTitle,
  31. payload: 'new title',
  32. };
  33. const state = folderReducer(inititalState, action);
  34. expect(state.hasChanged).toEqual(true);
  35. expect(state.title).toEqual('new title');
  36. });
  37. });
  38. describe('loadFolderPermissions', () => {
  39. let state: FolderState;
  40. beforeEach(() => {
  41. const action: Action = {
  42. type: ActionTypes.LoadFolderPermissions,
  43. payload: [
  44. { id: 2, dashboardId: 1, role: OrgRole.Viewer, permission: PermissionLevel.View },
  45. { id: 3, dashboardId: 1, role: OrgRole.Editor, permission: PermissionLevel.Edit },
  46. {
  47. id: 4,
  48. dashboardId: 10,
  49. permission: PermissionLevel.View,
  50. teamId: 1,
  51. team: 'MyTestTeam',
  52. inherited: true,
  53. },
  54. {
  55. id: 5,
  56. dashboardId: 1,
  57. permission: PermissionLevel.View,
  58. userId: 1,
  59. userLogin: 'MyTestUser',
  60. },
  61. {
  62. id: 6,
  63. dashboardId: 1,
  64. permission: PermissionLevel.Edit,
  65. teamId: 2,
  66. team: 'MyTestTeam2',
  67. },
  68. ],
  69. };
  70. state = folderReducer(inititalState, action);
  71. });
  72. it('should add permissions to state', async () => {
  73. expect(state.permissions.length).toBe(5);
  74. });
  75. it('should be sorted by sort rank and alphabetically', async () => {
  76. expect(state.permissions[0].name).toBe('MyTestTeam');
  77. expect(state.permissions[0].dashboardId).toBe(10);
  78. expect(state.permissions[1].name).toBe('Editor');
  79. expect(state.permissions[2].name).toBe('Viewer');
  80. expect(state.permissions[3].name).toBe('MyTestTeam2');
  81. expect(state.permissions[4].name).toBe('MyTestUser');
  82. });
  83. });
  84. });