The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/plugins/datasource/dashboard/DashboardQueryEditor.test.tsx

114 lines
3.2 KiB

import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { getDefaultTimeRange, LoadingState } from '@grafana/data';
import { SHARED_DASHBOARD_QUERY } from './types';
import { DashboardQueryEditor } from './DashboardQueryEditor';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { DashboardModel } from 'app/features/dashboard/state';
import { setDataSourceSrv } from '@grafana/runtime';
import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks';
jest.mock('app/core/config', () => ({
...(jest.requireActual('app/core/config') as unknown as object),
panels: {
timeseries: {
info: {
logos: {
small: '',
},
},
},
},
}));
setDataSourceSrv(
new MockDataSourceSrv({
test: mockDataSource({ isDefault: true }),
})
);
describe('DashboardQueryEditor', () => {
const mockOnChange = jest.fn();
const mockOnRunQueries = jest.fn();
const mockPanelData = {
state: LoadingState.Done,
series: [],
timeRange: getDefaultTimeRange(),
};
const mockQueries = [{ refId: 'A' }];
let mockDashboard: DashboardModel;
beforeEach(() => {
mockDashboard = new DashboardModel({
panels: [
{
targets: [],
type: 'timeseries',
id: 1,
title: 'My first panel',
},
{
targets: [],
id: 2,
type: 'timeseries',
title: 'Another panel',
},
{
datasource: {
uid: SHARED_DASHBOARD_QUERY,
},
targets: [],
id: 3,
type: 'timeseries',
title: 'A dashboard query panel',
},
],
});
jest.spyOn(getDashboardSrv(), 'getCurrent').mockImplementation(() => mockDashboard);
});
it('does not show a panel with the SHARED_DASHBOARD_QUERY datasource as an option in the dropdown', async () => {
render(
<DashboardQueryEditor
queries={mockQueries}
panelData={mockPanelData}
onChange={mockOnChange}
onRunQueries={mockOnRunQueries}
/>
);
const select = screen.getByText('Choose panel');
await userEvent.click(select);
const myFirstPanel = await screen.findByText('My first panel');
expect(myFirstPanel).toBeInTheDocument();
const anotherPanel = await screen.findByText('Another panel');
expect(anotherPanel).toBeInTheDocument();
expect(screen.queryByText('A dashboard query panel')).not.toBeInTheDocument();
});
it('does not show the current panelInEdit as an option in the dropdown', async () => {
mockDashboard.initEditPanel(mockDashboard.panels[0]);
render(
<DashboardQueryEditor
queries={mockQueries}
panelData={mockPanelData}
onChange={mockOnChange}
onRunQueries={mockOnRunQueries}
/>
);
const select = screen.getByText('Choose panel');
await userEvent.click(select);
expect(screen.queryByText('My first panel')).not.toBeInTheDocument();
const anotherPanel = await screen.findByText('Another panel');
expect(anotherPanel).toBeInTheDocument();
expect(screen.queryByText('A dashboard query panel')).not.toBeInTheDocument();
});
});