-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luca Tagliabue
committed
Jul 1, 2024
1 parent
e22d579
commit e350684
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
import { act, renderHook } from '@testing-library/react' | ||
import useFormbit from 'src/use-formbit' | ||
import * as Yup from 'yup' | ||
|
||
describe('Executing many different operations', () => { | ||
it('Should lead to a correct result', () => { | ||
// SETUP | ||
const initialValues = { } | ||
const schema = Yup.object({ | ||
firstName: Yup.string().required(), | ||
lastName: Yup.string(), | ||
age: Yup.number().min(18) | ||
}) | ||
|
||
const { result, unmount } = renderHook(() => useFormbit({ initialValues, yup: schema })) | ||
|
||
const successCallback = jest.fn() | ||
const errorCallback = jest.fn() | ||
|
||
// EXECUTON | ||
act(() => result.current.writeAll([ | ||
['firstName', 'John'], | ||
['lastName', 'Doe'], | ||
['age', 2] | ||
])) | ||
act(() => result.current.submitForm(successCallback, errorCallback)) | ||
|
||
act(() => result.current.removeAll(['firstName', 'lastName', 'age'])) | ||
act(() => result.current.write('firstName', 'John')) | ||
act(() => result.current.write('lastName', 'Doe')) | ||
act(() => result.current.write('age', 18)) | ||
act(() => result.current.submitForm(successCallback, errorCallback)) | ||
|
||
// VERIFY | ||
expect(result.current.form).toStrictEqual({ firstName: 'John', lastName: 'Doe', age: 18 }) | ||
expect(successCallback).toHaveBeenCalledTimes(1) | ||
expect(errorCallback).toHaveBeenCalledTimes(1) | ||
|
||
// TEARDOWN | ||
unmount() | ||
}) | ||
}) |