Skip to content

Latest commit

 

History

History
82 lines (69 loc) · 1.97 KB

example-formik.md

File metadata and controls

82 lines (69 loc) · 1.97 KB
id title
example-react-formik
Formik

Example based in this Async Submission Formik example

// myForm.js
import React from 'react'
import {Formik, Field, Form} from 'formik'

const sleep = ms => new Promise(r => setTimeout(r, ms))

export const MyForm = ({onSubmit}) => {
  const handleSubmit = async values => {
    await sleep(500)
    onSubmit(values)
  }

  return (
    <div>
      <h1>Sign Up</h1>
      <Formik
        initialValues={{
          firstName: '',
          lastName: '',
          email: '',
        }}
        onSubmit={handleSubmit}
      >
        <Form>
          <label htmlFor="firstName">First Name</label>
          <Field id="firstName" name="firstName" placeholder="Jane" />

          <label htmlFor="lastName">Last Name</label>
          <Field id="lastName" name="lastName" placeholder="Doe" />

          <label htmlFor="email">Email</label>
          <Field
            id="email"
            name="email"
            placeholder="[email protected]"
            type="email"
          />
          <button type="submit">Submit</button>
        </Form>
      </Formik>
    </div>
  )
}
// myForm.test.js
import React from 'react'
import {render, screen, waitFor} from '@testing-library/react'
import userEvent from '@testing-library/user-event'

import {MyForm} from './myForm.js'

test('rendering and submitting a basic Formik form', async () => {
  const handleSubmit = jest.fn()
  render(<MyForm onSubmit={handleSubmit} />)
  const user = userEvent.setup()

  await user.type(screen.getByLabelText(/first name/i), 'John')
  await user.type(screen.getByLabelText(/last name/i), 'Dee')
  await user.type(screen.getByLabelText(/email/i), '[email protected]')

  await user.click(screen.getByRole('button', {name: /submit/i}))

  await waitFor(() =>
    expect(handleSubmit).toHaveBeenCalledWith({
      email: '[email protected]',
      firstName: 'John',
      lastName: 'Dee',
    }),
  )
})