PanelChrome.test.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { act, render, screen } from '@testing-library/react';
  2. import React, { FC } from 'react';
  3. import { Provider } from 'react-redux';
  4. import configureMockStore from 'redux-mock-store';
  5. import { ReplaySubject } from 'rxjs';
  6. import { EventBusSrv, getDefaultTimeRange, LoadingState, PanelData, PanelPlugin, PanelProps } from '@grafana/data';
  7. import { PanelQueryRunner } from '../../query/state/PanelQueryRunner';
  8. import { setTimeSrv, TimeSrv } from '../services/TimeSrv';
  9. import { DashboardModel, PanelModel } from '../state';
  10. import { PanelChrome, Props } from './PanelChrome';
  11. jest.mock('app/core/profiler', () => ({
  12. profiler: {
  13. renderingCompleted: jest.fn(),
  14. },
  15. }));
  16. function setupTestContext(options: Partial<Props>) {
  17. const mockStore = configureMockStore<any, any>();
  18. const store = mockStore({ dashboard: { panels: [] } });
  19. const subject: ReplaySubject<PanelData> = new ReplaySubject<PanelData>();
  20. const panelQueryRunner = {
  21. getData: () => subject,
  22. run: () => {
  23. subject.next({ state: LoadingState.Done, series: [], timeRange: getDefaultTimeRange() });
  24. },
  25. } as unknown as PanelQueryRunner;
  26. const timeSrv = {
  27. timeRange: jest.fn(),
  28. } as unknown as TimeSrv;
  29. setTimeSrv(timeSrv);
  30. const defaults: Props = {
  31. panel: new PanelModel({
  32. id: 123,
  33. hasTitle: jest.fn(),
  34. replaceVariables: jest.fn(),
  35. events: new EventBusSrv(),
  36. getQueryRunner: () => panelQueryRunner,
  37. getOptions: jest.fn(),
  38. getDisplayTitle: jest.fn(),
  39. }),
  40. dashboard: {
  41. panelInitialized: jest.fn(),
  42. getTimezone: () => 'browser',
  43. events: new EventBusSrv(),
  44. } as unknown as DashboardModel,
  45. plugin: {
  46. meta: { skipDataQuery: false },
  47. panel: TestPanelComponent,
  48. } as unknown as PanelPlugin,
  49. isViewing: true,
  50. isEditing: false,
  51. isInView: false,
  52. width: 100,
  53. height: 100,
  54. onInstanceStateChange: () => {},
  55. };
  56. const props = { ...defaults, ...options };
  57. const { rerender } = render(
  58. <Provider store={store}>
  59. <PanelChrome {...props} />
  60. </Provider>
  61. );
  62. return { rerender, props, subject, store };
  63. }
  64. describe('PanelChrome', () => {
  65. describe('when the user scrolls by a panel so fast that it starts loading data but scrolls out of view', () => {
  66. it('then it should load the panel successfully when scrolled into view again', () => {
  67. const { rerender, props, subject, store } = setupTestContext({});
  68. expect(screen.queryByText(/plugin panel to render/i)).not.toBeInTheDocument();
  69. act(() => {
  70. subject.next({ state: LoadingState.Loading, series: [], timeRange: getDefaultTimeRange() });
  71. subject.next({ state: LoadingState.Done, series: [], timeRange: getDefaultTimeRange() });
  72. });
  73. const newProps = { ...props, isInView: true };
  74. rerender(
  75. <Provider store={store}>
  76. <PanelChrome {...newProps} />
  77. </Provider>
  78. );
  79. expect(screen.getByText(/plugin panel to render/i)).toBeInTheDocument();
  80. });
  81. });
  82. });
  83. const TestPanelComponent: FC<PanelProps> = () => <div>Plugin Panel to Render</div>;