grafana/public/app/features/explore/SecondaryActions.test.tsx
L-M-K-B a98fae32fc
Laura/chore/refactor test of secondary actions (#48745)
* Chore: transfer three tests to testing-library

* Chore: transfer last test to testing-library

* chore: add suggestion from code review

Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>

Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
2022-05-05 15:55:59 +02:00

78 lines
2.7 KiB
TypeScript

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { noop } from 'lodash';
import React from 'react';
import { SecondaryActions } from './SecondaryActions';
describe('SecondaryActions', () => {
it('should render component with three buttons', () => {
render(
<SecondaryActions
onClickAddQueryRowButton={noop}
onClickRichHistoryButton={noop}
onClickQueryInspectorButton={noop}
/>
);
expect(screen.getByRole('button', { name: /Add row button/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Rich history button/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument();
});
it('should not render add row button if addQueryRowButtonHidden=true', () => {
render(
<SecondaryActions
addQueryRowButtonHidden={true}
onClickAddQueryRowButton={noop}
onClickRichHistoryButton={noop}
onClickQueryInspectorButton={noop}
/>
);
expect(screen.queryByRole('button', { name: /Add row button/i })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: /Rich history button/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument();
});
it('should disable add row button if addQueryRowButtonDisabled=true', () => {
render(
<SecondaryActions
addQueryRowButtonDisabled={true}
onClickAddQueryRowButton={noop}
onClickRichHistoryButton={noop}
onClickQueryInspectorButton={noop}
/>
);
expect(screen.getByRole('button', { name: /Add row button/i })).toBeDisabled();
expect(screen.getByRole('button', { name: /Rich history button/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query inspector button/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(
<SecondaryActions
onClickAddQueryRowButton={onClickAddRow}
onClickRichHistoryButton={onClickHistory}
onClickQueryInspectorButton={onClickQueryInspector}
/>
);
await user.click(screen.getByRole('button', { name: /Add row button/i }));
expect(onClickAddRow).toBeCalledTimes(1);
await user.click(screen.getByRole('button', { name: /Rich history button/i }));
expect(onClickHistory).toBeCalledTimes(1);
await user.click(screen.getByRole('button', { name: /Query inspector button/i }));
expect(onClickQueryInspector).toBeCalledTimes(1);
});
});