|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import * as ReactHookForm from 'react-hook-form'; |
| 4 | +import { modulesCopy } from '../../../../../assets'; |
| 5 | +import { |
| 6 | + type IProposalActionsDecoderBooleanFieldProps, |
| 7 | + ProposalActionsDecoderBooleanField, |
| 8 | +} from './proposalActionsDecoderBooleanField'; |
| 9 | + |
| 10 | +jest.mock('react-hook-form', () => ({ |
| 11 | + ...jest.requireActual<typeof ReactHookForm>('react-hook-form'), |
| 12 | + __esModule: true, |
| 13 | +})); |
| 14 | + |
| 15 | +describe('<ProposalActionsDecoderBooleanField /> component', () => { |
| 16 | + const useControllerSpy = jest.spyOn(ReactHookForm, 'useController'); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + useControllerSpy.mockReturnValue({ |
| 20 | + fieldState: { error: undefined }, |
| 21 | + field: { value: undefined, onBlur: jest.fn(), onChange: jest.fn(), ref: jest.fn() }, |
| 22 | + } as unknown as ReactHookForm.UseControllerReturn); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + useControllerSpy.mockReset(); |
| 27 | + }); |
| 28 | + |
| 29 | + const createTestComponent = (props?: Partial<IProposalActionsDecoderBooleanFieldProps>) => { |
| 30 | + const completeProps: IProposalActionsDecoderBooleanFieldProps = { |
| 31 | + parameter: { name: 'boolParam', type: 'bool', value: undefined }, |
| 32 | + fieldName: 'testName', |
| 33 | + ...props, |
| 34 | + }; |
| 35 | + |
| 36 | + return <ProposalActionsDecoderBooleanField {...completeProps} />; |
| 37 | + }; |
| 38 | + |
| 39 | + it('registers the field on the form context and renders a radio group with no default selection', () => { |
| 40 | + const fieldName = 'parameter.0.value'; |
| 41 | + render(createTestComponent({ fieldName })); |
| 42 | + |
| 43 | + expect(useControllerSpy).toHaveBeenCalledWith({ |
| 44 | + name: fieldName, |
| 45 | + rules: { validate: expect.any(Function) as unknown }, |
| 46 | + }); |
| 47 | + |
| 48 | + const radios = screen.getAllByRole('radio'); |
| 49 | + expect(radios).toHaveLength(2); |
| 50 | + radios.forEach((radio) => expect(radio).not.toBeChecked()); |
| 51 | + expect(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanTrue })).toBeInTheDocument(); |
| 52 | + expect( |
| 53 | + screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanFalse }), |
| 54 | + ).toBeInTheDocument(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('prefixes the field name with the formPrefix property when set', () => { |
| 58 | + const fieldName = 'value'; |
| 59 | + const formPrefix = 'actions.0'; |
| 60 | + render(createTestComponent({ fieldName, formPrefix })); |
| 61 | + expect(useControllerSpy).toHaveBeenCalledWith(expect.objectContaining({ name: `${formPrefix}.${fieldName}` })); |
| 62 | + }); |
| 63 | + |
| 64 | + it('renders the parameter name, type and notice as field labels', () => { |
| 65 | + const parameter = { name: 'tryExecute', notice: 'description', type: 'bool', value: undefined }; |
| 66 | + render(createTestComponent({ parameter })); |
| 67 | + expect(screen.getByText(`(${parameter.type})`)).toBeInTheDocument(); |
| 68 | + expect(screen.getByText(parameter.notice)).toBeInTheDocument(); |
| 69 | + // eslint-disable-next-line testing-library/no-node-access |
| 70 | + expect(screen.getByText(`(${parameter.type})`).parentElement).toHaveTextContent( |
| 71 | + `${parameter.name} (${parameter.type})`, |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('does not render the field labels when hideLabels property is set to true', () => { |
| 76 | + const parameter = { name: 'tryExecute', notice: 'description', type: 'bool', value: undefined }; |
| 77 | + render(createTestComponent({ parameter, hideLabels: true })); |
| 78 | + expect(screen.queryByText(`(${parameter.type})`)).not.toBeInTheDocument(); |
| 79 | + expect(screen.queryByText(parameter.notice)).not.toBeInTheDocument(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('checks the radio item matching the current field value', () => { |
| 83 | + useControllerSpy.mockReturnValue({ |
| 84 | + fieldState: { error: undefined }, |
| 85 | + field: { value: false, onChange: jest.fn() }, |
| 86 | + } as unknown as ReactHookForm.UseControllerReturn); |
| 87 | + render(createTestComponent()); |
| 88 | + expect(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanFalse })).toBeChecked(); |
| 89 | + expect(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanTrue })).not.toBeChecked(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('triggers the onChange callback with a boolean value on radio selection', async () => { |
| 93 | + const onChange = jest.fn(); |
| 94 | + useControllerSpy.mockReturnValue({ |
| 95 | + fieldState: { error: undefined }, |
| 96 | + field: { value: undefined, onBlur: jest.fn(), onChange, ref: jest.fn() }, |
| 97 | + } as unknown as ReactHookForm.UseControllerReturn); |
| 98 | + render(createTestComponent()); |
| 99 | + await userEvent.click(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanTrue })); |
| 100 | + expect(onChange).toHaveBeenCalledWith(true); |
| 101 | + }); |
| 102 | + |
| 103 | + it('passes form blur and ref handlers to the radio group', () => { |
| 104 | + const onBlur = jest.fn(); |
| 105 | + const ref = jest.fn(); |
| 106 | + useControllerSpy.mockReturnValue({ |
| 107 | + fieldState: { error: undefined }, |
| 108 | + field: { value: undefined, onBlur, onChange: jest.fn(), ref }, |
| 109 | + } as unknown as ReactHookForm.UseControllerReturn); |
| 110 | + render(createTestComponent()); |
| 111 | + const radioGroup = screen.getByRole('radiogroup'); |
| 112 | + expect(ref).toHaveBeenCalledWith(radioGroup); |
| 113 | + |
| 114 | + radioGroup.focus(); |
| 115 | + radioGroup.blur(); |
| 116 | + expect(onBlur).toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('renders an alert when the form field has an error', () => { |
| 120 | + const error = { message: 'invalid field' }; |
| 121 | + useControllerSpy.mockReturnValue({ |
| 122 | + fieldState: { error }, |
| 123 | + field: { value: undefined, onBlur: jest.fn(), onChange: jest.fn(), ref: jest.fn() }, |
| 124 | + } as unknown as ReactHookForm.UseControllerReturn); |
| 125 | + render(createTestComponent()); |
| 126 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 127 | + expect(screen.getByText(error.message)).toBeInTheDocument(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments