Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
clodal opened this issue Aug 7, 2018 · 6 comments

Comments

@clodal
Copy link

clodal commented Aug 7, 2018

  • 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!

@kentcdodds
Copy link
Member

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

Do you mean fireEvent.change?

@clodal
Copy link
Author

clodal commented Aug 11, 2018

@kentcdodds Ah yes sorry, let me update it. fireEvent.change

@rGiladi
Copy link

rGiladi commented Aug 11, 2018

Hey, can you upload your components too?
I'm currently using RTL with Formik on one of my project and never had that issue..

Also, did you try upgrading RTL to the latest version?

@clodal
Copy link
Author

clodal commented Aug 13, 2018

@RoyGil Hi there, thanks for checking this out, this is the issue for me:

// package.json
"react-testing-library": "^5.0.0"
"formik": "^1.0.2"
// ContactForm.spec.js
it('Should submit form', () => {
  const { container } = render(<ContactForm />)

  const form = container.querySelector('form[name="contact"]')
  const name = container.querySelector('[name="name"]')
  const email = container.querySelector('[name="email"]')
  const content = container.querySelector('[name="content"]')

  fireEvent.change(name, { target: { value: 'my name' } })
  fireEvent.change(email, { target: { value: 'myemail@email.com' } })
  fireEvent.change(content, { target: { value: 'my content is more than 10' } })

  fireEvent.submit(form) // <<< Error: Uncaught [ReferenceError: HTMLButtonElement is not defined]
})
// ContactForm.jsx
const ContactForm = ({ handleSubmit }) => {
  <form name='contact' onSubmit={handleSubmit}>
    <input type='text' name='name' />
    <input type='email' name='email' />
    <input type='text' name='content' />
    <button type='submit'>Submit</button>
  </form>
}

const formikOptions = {
  handleSubmit(values, bag) {
    // This block here never gets fired when running tests
    ...
  }
}

export default withFormik(formikOptions)(ContactForm)

@clodal
Copy link
Author

clodal commented Aug 13, 2018

I can't reproduce this issue in a simple environment (https://codesandbox.io/s/x9o9xk15oq) which indicates that this is probably not an issue with the library, will close this. Thanks for your patience!

@clodal clodal closed this as completed Aug 13, 2018
julienw pushed a commit to julienw/react-testing-library that referenced this issue Dec 20, 2018
<!--
Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated!

Please make sure that you are familiar with and follow the Code of Conduct for
this project (found in the CODE_OF_CONDUCT.md file).

Also, please make sure you're familiar with and follow the instructions in the
contributing guidelines (found in the CONTRIBUTING.md file).

If you're new to contributing to open source projects, you might find this free
video course helpful: http://kcd.im/pull-request

Please fill out the information below to expedite the review and (hopefully)
merge of your pull request!
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->

**What**: Vague documentation (issue testing-library#153 )

<!-- Why are these changes necessary? -->

**Why**: To clarify usage to users

<!-- How were these changes implemented? -->

**How**: Added documentation

<!-- Have you done all of these things?  -->

**Checklist**:

<!-- add "N/A" to the end of each line that's irrelevant to your changes -->

<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->

- [x] Documentation N/A
- [ ] Tests N/A
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions -->

<!-- feel free to add additional comments -->
@IsaaX
Copy link

IsaaX commented Jan 10, 2019

I also ran into this issue and for those who stumble upon this I'd like to give them a heads up.

So I was using mocha, assuming clodal was also using mocha due to that 'step' he is using in the example. He couldn't reproduce in the sandbox due to the environment being jest meaning it has jsdom built into it so formik ended up using the global HtmlButtonElement aka (window.HtmlButtonElement) correctly (as a browser would). So to solve that issue, just add it to your globals and formik should be able to submit a form.

const dom = new JSDOM('<!doctype html><html><body><div id="root"><div></body></html>');
....<your globals here>
global.HTMLButtonElement = dom.window.HTMLButtonElement;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants