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>
This commit is contained in:
@@ -218,9 +218,6 @@ exports[`no enzyme tests`] = {
|
||||
"public/app/features/explore/RichHistory/RichHistoryStarredTab.test.tsx:3933225580": [
|
||||
[0, 17, 13, "RegExp match", "2409514259"]
|
||||
],
|
||||
"public/app/features/explore/SecondaryActions.test.tsx:1177396128": [
|
||||
[0, 19, 13, "RegExp match", "2409514259"]
|
||||
],
|
||||
"public/app/features/folders/FolderSettingsPage.test.tsx:1109052730": [
|
||||
[0, 19, 13, "RegExp match", "2409514259"]
|
||||
],
|
||||
|
||||
@@ -1,28 +1,27 @@
|
||||
import { shallow } from 'enzyme';
|
||||
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';
|
||||
|
||||
const addQueryRowButtonSelector = '[aria-label="Add row button"]';
|
||||
const richHistoryButtonSelector = '[aria-label="Rich history button"]';
|
||||
const queryInspectorButtonSelector = '[aria-label="Query inspector button"]';
|
||||
|
||||
describe('SecondaryActions', () => {
|
||||
it('should render component two buttons', () => {
|
||||
const wrapper = shallow(
|
||||
it('should render component with three buttons', () => {
|
||||
render(
|
||||
<SecondaryActions
|
||||
onClickAddQueryRowButton={noop}
|
||||
onClickRichHistoryButton={noop}
|
||||
onClickQueryInspectorButton={noop}
|
||||
/>
|
||||
);
|
||||
expect(wrapper.find(addQueryRowButtonSelector)).toHaveLength(1);
|
||||
expect(wrapper.find(richHistoryButtonSelector)).toHaveLength(1);
|
||||
|
||||
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', () => {
|
||||
const wrapper = shallow(
|
||||
render(
|
||||
<SecondaryActions
|
||||
addQueryRowButtonHidden={true}
|
||||
onClickAddQueryRowButton={noop}
|
||||
@@ -30,12 +29,14 @@ describe('SecondaryActions', () => {
|
||||
onClickQueryInspectorButton={noop}
|
||||
/>
|
||||
);
|
||||
expect(wrapper.find(addQueryRowButtonSelector)).toHaveLength(0);
|
||||
expect(wrapper.find(richHistoryButtonSelector)).toHaveLength(1);
|
||||
|
||||
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', () => {
|
||||
const wrapper = shallow(
|
||||
render(
|
||||
<SecondaryActions
|
||||
addQueryRowButtonDisabled={true}
|
||||
onClickAddQueryRowButton={noop}
|
||||
@@ -43,14 +44,20 @@ describe('SecondaryActions', () => {
|
||||
onClickQueryInspectorButton={noop}
|
||||
/>
|
||||
);
|
||||
expect(wrapper.find(addQueryRowButtonSelector).props().disabled).toBe(true);
|
||||
|
||||
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', () => {
|
||||
it('should map click handlers correctly', async () => {
|
||||
const user = userEvent.setup();
|
||||
|
||||
const onClickAddRow = jest.fn();
|
||||
const onClickHistory = jest.fn();
|
||||
const onClickQueryInspector = jest.fn();
|
||||
const wrapper = shallow(
|
||||
|
||||
render(
|
||||
<SecondaryActions
|
||||
onClickAddQueryRowButton={onClickAddRow}
|
||||
onClickRichHistoryButton={onClickHistory}
|
||||
@@ -58,13 +65,13 @@ describe('SecondaryActions', () => {
|
||||
/>
|
||||
);
|
||||
|
||||
wrapper.find(addQueryRowButtonSelector).simulate('click');
|
||||
expect(onClickAddRow).toBeCalled();
|
||||
await user.click(screen.getByRole('button', { name: /Add row button/i }));
|
||||
expect(onClickAddRow).toBeCalledTimes(1);
|
||||
|
||||
wrapper.find(richHistoryButtonSelector).simulate('click');
|
||||
expect(onClickHistory).toBeCalled();
|
||||
await user.click(screen.getByRole('button', { name: /Rich history button/i }));
|
||||
expect(onClickHistory).toBeCalledTimes(1);
|
||||
|
||||
wrapper.find(queryInspectorButtonSelector).simulate('click');
|
||||
expect(onClickQueryInspector).toBeCalled();
|
||||
await user.click(screen.getByRole('button', { name: /Query inspector button/i }));
|
||||
expect(onClickQueryInspector).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user