This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 376
refactor: add useForm hook #3159
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9357fae
add react-hooks testing lib
a-b-r-o-w-n 9fa85ec
add useForm hook
a-b-r-o-w-n fe209c0
use useForm in a couple of forms
a-b-r-o-w-n 3f44419
Merge branch 'master' into abrown/chore/use-form-hook
a-b-r-o-w-n 5c8d7ef
Merge branch 'master' into abrown/chore/use-form-hook
a-b-r-o-w-n 637e742
Merge branch 'master' into abrown/chore/use-form-hook
a-b-r-o-w-n 520d75e
remove comment
a-b-r-o-w-n a7e30f9
Merge branch 'master' into abrown/chore/use-form-hook
a-b-r-o-w-n 0b37480
Merge branch 'master' into abrown/chore/use-form-hook
a-b-r-o-w-n 40012e5
Merge branch 'master' into abrown/chore/use-form-hook
a-b-r-o-w-n 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
140 changes: 140 additions & 0 deletions
140
Composer/packages/client/__tests__/hooks/useForm.test.ts
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,140 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { hooks } from '@bfc/test-utils'; | ||
|
|
||
| import { useForm, FieldConfig } from '../../src/hooks/useForm'; | ||
|
|
||
| interface TestFormData { | ||
| requiredField: string; | ||
| customValidationField: string; | ||
| asyncValidationField: string; | ||
| } | ||
|
|
||
| afterEach(hooks.cleanup); | ||
|
|
||
| describe('useForm', () => { | ||
| const custValidate = jest.fn(); | ||
| const asyncValidate = jest.fn(); | ||
|
|
||
| const fields: FieldConfig<TestFormData> = { | ||
| requiredField: { | ||
| defaultValue: 'foo', | ||
| required: true, | ||
| }, | ||
| customValidationField: { | ||
| defaultValue: 'bar', | ||
| validate: custValidate, | ||
| }, | ||
| asyncValidationField: { | ||
| defaultValue: 'baz', | ||
| validate: asyncValidate, | ||
| }, | ||
| }; | ||
|
|
||
| describe('formData', () => { | ||
| it('applies default values', () => { | ||
| const { result } = hooks.renderHook(() => useForm(fields)); | ||
|
|
||
| expect(result.current.formData).toEqual({ | ||
| requiredField: 'foo', | ||
| customValidationField: 'bar', | ||
| asyncValidationField: 'baz', | ||
| }); | ||
| }); | ||
|
|
||
| it('can update single fields', async () => { | ||
| const { result, waitForNextUpdate } = hooks.renderHook(() => useForm(fields)); | ||
|
|
||
| await hooks.act(async () => { | ||
| result.current.updateField('requiredField', 'new value'); | ||
| await waitForNextUpdate(); | ||
| }); | ||
|
|
||
| expect(result.current.formData.requiredField).toEqual('new value'); | ||
| }); | ||
|
|
||
| it('can update the whole object', async () => { | ||
| const { result, waitForNextUpdate } = hooks.renderHook(() => useForm(fields)); | ||
|
|
||
| await hooks.act(async () => { | ||
| result.current.updateForm({ | ||
| requiredField: 'new', | ||
| customValidationField: 'form', | ||
| asyncValidationField: 'data', | ||
| }); | ||
| await waitForNextUpdate(); | ||
| }); | ||
|
|
||
| expect(result.current.formData).toEqual({ | ||
| requiredField: 'new', | ||
| customValidationField: 'form', | ||
| asyncValidationField: 'data', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formErrors', () => { | ||
| it('can validate when mounting', async () => { | ||
| custValidate.mockReturnValue('custom'); | ||
| asyncValidate.mockResolvedValue('async'); | ||
| const { result, waitForNextUpdate } = hooks.renderHook(() => useForm(fields, { validateOnMount: true })); | ||
| await waitForNextUpdate(); | ||
|
|
||
| expect(result.current.formErrors).toMatchObject({ | ||
| customValidationField: 'custom', | ||
| asyncValidationField: 'async', | ||
| }); | ||
| }); | ||
|
|
||
| it('validates required fields', async () => { | ||
| const { result, waitForNextUpdate } = hooks.renderHook(() => useForm(fields)); | ||
|
|
||
| await hooks.act(async () => { | ||
| result.current.updateField('requiredField', ''); | ||
| await waitForNextUpdate(); | ||
| }); | ||
|
|
||
| expect(result.current.formErrors.requiredField).toEqual('requiredField is required'); | ||
| }); | ||
|
|
||
| it('validates using a custom validator', async () => { | ||
| custValidate.mockReturnValue('my custom validation'); | ||
| const { result, waitForNextUpdate } = hooks.renderHook(() => useForm(fields)); | ||
|
|
||
| await hooks.act(async () => { | ||
| result.current.updateField('customValidationField', 'foo'); | ||
| await waitForNextUpdate(); | ||
| }); | ||
|
|
||
| expect(result.current.formErrors.customValidationField).toEqual('my custom validation'); | ||
| }); | ||
|
|
||
| it('validates using an asyn validator', async () => { | ||
| asyncValidate.mockResolvedValue('my async validation'); | ||
| const { result, waitForNextUpdate } = hooks.renderHook(() => useForm(fields)); | ||
|
|
||
| await hooks.act(async () => { | ||
| result.current.updateField('asyncValidationField', 'foo'); | ||
| await waitForNextUpdate(); | ||
| }); | ||
|
|
||
| expect(result.current.formErrors.asyncValidationField).toEqual('my async validation'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('hasErrors', () => { | ||
| it('returns true when there are errors', async () => { | ||
| const { result, waitForNextUpdate } = hooks.renderHook(() => useForm(fields)); | ||
|
|
||
| expect(result.current.hasErrors).toBe(false); | ||
|
|
||
| await hooks.act(async () => { | ||
| result.current.updateField('requiredField', ''); | ||
| await waitForNextUpdate(); | ||
| }); | ||
|
|
||
| expect(result.current.hasErrors).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
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
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,4 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| export * from './useForm'; |
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.