Skip to content

Commit

Permalink
feat: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Tagliabue committed Jun 28, 2024
1 parent 8e249b0 commit 1406345
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/__tests__/use-execute-callbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]'

Expand All @@ -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))
Expand All @@ -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)
})
})

0 comments on commit 1406345

Please sign in to comment.