initDashboard.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import configureMockStore from 'redux-mock-store';
  2. import thunk from 'redux-thunk';
  3. import { initDashboard, InitDashboardArgs } from './initDashboard';
  4. import { DashboardRouteInfo } from 'app/types';
  5. import { getBackendSrv } from 'app/core/services/backend_srv';
  6. import {
  7. dashboardInitFetching,
  8. dashboardInitCompleted,
  9. dashboardInitServices,
  10. } from './actions';
  11. jest.mock('app/core/services/backend_srv');
  12. const mockStore = configureMockStore([thunk]);
  13. interface ScenarioContext {
  14. args: InitDashboardArgs;
  15. timeSrv: any;
  16. annotationsSrv: any;
  17. unsavedChangesSrv: any;
  18. variableSrv: any;
  19. dashboardSrv: any;
  20. keybindingSrv: any;
  21. backendSrv: any;
  22. setup: (fn: () => void) => void;
  23. actions: any[];
  24. storeState: any;
  25. }
  26. type ScenarioFn = (ctx: ScenarioContext) => void;
  27. function describeInitScenario(description: string, scenarioFn: ScenarioFn) {
  28. describe(description, () => {
  29. const timeSrv = { init: jest.fn() };
  30. const annotationsSrv = { init: jest.fn() };
  31. const unsavedChangesSrv = { init: jest.fn() };
  32. const variableSrv = { init: jest.fn() };
  33. const dashboardSrv = { setCurrent: jest.fn() };
  34. const keybindingSrv = { setupDashboardBindings: jest.fn() };
  35. const injectorMock = {
  36. get: (name: string) => {
  37. switch (name) {
  38. case 'timeSrv':
  39. return timeSrv;
  40. case 'annotationsSrv':
  41. return annotationsSrv;
  42. case 'unsavedChangesSrv':
  43. return unsavedChangesSrv;
  44. case 'dashboardSrv':
  45. return dashboardSrv;
  46. case 'variableSrv':
  47. return variableSrv;
  48. case 'keybindingSrv':
  49. return keybindingSrv;
  50. default:
  51. throw { message: 'Unknown service ' + name };
  52. }
  53. },
  54. };
  55. let setupFn = () => {};
  56. const ctx: ScenarioContext = {
  57. args: {
  58. $injector: injectorMock,
  59. $scope: {},
  60. fixUrl: false,
  61. routeInfo: DashboardRouteInfo.Normal,
  62. },
  63. backendSrv: getBackendSrv(),
  64. timeSrv,
  65. annotationsSrv,
  66. unsavedChangesSrv,
  67. variableSrv,
  68. dashboardSrv,
  69. keybindingSrv,
  70. actions: [],
  71. storeState: {
  72. location: {
  73. query: {},
  74. },
  75. user: {},
  76. },
  77. setup: (fn: () => void) => {
  78. setupFn = fn;
  79. },
  80. };
  81. beforeEach(async () => {
  82. setupFn();
  83. const store = mockStore(ctx.storeState);
  84. await store.dispatch(initDashboard(ctx.args));
  85. ctx.actions = store.getActions();
  86. });
  87. scenarioFn(ctx);
  88. });
  89. }
  90. describeInitScenario('Initializing new dashboard', ctx => {
  91. ctx.setup(() => {
  92. ctx.storeState.user.orgId = 12;
  93. ctx.args.routeInfo = DashboardRouteInfo.New;
  94. });
  95. it('Should send action dashboardInitFetching', () => {
  96. expect(ctx.actions[0].type).toBe(dashboardInitFetching.type);
  97. });
  98. it('Should send action dashboardInitServices ', () => {
  99. expect(ctx.actions[1].type).toBe(dashboardInitServices.type);
  100. });
  101. it('Should update location with orgId query param', () => {
  102. expect(ctx.actions[2].type).toBe('UPDATE_LOCATION');
  103. expect(ctx.actions[2].payload.query.orgId).toBe(12);
  104. });
  105. it('Should send action dashboardInitCompleted', () => {
  106. expect(ctx.actions[3].type).toBe(dashboardInitCompleted.type);
  107. expect(ctx.actions[3].payload.title).toBe('New dashboard');
  108. });
  109. it('Should Initializing services', () => {
  110. expect(ctx.timeSrv.init).toBeCalled();
  111. expect(ctx.annotationsSrv.init).toBeCalled();
  112. expect(ctx.variableSrv.init).toBeCalled();
  113. expect(ctx.unsavedChangesSrv.init).toBeCalled();
  114. expect(ctx.keybindingSrv.setupDashboardBindings).toBeCalled();
  115. expect(ctx.dashboardSrv.setCurrent).toBeCalled();
  116. });
  117. });
  118. describeInitScenario('Initializing home dashboard', ctx => {
  119. ctx.setup(() => {
  120. ctx.args.routeInfo = DashboardRouteInfo.Home;
  121. ctx.backendSrv.get.mockReturnValue(Promise.resolve({
  122. redirectUri: '/u/123/my-home'
  123. }));
  124. });
  125. it('Should redirect to custom home dashboard', () => {
  126. expect(ctx.actions[1].type).toBe('UPDATE_LOCATION');
  127. expect(ctx.actions[1].payload.path).toBe('/u/123/my-home');
  128. });
  129. });