2022-11-15 17:49:02 +01:00
|
|
|
/* eslint-disable jest/require-top-level-describe */
|
|
|
|
import React from 'react';
|
|
|
|
import { render } from '@testing-library/react';
|
|
|
|
import { renderControlledInput } from '../../../../test/lib/render-helpers';
|
|
|
|
import { TextFieldSearch } from './text-field-search';
|
|
|
|
|
|
|
|
describe('TextFieldSearch', () => {
|
|
|
|
it('should render correctly', () => {
|
2022-12-01 22:48:53 +01:00
|
|
|
const { getByRole, container } = render(<TextFieldSearch />);
|
2022-11-15 17:49:02 +01:00
|
|
|
expect(getByRole('searchbox')).toBeDefined();
|
2022-12-01 22:48:53 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
2022-11-15 17:49:02 +01:00
|
|
|
});
|
2022-12-01 22:48:53 +01:00
|
|
|
it('should render with a custom className', () => {
|
|
|
|
const fn = jest.fn();
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<TextFieldSearch
|
|
|
|
data-testid="test-search"
|
|
|
|
className="test-search"
|
|
|
|
clearButtonOnClick={fn}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('test-search')).toHaveClass('test-search');
|
|
|
|
});
|
|
|
|
it('should render showClearButton button when value exists', async () => {
|
|
|
|
const fn = jest.fn();
|
2022-11-15 17:49:02 +01:00
|
|
|
// As showClearButton is intended to be used with a controlled input we need to use renderControlledInput
|
|
|
|
const { user, getByRole } = renderControlledInput(TextFieldSearch, {
|
2022-12-01 22:48:53 +01:00
|
|
|
clearButtonOnClick: fn,
|
2022-11-15 17:49:02 +01:00
|
|
|
});
|
|
|
|
await user.type(getByRole('searchbox'), 'test value');
|
|
|
|
expect(getByRole('searchbox')).toHaveValue('test value');
|
2023-05-16 22:15:13 +02:00
|
|
|
expect(getByRole('button', { name: /clear/u })).toBeDefined();
|
2022-11-15 17:49:02 +01:00
|
|
|
});
|
2023-02-16 00:43:51 +01:00
|
|
|
it('should still render with the endAccessory if it exists', async () => {
|
2022-12-01 22:48:53 +01:00
|
|
|
const fn = jest.fn();
|
2022-11-15 17:49:02 +01:00
|
|
|
// As showClearButton is intended to be used with a controlled input we need to use renderControlledInput
|
|
|
|
const { user, getByRole, getByText } = renderControlledInput(
|
|
|
|
TextFieldSearch,
|
|
|
|
{
|
2022-12-01 22:48:53 +01:00
|
|
|
clearButtonOnClick: fn,
|
2023-02-16 00:43:51 +01:00
|
|
|
endAccessory: <div>end-accessory</div>,
|
2022-11-15 17:49:02 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
await user.type(getByRole('searchbox'), 'test value');
|
|
|
|
expect(getByRole('searchbox')).toHaveValue('test value');
|
2023-05-16 22:15:13 +02:00
|
|
|
expect(getByRole('button', { name: /clear/u })).toBeDefined();
|
2023-02-16 00:43:51 +01:00
|
|
|
expect(getByText('end-accessory')).toBeDefined();
|
2022-11-15 17:49:02 +01:00
|
|
|
});
|
|
|
|
it('should fire onClick event when passed to clearButtonOnClick when clear button is clicked', async () => {
|
|
|
|
// As showClearButton is intended to be used with a controlled input we need to use renderControlledInput
|
|
|
|
const fn = jest.fn();
|
|
|
|
const { user, getByRole } = renderControlledInput(TextFieldSearch, {
|
|
|
|
clearButtonOnClick: fn,
|
|
|
|
});
|
|
|
|
await user.type(getByRole('searchbox'), 'test value');
|
2023-05-16 22:15:13 +02:00
|
|
|
await user.click(getByRole('button', { name: /clear/u }));
|
2022-11-15 17:49:02 +01:00
|
|
|
expect(fn).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
it('should fire onClick event when passed to clearButtonProps.onClick prop', async () => {
|
|
|
|
// As showClearButton is intended to be used with a controlled input we need to use renderControlledInput
|
|
|
|
const fn = jest.fn();
|
|
|
|
const { user, getByRole } = renderControlledInput(TextFieldSearch, {
|
|
|
|
clearButtonProps: { onClick: fn },
|
2022-12-01 22:48:53 +01:00
|
|
|
clearButtonOnClick: fn,
|
2022-11-15 17:49:02 +01:00
|
|
|
});
|
|
|
|
await user.type(getByRole('searchbox'), 'test value');
|
2023-05-16 22:15:13 +02:00
|
|
|
await user.click(getByRole('button', { name: /clear/u }));
|
2022-11-15 17:49:02 +01:00
|
|
|
expect(fn).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|