DashboardPage.test.tsx 7.5 KB

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