DashboardQueryEditor.test.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { getDefaultTimeRange, LoadingState } from '@grafana/data';
  5. import { setDataSourceSrv } from '@grafana/runtime';
  6. import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks';
  7. import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
  8. import { DashboardModel } from 'app/features/dashboard/state';
  9. import { DashboardQueryEditor } from './DashboardQueryEditor';
  10. import { SHARED_DASHBOARD_QUERY } from './types';
  11. jest.mock('app/core/config', () => ({
  12. ...(jest.requireActual('app/core/config') as unknown as object),
  13. panels: {
  14. timeseries: {
  15. info: {
  16. logos: {
  17. small: '',
  18. },
  19. },
  20. },
  21. },
  22. }));
  23. setDataSourceSrv(
  24. new MockDataSourceSrv({
  25. test: mockDataSource({ isDefault: true }),
  26. })
  27. );
  28. describe('DashboardQueryEditor', () => {
  29. const mockOnChange = jest.fn();
  30. const mockOnRunQueries = jest.fn();
  31. const mockPanelData = {
  32. state: LoadingState.Done,
  33. series: [],
  34. timeRange: getDefaultTimeRange(),
  35. };
  36. const mockQueries = [{ refId: 'A' }];
  37. let mockDashboard: DashboardModel;
  38. beforeEach(() => {
  39. mockDashboard = new DashboardModel({
  40. panels: [
  41. {
  42. targets: [],
  43. type: 'timeseries',
  44. id: 1,
  45. title: 'My first panel',
  46. },
  47. {
  48. targets: [],
  49. id: 2,
  50. type: 'timeseries',
  51. title: 'Another panel',
  52. },
  53. {
  54. datasource: {
  55. uid: SHARED_DASHBOARD_QUERY,
  56. },
  57. targets: [],
  58. id: 3,
  59. type: 'timeseries',
  60. title: 'A dashboard query panel',
  61. },
  62. ],
  63. });
  64. jest.spyOn(getDashboardSrv(), 'getCurrent').mockImplementation(() => mockDashboard);
  65. });
  66. it('does not show a panel with the SHARED_DASHBOARD_QUERY datasource as an option in the dropdown', async () => {
  67. render(
  68. <DashboardQueryEditor
  69. queries={mockQueries}
  70. panelData={mockPanelData}
  71. onChange={mockOnChange}
  72. onRunQueries={mockOnRunQueries}
  73. />
  74. );
  75. const select = screen.getByText('Choose panel');
  76. await userEvent.click(select);
  77. const myFirstPanel = await screen.findByText('My first panel');
  78. expect(myFirstPanel).toBeInTheDocument();
  79. const anotherPanel = await screen.findByText('Another panel');
  80. expect(anotherPanel).toBeInTheDocument();
  81. expect(screen.queryByText('A dashboard query panel')).not.toBeInTheDocument();
  82. });
  83. it('does not show the current panelInEdit as an option in the dropdown', async () => {
  84. mockDashboard.initEditPanel(mockDashboard.panels[0]);
  85. render(
  86. <DashboardQueryEditor
  87. queries={mockQueries}
  88. panelData={mockPanelData}
  89. onChange={mockOnChange}
  90. onRunQueries={mockOnRunQueries}
  91. />
  92. );
  93. const select = screen.getByText('Choose panel');
  94. await userEvent.click(select);
  95. expect(screen.queryByText('My first panel')).not.toBeInTheDocument();
  96. const anotherPanel = await screen.findByText('Another panel');
  97. expect(anotherPanel).toBeInTheDocument();
  98. expect(screen.queryByText('A dashboard query panel')).not.toBeInTheDocument();
  99. });
  100. });