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/features/explore/SecondaryActions.test.tsx

88 lines
3.3 KiB

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { noop } from 'lodash';
import { QueriesDrawerContextProviderMock } from './QueriesDrawer/mocks';
import { SecondaryActions } from './SecondaryActions';
describe('SecondaryActions', () => {
it('should render component with three buttons', () => {
render(<SecondaryActions onClickAddQueryRowButton={noop} onClickQueryInspectorButton={noop} />);
expect(screen.getByRole('button', { name: /Add query/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query history/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query inspector/i })).toBeInTheDocument();
});
it('should not render hidden elements', () => {
render(
<QueriesDrawerContextProviderMock queryLibraryAvailable={false}>
<SecondaryActions
addQueryRowButtonHidden={true}
richHistoryRowButtonHidden={true}
onClickAddQueryRowButton={noop}
onClickQueryInspectorButton={noop}
/>
</QueriesDrawerContextProviderMock>
);
expect(screen.queryByRole('button', { name: /Add query/i })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: /Query history/i })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query inspector/i })).toBeInTheDocument();
});
it('should not render query history button when query library is available', () => {
render(
<QueriesDrawerContextProviderMock queryLibraryAvailable={true}>
<SecondaryActions
richHistoryRowButtonHidden={false}
onClickAddQueryRowButton={noop}
onClickQueryInspectorButton={noop}
/>
</QueriesDrawerContextProviderMock>
);
expect(screen.queryByRole('button', { name: /Query history/i })).not.toBeInTheDocument();
});
it('should disable add row button if addQueryRowButtonDisabled=true', () => {
render(
<SecondaryActions
addQueryRowButtonDisabled={true}
onClickAddQueryRowButton={noop}
onClickQueryInspectorButton={noop}
/>
);
expect(screen.getByRole('button', { name: /Add query/i })).toBeDisabled();
expect(screen.getByRole('button', { name: /Query history/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query inspector/i })).toBeInTheDocument();
});
it('should map click handlers correctly', async () => {
const user = userEvent.setup();
const onClickAddRow = jest.fn();
const onClickHistory = jest.fn();
const onClickQueryInspector = jest.fn();
render(
<QueriesDrawerContextProviderMock setDrawerOpened={onClickHistory}>
<SecondaryActions
onClickAddQueryRowButton={onClickAddRow}
onClickQueryInspectorButton={onClickQueryInspector}
/>
</QueriesDrawerContextProviderMock>
);
await user.click(screen.getByRole('button', { name: /Add query/i }));
expect(onClickAddRow).toBeCalledTimes(1);
await user.click(screen.getByRole('button', { name: /Query history/i }));
expect(onClickHistory).toBeCalledTimes(1);
expect(onClickHistory).toBeCalledWith(true);
await user.click(screen.getByRole('button', { name: /Query inspector/i }));
expect(onClickQueryInspector).toBeCalledTimes(1);
});
});