Explore.test.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { Provider } from 'react-redux';
  4. import { AutoSizerProps } from 'react-virtualized-auto-sizer';
  5. import { DataSourceApi, LoadingState, CoreApp, createTheme } from '@grafana/data';
  6. import { configureStore } from 'app/store/configureStore';
  7. import { ExploreId } from 'app/types/explore';
  8. import { Explore, Props } from './Explore';
  9. import { scanStopAction } from './state/query';
  10. import { createEmptyQueryResponse } from './state/utils';
  11. const makeEmptyQueryResponse = (loadingState: LoadingState) => {
  12. const baseEmptyResponse = createEmptyQueryResponse();
  13. baseEmptyResponse.request = {
  14. requestId: '1',
  15. intervalMs: 0,
  16. interval: '1s',
  17. dashboardId: 0,
  18. panelId: 1,
  19. range: baseEmptyResponse.timeRange,
  20. scopedVars: {
  21. apps: {
  22. value: 'value',
  23. text: 'text',
  24. },
  25. },
  26. targets: [
  27. {
  28. refId: 'A',
  29. },
  30. ],
  31. timezone: 'UTC',
  32. app: CoreApp.Explore,
  33. startTime: 0,
  34. };
  35. baseEmptyResponse.state = loadingState;
  36. return baseEmptyResponse;
  37. };
  38. const dummyProps: Props = {
  39. logsResult: undefined,
  40. changeSize: jest.fn(),
  41. datasourceInstance: {
  42. meta: {
  43. metrics: true,
  44. logs: true,
  45. },
  46. components: {
  47. QueryEditorHelp: {},
  48. },
  49. } as DataSourceApi,
  50. datasourceMissing: false,
  51. exploreId: ExploreId.left,
  52. loading: false,
  53. modifyQueries: jest.fn(),
  54. scanStart: jest.fn(),
  55. scanStopAction: scanStopAction,
  56. setQueries: jest.fn(),
  57. queryKeys: [],
  58. isLive: false,
  59. syncedTimes: false,
  60. updateTimeRange: jest.fn(),
  61. makeAbsoluteTime: jest.fn(),
  62. graphResult: [],
  63. absoluteRange: {
  64. from: 0,
  65. to: 0,
  66. },
  67. timeZone: 'UTC',
  68. queryResponse: makeEmptyQueryResponse(LoadingState.NotStarted),
  69. addQueryRow: jest.fn(),
  70. theme: createTheme(),
  71. showMetrics: true,
  72. showLogs: true,
  73. showTable: true,
  74. showTrace: true,
  75. showNodeGraph: true,
  76. splitOpen: (() => {}) as any,
  77. logsVolumeData: undefined,
  78. loadLogsVolumeData: () => {},
  79. changeGraphStyle: () => {},
  80. graphStyle: 'lines',
  81. };
  82. jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => {
  83. return {
  84. getDataSourceSrv: () => ({
  85. get: () => Promise.resolve({}),
  86. getList: () => [],
  87. getInstanceSettings: () => {},
  88. }),
  89. };
  90. });
  91. // for the AutoSizer component to have a width
  92. jest.mock('react-virtualized-auto-sizer', () => {
  93. return ({ children }: AutoSizerProps) => children({ height: 1, width: 1 });
  94. });
  95. const setup = (overrideProps?: Partial<Props>) => {
  96. const store = configureStore();
  97. const exploreProps = { ...dummyProps, ...overrideProps };
  98. return render(
  99. <Provider store={store}>
  100. <Explore {...exploreProps} />
  101. </Provider>
  102. );
  103. };
  104. describe('Explore', () => {
  105. it('should not render no data with not started loading state', () => {
  106. setup();
  107. expect(screen.queryByTestId('explore-no-data')).not.toBeInTheDocument();
  108. });
  109. it('should render no data with done loading state', async () => {
  110. const queryResp = makeEmptyQueryResponse(LoadingState.Done);
  111. setup({ queryResponse: queryResp });
  112. expect(screen.getByTestId('explore-no-data')).toBeInTheDocument();
  113. });
  114. });