DashboardGrid.test.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React from 'react';
  2. import { shallow, ShallowWrapper } from 'enzyme';
  3. import { DashboardGrid, Props } from './DashboardGrid';
  4. import { DashboardModel } from '../state';
  5. interface ScenarioContext {
  6. props: Props;
  7. wrapper?: ShallowWrapper<Props, any, DashboardGrid>;
  8. setup?: (fn: () => void) => void;
  9. setProps: (props: Partial<Props>) => void;
  10. }
  11. function getTestDashboard(overrides?: any, metaOverrides?: any): DashboardModel {
  12. const data = Object.assign(
  13. {
  14. title: 'My dashboard',
  15. panels: [
  16. {
  17. id: 1,
  18. type: 'graph',
  19. title: 'My graph',
  20. gridPos: { x: 0, y: 0, w: 24, h: 10 },
  21. },
  22. {
  23. id: 2,
  24. type: 'graph2',
  25. title: 'My graph2',
  26. gridPos: { x: 0, y: 10, w: 25, h: 10 },
  27. },
  28. {
  29. id: 3,
  30. type: 'graph3',
  31. title: 'My graph3',
  32. gridPos: { x: 0, y: 20, w: 25, h: 100 },
  33. },
  34. {
  35. id: 4,
  36. type: 'graph4',
  37. title: 'My graph4',
  38. gridPos: { x: 0, y: 120, w: 25, h: 10 },
  39. },
  40. ],
  41. },
  42. overrides
  43. );
  44. const meta = Object.assign({ canSave: true, canEdit: true }, metaOverrides);
  45. return new DashboardModel(data, meta);
  46. }
  47. function dashboardGridScenario(description: string, scenarioFn: (ctx: ScenarioContext) => void) {
  48. describe(description, () => {
  49. let setupFn: () => void;
  50. const ctx: ScenarioContext = {
  51. setup: fn => {
  52. setupFn = fn;
  53. },
  54. props: {
  55. isEditing: false,
  56. isFullscreen: false,
  57. scrollTop: null,
  58. dashboard: getTestDashboard(),
  59. },
  60. setProps: (props: Partial<Props>) => {
  61. Object.assign(ctx.props, props);
  62. if (ctx.wrapper) {
  63. ctx.wrapper.setProps(ctx.props);
  64. }
  65. },
  66. };
  67. beforeEach(() => {
  68. setupFn();
  69. ctx.wrapper = shallow(<DashboardGrid {...ctx.props} />);
  70. });
  71. scenarioFn(ctx);
  72. });
  73. }
  74. describe('DashboardGrid', () => {
  75. dashboardGridScenario('Can render dashboard grid', ctx => {
  76. ctx.setup(() => {});
  77. it('Should render', () => {
  78. expect(ctx.wrapper).toMatchSnapshot();
  79. });
  80. });
  81. });