reducers.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {
  2. loadDashboardPermissions,
  3. dashboardInitFetching,
  4. dashboardInitCompleted,
  5. dashboardInitFailed,
  6. dashboardInitSlow,
  7. } from './actions';
  8. import { OrgRole, PermissionLevel, DashboardState, DashboardInitPhase } from 'app/types';
  9. import { initialState, dashboardReducer } from './reducers';
  10. import { DashboardModel } from './DashboardModel';
  11. describe('dashboard reducer', () => {
  12. describe('loadDashboardPermissions', () => {
  13. let state: DashboardState;
  14. beforeEach(() => {
  15. const action = loadDashboardPermissions([
  16. { id: 2, dashboardId: 1, role: OrgRole.Viewer, permission: PermissionLevel.View },
  17. { id: 3, dashboardId: 1, role: OrgRole.Editor, permission: PermissionLevel.Edit },
  18. ]);
  19. state = dashboardReducer(initialState, action);
  20. });
  21. it('should add permissions to state', async () => {
  22. expect(state.permissions.length).toBe(2);
  23. });
  24. });
  25. describe('dashboardInitCompleted', () => {
  26. let state: DashboardState;
  27. beforeEach(() => {
  28. state = dashboardReducer(initialState, dashboardInitFetching());
  29. state = dashboardReducer(state, dashboardInitSlow());
  30. state = dashboardReducer(state, dashboardInitCompleted(new DashboardModel({ title: 'My dashboard' })));
  31. });
  32. it('should set model', async () => {
  33. expect(state.model.title).toBe('My dashboard');
  34. });
  35. it('should set reset isInitSlow', async () => {
  36. expect(state.isInitSlow).toBe(false);
  37. });
  38. });
  39. describe('dashboardInitFailed', () => {
  40. let state: DashboardState;
  41. beforeEach(() => {
  42. state = dashboardReducer(initialState, dashboardInitFetching());
  43. state = dashboardReducer(state, dashboardInitFailed({ message: 'Oh no', error: 'sad' }));
  44. });
  45. it('should set model', async () => {
  46. expect(state.model.title).toBe('Dashboard init failed');
  47. });
  48. it('should set reset isInitSlow', async () => {
  49. expect(state.isInitSlow).toBe(false);
  50. });
  51. it('should set initError', async () => {
  52. expect(state.initError.message).toBe('Oh no');
  53. });
  54. it('should set phase failed', async () => {
  55. expect(state.initPhase).toBe(DashboardInitPhase.Failed);
  56. });
  57. });
  58. });