initDashboard.test.ts 4.1 KB

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