Skip to content

Formik + React Testing Library => ReferenceError: HTMLButtonElement is not defined #154

Closed
@clodal

Description

@clodal
  • react-testing-library version: 3.0.2
  • formik version: 1.0.2
  • react version: 16.3.1
  • node version: 8.9.0
  • yarn version: 1.7.0

Relevant code or config:

// posts.spec.js
const find = (container, selector) => container.querySelector(selector)
const change = (el, val) => Simulate.change(el, val)
const submit = (el) => Simulate.submit(el)

step('Post editing form works', (done) => {
  // 1. Get elements
  const postForm = find(container, 'form[name="post"]')
  const contentField = find(postForm, '[name="content"]')
  const titleField = find(postForm, '[name="title"]')
  const submitBtn = find(postForm, 'button[type="submit"]')

  // 2. Change values
  change(titleField, { target: { name: 'title', value: 'Post title 33' } })
  change(contentField, { target: { name: 'content', value: 'Post content 33' } })

  // 3. Check values out
  console.log('titleField.value', titleField.value) // 'Post title 33' - OK
  console.log('contentField.value', contentField.value) // 'Post content 33' - OK
  console.log('submitBtn', submitBtn) // HTMLButtonElement { FiberNode: { ... } } - OK

  // 4. Trigger submit
  submit(postForm) // Error: Uncaught [ReferenceError: HTMLButtonElement is not defined]
})
// bash
Error: Uncaught [ReferenceError: HTMLButtonElement is not defined]
    ...
    at processImmediate [as _immediateCallback] (timers.js:722:5) ReferenceError: HTMLButtonElement is not defined
    at Formik._this.handleSubmit (/Users/joel/Sites/apollo-universal-starter-kit/node_modules/formik/src/Formik.tsx:401:34)
    at HTMLUnknownElement.callCallback (/Users/joel/Sites/apollo-universal-starter-kit/node_modules/react-dom/cjs/react-dom.development.js:100:14)
    ...

What you did:

Wrote unit tests for a post form

What happened:

Unit test fails with error related to html element not found

Problem description:

Even though RTL seems to have found the html element, it seems like the element is missing on submit.

It's weird because i am able to log the element out but the error keeps happening.

I have tried to upgrade to RTL v4, using fireEvent.simulate fireEvent.change to no avail as well.

Suggested solution:

Could this be related to issue here: https://spectrum.chat/thread/2062f0a2-f137-4c94-b911-4b98f4703876? Not sure

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions