-
Notifications
You must be signed in to change notification settings - Fork 13k
fix: User admin GUI crash when rendering an invalid custom field type #37154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
085e484
fix: prevent CustomFieldForm from rendering invalid field types
aleksandernsilva d332c3e
refactor: adjusted label default
aleksandernsilva 152ff5e
a11y: improved form accessibility
aleksandernsilva 04d09d4
test: CustomFieldsForm unit tests
aleksandernsilva 18bb5b9
test: added e2e test case
aleksandernsilva fda72e9
chore: changeset
aleksandernsilva 77f158b
Merge branch 'develop' into fix/user-custom-fields
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@rocket.chat/meteor": patch | ||
| "@rocket.chat/ui-client": patch | ||
| --- | ||
|
|
||
| Fixes a GUI crash happening in the admin user page when attempting to display an invalid custom field |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/ui-client/src/components/CustomFieldsForm.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import type { CustomFieldMetadata } from '@rocket.chat/core-typings'; | ||
| import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
| import { render, screen, waitFor, within } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import type { Control } from 'react-hook-form'; | ||
| import { useForm } from 'react-hook-form'; | ||
|
|
||
| import { CustomFieldsForm } from './CustomFieldsForm'; | ||
|
|
||
| type TestComponentProps = { | ||
| metadata: CustomFieldMetadata[]; | ||
| formName: string; | ||
| onSubmit: (data: any) => void; | ||
| }; | ||
|
|
||
| const appRoot = mockAppRoot() | ||
| .withTranslations('en', 'core', { | ||
| Required_field: '{{field}} required', | ||
| }) | ||
| .build(); | ||
|
|
||
| const TestComponent = ({ metadata, formName, onSubmit }: TestComponentProps) => { | ||
| const { control, handleSubmit } = useForm({ mode: 'onBlur' }); | ||
|
|
||
| return ( | ||
| <form onSubmit={handleSubmit(onSubmit)}> | ||
| <CustomFieldsForm formName={formName} formControl={control as unknown as Control<any>} metadata={metadata} /> | ||
| <button type='submit'>Submit</button> | ||
| </form> | ||
| ); | ||
| }; | ||
|
|
||
| describe('CustomFieldsForm', () => { | ||
| it('should render all custom fields', () => { | ||
| const metadata: CustomFieldMetadata[] = [ | ||
| { name: 'field1', type: 'text', label: 'Field 1', required: true, defaultValue: '', options: [] }, | ||
| { | ||
| name: 'field2', | ||
| type: 'select', | ||
| label: 'Field 2', | ||
| required: false, | ||
| defaultValue: 'a', | ||
| options: [ | ||
| ['a', 'a'], | ||
| ['b', 'b'], | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Field 1' })).toBeInTheDocument(); | ||
| expect(within(screen.getByLabelText('Field 2')).getByRole('combobox', { hidden: true })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render a text input', () => { | ||
| const metadata: CustomFieldMetadata[] = [ | ||
| { name: 'field1', type: 'text', label: 'Field 1', required: true, defaultValue: '', options: [] }, | ||
| ]; | ||
|
|
||
| render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Field 1' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render a select input', () => { | ||
| const metadata: CustomFieldMetadata[] = [ | ||
| { | ||
| name: 'field2', | ||
| type: 'select', | ||
| label: 'Field 2', | ||
| required: false, | ||
| defaultValue: 'a', | ||
| options: [ | ||
| ['a', 'a'], | ||
| ['b', 'b'], | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); | ||
|
|
||
| expect(within(screen.getByLabelText('Field 2')).getByRole('combobox', { hidden: true })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should show required error message', async () => { | ||
| const metadata: CustomFieldMetadata[] = [ | ||
| { name: 'field1', type: 'text', label: 'Field 1', required: true, defaultValue: '', options: [] }, | ||
| ]; | ||
|
|
||
| render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />); | ||
|
|
||
| const input = screen.getByRole('textbox', { name: 'Field 1' }); | ||
| await userEvent.click(input); | ||
| await userEvent.tab(); | ||
|
|
||
| await waitFor(() => expect(input).toHaveAccessibleDescription('Field 1 required')); | ||
| }); | ||
|
|
||
| it('should show minLength error message', async () => { | ||
| const metadata: CustomFieldMetadata[] = [ | ||
| { name: 'field1', type: 'text', label: 'Field 1', required: true, minLength: 5, defaultValue: '', options: [] }, | ||
| ]; | ||
|
|
||
| render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); | ||
|
|
||
| const input = screen.getByRole('textbox', { name: 'Field 1' }); | ||
| await userEvent.type(input, '123'); | ||
| await userEvent.tab(); | ||
|
|
||
| await waitFor(() => expect(input).toHaveAccessibleDescription('Min_length_is')); | ||
| }); | ||
|
|
||
| it('should validate maxLength', async () => { | ||
| const metadata: CustomFieldMetadata[] = [ | ||
| { name: 'field1', type: 'text', label: 'Field 1', required: true, maxLength: 3, defaultValue: '', options: [] }, | ||
| ]; | ||
|
|
||
| render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }); | ||
|
|
||
| const input = screen.getByRole('textbox', { name: 'Field 1' }); | ||
| await userEvent.type(input, '123456'); | ||
| await userEvent.tab(); | ||
|
|
||
| expect(input).toHaveValue('123'); | ||
| }); | ||
|
|
||
| it('should not throw when attempting to render invalid field type', () => { | ||
| const metadata: CustomFieldMetadata[] = [ | ||
| { name: 'field1', type: 'invalid_type' as any, label: 'Field 1', required: true, defaultValue: '', options: [] }, | ||
| ]; | ||
|
|
||
| expect(() => | ||
| render(<TestComponent metadata={metadata} formName='testForm' onSubmit={jest.fn()} />, { wrapper: appRoot }), | ||
| ).not.toThrow(); | ||
|
|
||
| expect(screen.queryByLabelText('Field 1')).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.