reducers.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { DashboardInitPhase, DashboardState, OrgRole, PermissionLevel } from 'app/types';
  2. import { DashboardModel } from './DashboardModel';
  3. import {
  4. dashboardInitCompleted,
  5. dashboardInitFailed,
  6. dashboardInitFetching,
  7. loadDashboardPermissions,
  8. dashboardReducer,
  9. initialState,
  10. } from './reducers';
  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(
  30. state,
  31. dashboardInitCompleted(
  32. new DashboardModel({
  33. title: 'My dashboard',
  34. panels: [{ id: 1 }, { id: 2 }],
  35. })
  36. )
  37. );
  38. });
  39. it('should set model', async () => {
  40. expect(state.getModel()!.title).toBe('My dashboard');
  41. });
  42. });
  43. describe('dashboardInitFailed', () => {
  44. let state: DashboardState;
  45. beforeEach(() => {
  46. state = dashboardReducer(initialState, dashboardInitFetching());
  47. state = dashboardReducer(state, dashboardInitFailed({ message: 'Oh no', error: 'sad' }));
  48. });
  49. it('should set model', async () => {
  50. expect(state.getModel()?.title).toBe('Dashboard init failed');
  51. });
  52. it('should set initError', async () => {
  53. expect(state.initError?.message).toBe('Oh no');
  54. });
  55. it('should set phase failed', async () => {
  56. expect(state.initPhase).toBe(DashboardInitPhase.Failed);
  57. });
  58. });
  59. });