Skip to content

Commit

Permalink
feat: miscellaneous test
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Tagliabue committed Jul 1, 2024
1 parent e22d579 commit e350684
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/__tests__/miscellaneous.ts
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()
})
})

0 comments on commit e350684

Please sign in to comment.