DashboardPage.test.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import React from 'react';
  2. import { shallow, ShallowWrapper } from 'enzyme';
  3. import { DashboardPage, Props, State } from './DashboardPage';
  4. import { DashboardModel } from '../state';
  5. import { cleanUpDashboard } from '../state/actions';
  6. import { getNoPayloadActionCreatorMock, NoPayloadActionCreatorMock } from 'app/core/redux';
  7. import { DashboardRouteInfo, DashboardInitPhase } from 'app/types';
  8. jest.mock('sass/_variables.scss', () => ({
  9. panelhorizontalpadding: 10,
  10. panelVerticalPadding: 10,
  11. }));
  12. jest.mock('app/features/dashboard/components/DashboardSettings/SettingsCtrl', () => ({}));
  13. interface ScenarioContext {
  14. cleanUpDashboardMock: NoPayloadActionCreatorMock;
  15. dashboard?: DashboardModel;
  16. setDashboardProp: (overrides?: any, metaOverrides?: any) => void;
  17. wrapper?: ShallowWrapper<Props, State, DashboardPage>;
  18. mount: (propOverrides?: Partial<Props>) => void;
  19. setup?: (fn: () => void) => void;
  20. }
  21. function getTestDashboard(overrides?: any, metaOverrides?: any): DashboardModel {
  22. const data = Object.assign(
  23. {
  24. title: 'My dashboard',
  25. panels: [
  26. {
  27. id: 1,
  28. type: 'graph',
  29. title: 'My graph',
  30. gridPos: { x: 0, y: 0, w: 1, h: 1 },
  31. },
  32. ],
  33. },
  34. overrides
  35. );
  36. const meta = Object.assign({ canSave: true, canEdit: true }, metaOverrides);
  37. return new DashboardModel(data, meta);
  38. }
  39. function dashboardPageScenario(description, scenarioFn: (ctx: ScenarioContext) => void) {
  40. describe(description, () => {
  41. let setupFn: () => void;
  42. const ctx: ScenarioContext = {
  43. cleanUpDashboardMock: getNoPayloadActionCreatorMock(cleanUpDashboard),
  44. setup: fn => {
  45. setupFn = fn;
  46. },
  47. setDashboardProp: (overrides?: any, metaOverrides?: any) => {
  48. ctx.dashboard = getTestDashboard(overrides, metaOverrides);
  49. ctx.wrapper.setProps({ dashboard: ctx.dashboard });
  50. },
  51. mount: (propOverrides?: Partial<Props>) => {
  52. const props: Props = {
  53. urlSlug: 'my-dash',
  54. $scope: {},
  55. urlUid: '11',
  56. $injector: {},
  57. routeInfo: DashboardRouteInfo.Normal,
  58. urlEdit: false,
  59. urlFullscreen: false,
  60. initPhase: DashboardInitPhase.NotStarted,
  61. isInitSlow: false,
  62. initDashboard: jest.fn(),
  63. updateLocation: jest.fn(),
  64. notifyApp: jest.fn(),
  65. cleanUpDashboard: ctx.cleanUpDashboardMock,
  66. dashboard: null,
  67. };
  68. Object.assign(props, propOverrides);
  69. ctx.dashboard = props.dashboard;
  70. ctx.wrapper = shallow(<DashboardPage {...props} />);
  71. },
  72. };
  73. beforeEach(() => {
  74. setupFn();
  75. });
  76. scenarioFn(ctx);
  77. });
  78. }
  79. describe('DashboardPage', () => {
  80. dashboardPageScenario('Given initial state', ctx => {
  81. ctx.setup(() => {
  82. ctx.mount();
  83. });
  84. it('Should render nothing', () => {
  85. expect(ctx.wrapper).toMatchSnapshot();
  86. });
  87. });
  88. dashboardPageScenario('Dashboard is fetching slowly', ctx => {
  89. ctx.setup(() => {
  90. ctx.mount();
  91. ctx.wrapper.setProps({
  92. isInitSlow: true,
  93. initPhase: DashboardInitPhase.Fetching,
  94. });
  95. });
  96. it('Should render slow init state', () => {
  97. expect(ctx.wrapper).toMatchSnapshot();
  98. });
  99. });
  100. dashboardPageScenario('Dashboard init completed ', ctx => {
  101. ctx.setup(() => {
  102. ctx.mount();
  103. ctx.setDashboardProp();
  104. });
  105. it('Should update title', () => {
  106. expect(document.title).toBe('My dashboard - Grafana');
  107. });
  108. it('Should render dashboard grid', () => {
  109. expect(ctx.wrapper).toMatchSnapshot();
  110. });
  111. });
  112. dashboardPageScenario('When user goes into panel edit', ctx => {
  113. ctx.setup(() => {
  114. ctx.mount();
  115. ctx.setDashboardProp();
  116. ctx.wrapper.setProps({
  117. urlFullscreen: true,
  118. urlEdit: true,
  119. urlPanelId: '1',
  120. });
  121. });
  122. it('Should update model state to fullscreen & edit', () => {
  123. expect(ctx.dashboard.meta.fullscreen).toBe(true);
  124. expect(ctx.dashboard.meta.isEditing).toBe(true);
  125. });
  126. it('Should update component state to fullscreen and edit', () => {
  127. const state = ctx.wrapper.state();
  128. expect(state.isEditing).toBe(true);
  129. expect(state.isFullscreen).toBe(true);
  130. });
  131. });
  132. dashboardPageScenario('When user goes back to dashboard from panel edit', ctx => {
  133. ctx.setup(() => {
  134. ctx.mount();
  135. ctx.setDashboardProp();
  136. ctx.wrapper.setState({ scrollTop: 100 });
  137. ctx.wrapper.setProps({
  138. urlFullscreen: true,
  139. urlEdit: true,
  140. urlPanelId: '1',
  141. });
  142. ctx.wrapper.setProps({
  143. urlFullscreen: false,
  144. urlEdit: false,
  145. urlPanelId: null,
  146. });
  147. });
  148. it('Should update model state normal state', () => {
  149. expect(ctx.dashboard.meta.fullscreen).toBe(false);
  150. expect(ctx.dashboard.meta.isEditing).toBe(false);
  151. });
  152. it('Should update component state to normal and restore scrollTop', () => {
  153. const state = ctx.wrapper.state();
  154. expect(state.isEditing).toBe(false);
  155. expect(state.isFullscreen).toBe(false);
  156. expect(state.scrollTop).toBe(100);
  157. });
  158. });
  159. dashboardPageScenario('When dashboard has editview url state', ctx => {
  160. ctx.setup(() => {
  161. ctx.mount();
  162. ctx.setDashboardProp();
  163. ctx.wrapper.setProps({
  164. editview: 'settings',
  165. });
  166. });
  167. it('should render settings view', () => {
  168. expect(ctx.wrapper).toMatchSnapshot();
  169. });
  170. it('should set animation state', () => {
  171. expect(ctx.wrapper.state().isSettingsOpening).toBe(true);
  172. });
  173. });
  174. dashboardPageScenario('When adding panel', ctx => {
  175. ctx.setup(() => {
  176. ctx.mount();
  177. ctx.setDashboardProp();
  178. ctx.wrapper.setState({ scrollTop: 100 });
  179. ctx.wrapper.instance().onAddPanel();
  180. });
  181. it('should set scrollTop to 0', () => {
  182. expect(ctx.wrapper.state().scrollTop).toBe(0);
  183. });
  184. it('should add panel widget to dashboard panels', () => {
  185. expect(ctx.dashboard.panels[0].type).toBe('add-panel');
  186. });
  187. });
  188. dashboardPageScenario('Given panel with id 0', ctx => {
  189. ctx.setup(() => {
  190. ctx.mount();
  191. ctx.setDashboardProp({
  192. panels: [{ id: 0, type: 'graph' }],
  193. schemaVersion: 17,
  194. });
  195. ctx.wrapper.setProps({
  196. urlEdit: true,
  197. urlFullscreen: true,
  198. urlPanelId: '0',
  199. });
  200. });
  201. it('Should go into edit mode', () => {
  202. expect(ctx.wrapper.state().isEditing).toBe(true);
  203. expect(ctx.wrapper.state().fullscreenPanel.id).toBe(0);
  204. });
  205. });
  206. dashboardPageScenario('When dashboard unmounts', ctx => {
  207. ctx.setup(() => {
  208. ctx.mount();
  209. ctx.setDashboardProp({
  210. panels: [{ id: 0, type: 'graph' }],
  211. schemaVersion: 17,
  212. });
  213. ctx.wrapper.unmount();
  214. });
  215. it('Should call clean up action', () => {
  216. expect(ctx.cleanUpDashboardMock.calls).toBe(1);
  217. });
  218. });
  219. });