-
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
Jun 28, 2024
1 parent
8e249b0
commit 1406345
Showing
1 changed file
with
22 additions
and
14 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 |
---|---|---|
|
@@ -2,11 +2,6 @@ import { act, renderHook } from '@testing-library/react' | |
import useFormbit from 'src/use-formbit' | ||
import * as yup from 'yup' | ||
|
||
const initialValues = { | ||
name: '', | ||
email: '' | ||
} | ||
|
||
const name = 'John Doe' | ||
const email = '[email protected]' | ||
|
||
|
@@ -17,6 +12,8 @@ const schema = yup.object().shape({ | |
|
||
describe('useExecuteCallbacks', () => { | ||
it('write can be used inside successCallbacks', () => { | ||
const initialValues = { name: '', email: '' } | ||
|
||
const { result } = renderHook(() => useFormbit({ initialValues, yup: schema })) | ||
|
||
act(() => result.current.write('name', name)) | ||
|
@@ -27,27 +24,38 @@ describe('useExecuteCallbacks', () => { | |
expect(result.current.form.name).toBe('after-success-callback') | ||
}) | ||
|
||
it('Subsequent calls with different successCallbacks are executed', () => { | ||
it('Subsequent calls of write and writeAll with different successCallbacks are executed correctly', () => { | ||
const initialValues = { name: '', email: '' } | ||
|
||
const { result } = renderHook(() => useFormbit({ initialValues, yup: schema })) | ||
|
||
act(() => result.current.write('name', name)) | ||
act(() => result.current.write('email', email)) | ||
|
||
act(() => result.current.validateForm(() => result.current.write('name', 'first-validation'))) | ||
act(() => result.current.validateForm(() => result.current.write('email', 'second-validation'))) | ||
act(() => result.current.validateForm(() => result.current.write('name', 'Al'))) | ||
act(() => result.current.validateForm(() => result.current.writeAll([ | ||
['name', 'John'], | ||
['email', '[email protected]'] | ||
]))) | ||
|
||
expect(result.current.form).toStrictEqual({ name: 'first-validation', email: 'second-validation' }) | ||
expect(result.current.form).toStrictEqual({ name: 'John', email: '[email protected]' }) | ||
}) | ||
|
||
it('successCallbacks are called only once', () => { | ||
it('successCallbacks and errorCallbacks are called correctly', () => { | ||
const initialValues = { name: '', email: '' } | ||
|
||
const successCallback = jest.fn() | ||
const errorCallback = jest.fn() | ||
|
||
const { result } = renderHook(() => useFormbit({ initialValues, yup: schema })) | ||
|
||
act(() => result.current.write('name', name)) | ||
act(() => result.current.write('email', email)) | ||
act(() => result.current.write('name', name, { successCallback, errorCallback })) | ||
act(() => result.current.validateForm(successCallback, errorCallback)) | ||
|
||
act(() => result.current.validateForm(successCallback)) | ||
act(() => result.current.write('email', email, { successCallback, errorCallback })) | ||
act(() => result.current.validateForm(successCallback, errorCallback)) | ||
|
||
expect(successCallback).toHaveBeenCalledTimes(1) | ||
expect(successCallback).toHaveBeenCalledTimes(3) | ||
expect(errorCallback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |