mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
* refactor: modify interfaces to make tooltip or aria-label required * refactor: change functionality around aria-label and tooltip * refactor: change and add information in storybook documentation * refactor: remove default from tooltip * refactor: IconButton to make tooltip or aria-label required * refactor: Fix tests * refactor: Fix tests * refactor: Fix tests * refactor: Fix tests * feat: add migration guide for breaking change * feat: add latest requirements to storybook docs * refactor: separate iconbutton story with and without tooltip * refactor: remove exported baseArgs * refactor: clean up and restructure original story * refactor: adjust styling * refactor: enable control for tooltip * refactor: clean up * refactor: enable control for aria-label * refactor: fix theme getting the wrong theme * refactor: fix tests * refactor: adjust story * refactor: remove confusing story * refactor: adjust controls for stories
154 lines
4.5 KiB
TypeScript
154 lines
4.5 KiB
TypeScript
import { fireEvent, queryByLabelText, render, screen } from '@testing-library/react';
|
|
import React from 'react';
|
|
|
|
import { type DataQuery } from '@grafana/schema';
|
|
import { mockDataSource } from 'app/features/alerting/unified/mocks';
|
|
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
|
|
import createMockPanelData from 'app/plugins/datasource/azuremonitor/__mocks__/panelData';
|
|
|
|
import { QueryEditorRows, Props } from './QueryEditorRows';
|
|
|
|
const mockDS = mockDataSource({
|
|
name: 'CloudManager',
|
|
type: DataSourceType.Alertmanager,
|
|
});
|
|
|
|
const mockVariable = mockDataSource({
|
|
name: '${dsVariable}',
|
|
type: 'datasource',
|
|
});
|
|
|
|
jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => {
|
|
return {
|
|
getDataSourceSrv: () => ({
|
|
get: () => Promise.resolve({ ...mockDS, getRef: () => {} }),
|
|
getList: ({ variables }: { variables: boolean }) => (variables ? [mockDS, mockVariable] : [mockDS]),
|
|
getInstanceSettings: () => ({
|
|
...mockDS,
|
|
meta: {
|
|
...mockDS.meta,
|
|
alerting: true,
|
|
mixed: true,
|
|
},
|
|
}),
|
|
}),
|
|
};
|
|
});
|
|
|
|
const props: Props = {
|
|
queries: [
|
|
{
|
|
datasource: mockDS,
|
|
refId: 'A',
|
|
},
|
|
{
|
|
datasource: mockDS,
|
|
refId: 'B',
|
|
},
|
|
],
|
|
dsSettings: mockDataSource(),
|
|
onQueriesChange: function (queries: DataQuery[]): void {
|
|
throw new Error('Function not implemented.');
|
|
},
|
|
onAddQuery: function (query: DataQuery): void {
|
|
throw new Error('Function not implemented.');
|
|
},
|
|
onRunQueries: function (): void {
|
|
throw new Error('Function not implemented.');
|
|
},
|
|
data: createMockPanelData(),
|
|
};
|
|
|
|
jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => {
|
|
return {
|
|
getDataSourceSrv: () => ({
|
|
get: () => Promise.resolve(mockDS),
|
|
getList: ({ variables }: { variables: boolean }) => (variables ? [mockDS, mockVariable] : [mockDS]),
|
|
getInstanceSettings: () => mockDS,
|
|
}),
|
|
};
|
|
});
|
|
|
|
describe('QueryEditorRows', () => {
|
|
it('Should render queries', async () => {
|
|
const {
|
|
renderResult: { rerender },
|
|
} = renderScenario();
|
|
expect((await screen.findByTestId('query-editor-rows')).children.length).toBe(2);
|
|
|
|
rerender(
|
|
<QueryEditorRows
|
|
{...props}
|
|
queries={[
|
|
{
|
|
datasource: mockDS,
|
|
refId: 'A',
|
|
},
|
|
]}
|
|
/>
|
|
);
|
|
|
|
expect((await screen.findByTestId('query-editor-rows')).children.length).toBe(1);
|
|
});
|
|
|
|
it('Should be able to expand and collapse queries', async () => {
|
|
renderScenario();
|
|
const queryEditorRows = await screen.findAllByTestId('query-editor-row');
|
|
|
|
for (const childQuery of queryEditorRows) {
|
|
const toggleExpandButton = queryByLabelText(childQuery, 'Collapse query row') as HTMLElement;
|
|
|
|
expect(toggleExpandButton).toBeInTheDocument();
|
|
expect(toggleExpandButton.getAttribute('aria-expanded')).toBe('true');
|
|
|
|
fireEvent.click(toggleExpandButton);
|
|
|
|
expect(toggleExpandButton.getAttribute('aria-expanded')).toBe('false');
|
|
}
|
|
});
|
|
|
|
it('Should be able to duplicate queries', async () => {
|
|
const onAddQuery = jest.fn();
|
|
const onQueryCopied = jest.fn();
|
|
|
|
renderScenario({ onAddQuery, onQueryCopied });
|
|
const queryEditorRows = await screen.findAllByTestId('query-editor-row');
|
|
queryEditorRows.map(async (childQuery) => {
|
|
const duplicateQueryButton = queryByLabelText(childQuery, 'Duplicate query') as HTMLElement;
|
|
|
|
expect(duplicateQueryButton).toBeInTheDocument();
|
|
|
|
fireEvent.click(duplicateQueryButton);
|
|
});
|
|
|
|
expect(onAddQuery).toHaveBeenCalledTimes(queryEditorRows.length);
|
|
expect(onQueryCopied).toHaveBeenCalledTimes(queryEditorRows.length);
|
|
});
|
|
|
|
it('Should be able to delete queries', async () => {
|
|
const onQueriesChange = jest.fn();
|
|
const onQueryRemoved = jest.fn();
|
|
renderScenario({ onQueriesChange, onQueryRemoved });
|
|
|
|
const queryEditorRows = await screen.findAllByTestId('query-editor-row');
|
|
queryEditorRows.map(async (childQuery) => {
|
|
const deleteQueryButton = queryByLabelText(childQuery, 'Remove query') as HTMLElement;
|
|
|
|
expect(deleteQueryButton).toBeInTheDocument();
|
|
|
|
fireEvent.click(deleteQueryButton);
|
|
});
|
|
|
|
expect(onQueriesChange).toHaveBeenCalledTimes(queryEditorRows.length);
|
|
expect(onQueryRemoved).toHaveBeenCalledTimes(queryEditorRows.length);
|
|
});
|
|
});
|
|
|
|
function renderScenario(overrides?: Partial<Props>) {
|
|
Object.assign(props, overrides);
|
|
|
|
return {
|
|
renderResult: render(<QueryEditorRows {...props} />),
|
|
};
|
|
}
|