import '@testing-library/jest-dom/extend-expect' import React, { ReactElement } from 'react' import { Formik } from 'formik' import { render, fireEvent, waitForDomChange, wait, waitFor, getAllByRole, } from '@testing-library/react' import FormItem from '.' import Input from '../input' import Form from '../form/form' import Select from '../select' import SubmitButton from '../submit-button' import { act } from 'react-dom/test-utils' const { Option } = Select const Test = ({ children, validate, }: { children: ReactElement validate?: () => object }) => { return ( {}} validate={validate}>
{children}
) } test('displays validation result', async () => { const validate = () => ({ test: 'error' }) const { getByTestId, queryByText } = render( , ) expect(queryByText('error')).not.toBeInTheDocument() fireEvent.change(getByTestId('input'), { target: { name: 'test', value: 'test' }, }) fireEvent.click(getByTestId('submit')) await waitFor(() => expect(queryByText('error')).toBeInTheDocument()) }) test('displayes initial error', async () => { const { queryByText } = render( {}} >
, ) expect(queryByText('initialError')).toBeInTheDocument() }) test('displayes error instead of initialError after touched when showInitialErrorAfterTouched is false', async () => { const validate = () => 'error' const { getByTestId, queryByText } = render( {}} >
, ) expect(queryByText('initialError')).toBeInTheDocument() expect(queryByText('error')).not.toBeInTheDocument() fireEvent.change(getByTestId('input'), { target: { name: 'test', value: 'test' }, }) fireEvent.blur(getByTestId('input')) fireEvent.click(getByTestId('submit')) await waitFor(() => { expect(queryByText('error')).toBeInTheDocument() expect(queryByText('initialError')).not.toBeInTheDocument() }) }) test('displayes initialError with error after touched when showInitialErrorAfterTouched is true', async () => { const validate = () => 'error' const { getByTestId, queryByText } = render( {}} >
, ) expect(queryByText('initialError')).toBeInTheDocument() expect(queryByText('error')).not.toBeInTheDocument() fireEvent.change(getByTestId('input'), { target: { name: 'test', value: 'test' }, }) fireEvent.blur(getByTestId('input')) fireEvent.click(getByTestId('submit')) await waitFor(() => { expect(queryByText('error')).toBeInTheDocument() expect(queryByText('initialError')).toBeInTheDocument() }) }) test('should not display help if no display is required', async () => { const validate = () => 'error' const { getByTestId } = render( {}}>
, ) const explainElement = getByTestId('input').parentElement!.nextSibling expect(explainElement).toBeNull() }) test('handles changes on multiselect without prop-types error', async () => { const { getByTestId, queryByText, baseElement } = render( , ) expect(queryByText('error')).not.toBeInTheDocument() console.error = jest.fn() const uat = getByTestId('input') await act(async () => { fireEvent.click(uat) }) // work around type error: cast to any await waitFor(() => getAllByRole(baseElement as any, 'option')) // work around type error: cast to any const one = getAllByRole(baseElement as any, 'option') fireEvent.click(one[0]) expect(console.error).not.toHaveBeenCalled() //@ts-ignore console.error.mockRestore() }) test('handles changes on multiselect with array field error', async () => { const validate = () => 'error' const { getByTestId, queryByText } = render( {}}>
, ) expect(queryByText('error')).not.toBeInTheDocument() fireEvent.click(getByTestId('submit')) await waitFor(() => expect(queryByText('error')).toBeInTheDocument()) }) test('displays validation result on nested input', async () => { const validate = () => 'error' const { getByTestId, queryByText } = render( {}}>
, ) expect(queryByText('error')).not.toBeInTheDocument() fireEvent.change(getByTestId('input'), { target: { name: 'test', value: 'test' }, }) fireEvent.blur(getByTestId('input')) fireEvent.click(getByTestId('submit')) await waitFor(() => expect(queryByText('error')).toBeInTheDocument()) }) test('displays validation success ', async () => { const validate = () => undefined const { getByTestId, queryByLabelText } = render( {}}>
, ) expect(queryByLabelText('check-circle')).not.toBeInTheDocument() await act(async () => { fireEvent.change(getByTestId('input'), { target: { name: 'test', value: 'test' }, }) fireEvent.blur(getByTestId('input')) }) expect(queryByLabelText('check-circle')).toBeInTheDocument() }) test('labels link to the input element', async () => { const { getByTestId, getByLabelText } = render( {}}>
, ) expect(getByLabelText('The label')).toEqual(getByTestId('input')) })