DashboardGrid.test.tsx 2.4 KB

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