|
| 1 | +import type { CustomFieldMetadata } from '@rocket.chat/core-typings'; |
| 2 | +import { mockAppRoot } from '@rocket.chat/mock-providers'; |
| 3 | +import { render, screen, waitFor, within } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import type { Control } from 'react-hook-form'; |
| 6 | +import { useForm } from 'react-hook-form'; |
| 7 | + |
| 8 | +import { CustomFieldsForm } from './CustomFieldsForm'; |
| 9 | + |
| 10 | +type TestComponentProps = { |
| 11 | + metadata: CustomFieldMetadata[]; |
| 12 | + formName: string; |
| 13 | + onSubmit: (data: any) => void; |
| 14 | +}; |
| 15 | + |
| 16 | +const appRoot = mockAppRoot() |
| 17 | + .withTranslations('en', 'core', { |
| 18 | + Required_field: '{{field}} required', |
| 19 | + }) |
| 20 | + .build(); |
| 21 | + |
| 22 | +const TestComponent = ({ metadata, formName, onSubmit }: TestComponentProps) => { |
| 23 | + const { control, handleSubmit } = useForm({ mode: 'onBlur' }); |
| 24 | + |
| 25 | + return ( |
| 26 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 27 | + <CustomFieldsForm formName={formName} formControl={control as unknown as Control<any>} metadata={metadata} /> |
| 28 | + <button type='submit'>Submit</button> |
| 29 | + </form> |
| 30 | + ); |
| 31 | +}; |
| 32 | + |
| 33 | +describe('CustomFieldsForm', () => { |
| 34 | + it('should render all custom fields', () => { |
| 35 | + const metadata: CustomFieldMetadata[] = [ |
| 36 | + { name: 'field1', type: 'text', label: 'Field 1', required: true, defaultValue: '', options: [] }, |
| 37 | + { |
| 38 | + name: 'field2', |
| 39 | + type: 'select', |
| 40 | + label: 'Field 2', |
| 41 | + required: false, |
| 42 | + defaultValue: 'a', |
| 43 | + options: [ |
| 44 | + ['a', 'a'], |
| 45 | + ['b', 'b'], |
| 46 | + ], |
| 47 | + }, |
| 48 | + ]; |
| 49 | + |
| 50 | + render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />); |
| 51 | + |
| 52 | + expect(screen.getByRole('textbox', { name: 'Field 1' })).toBeInTheDocument(); |
| 53 | + expect(within(screen.getByLabelText('Field 2')).getByRole('combobox', { hidden: true })).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should render a text input', () => { |
| 57 | + const metadata: CustomFieldMetadata[] = [ |
| 58 | + { name: 'field1', type: 'text', label: 'Field 1', required: true, defaultValue: '', options: [] }, |
| 59 | + ]; |
| 60 | + |
| 61 | + render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); |
| 62 | + |
| 63 | + expect(screen.getByRole('textbox', { name: 'Field 1' })).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should render a select input', () => { |
| 67 | + const metadata: CustomFieldMetadata[] = [ |
| 68 | + { |
| 69 | + name: 'field2', |
| 70 | + type: 'select', |
| 71 | + label: 'Field 2', |
| 72 | + required: false, |
| 73 | + defaultValue: 'a', |
| 74 | + options: [ |
| 75 | + ['a', 'a'], |
| 76 | + ['b', 'b'], |
| 77 | + ], |
| 78 | + }, |
| 79 | + ]; |
| 80 | + |
| 81 | + render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); |
| 82 | + |
| 83 | + expect(within(screen.getByLabelText('Field 2')).getByRole('combobox', { hidden: true })).toBeInTheDocument(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should show required error message', async () => { |
| 87 | + const metadata: CustomFieldMetadata[] = [ |
| 88 | + { name: 'field1', type: 'text', label: 'Field 1', required: true, defaultValue: '', options: [] }, |
| 89 | + ]; |
| 90 | + |
| 91 | + render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />); |
| 92 | + |
| 93 | + const input = screen.getByRole('textbox', { name: 'Field 1' }); |
| 94 | + await userEvent.click(input); |
| 95 | + await userEvent.tab(); |
| 96 | + |
| 97 | + await waitFor(() => expect(input).toHaveAccessibleDescription('Field 1 required')); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should show minLength error message', async () => { |
| 101 | + const metadata: CustomFieldMetadata[] = [ |
| 102 | + { name: 'field1', type: 'text', label: 'Field 1', required: true, minLength: 5, defaultValue: '', options: [] }, |
| 103 | + ]; |
| 104 | + |
| 105 | + render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); |
| 106 | + |
| 107 | + const input = screen.getByRole('textbox', { name: 'Field 1' }); |
| 108 | + await userEvent.type(input, '123'); |
| 109 | + await userEvent.tab(); |
| 110 | + |
| 111 | + await waitFor(() => expect(input).toHaveAccessibleDescription('Min_length_is')); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should validate maxLength', async () => { |
| 115 | + const metadata: CustomFieldMetadata[] = [ |
| 116 | + { name: 'field1', type: 'text', label: 'Field 1', required: true, maxLength: 3, defaultValue: '', options: [] }, |
| 117 | + ]; |
| 118 | + |
| 119 | + render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); |
| 120 | + |
| 121 | + const input = screen.getByRole('textbox', { name: 'Field 1' }); |
| 122 | + await userEvent.type(input, '123456'); |
| 123 | + await userEvent.tab(); |
| 124 | + |
| 125 | + expect(input).toHaveValue('123'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should not throw when attempting to render invalid field type', () => { |
| 129 | + const metadata: CustomFieldMetadata[] = [ |
| 130 | + { name: 'field1', type: 'invalid_type' as any, label: 'Field 1', required: true, defaultValue: '', options: [] }, |
| 131 | + ]; |
| 132 | + |
| 133 | + expect(() => |
| 134 | + render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }), |
| 135 | + ).not.toThrow(); |
| 136 | + |
| 137 | + expect(screen.queryByLabelText('Field 1')).not.toBeInTheDocument(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments