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

added fireEvent util #13

Merged
merged 4 commits into from
Apr 10, 2018
Merged

Conversation

jomaxx
Copy link
Collaborator

@jomaxx jomaxx commented Apr 9, 2018

What: added fireEvent utility

Why: #5

How: src/events.js

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

@jomaxx jomaxx force-pushed the jomaxx/fireEvent branch from e8e34aa to 6c6332f Compare April 9, 2018 21:19
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! This is so great!

Looks like this is missing some coverage

If you could open coverage/lcov-report/index.html locally and see what we're missing then add some tests to cover those that'd be super. Thank you!

describe('Clipboard Events', () => {
;['copy', 'paste'].forEach(eventName => {
it(`fires ${eventName}`, () => {
const node = document.createElement('input')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is very similar throughout the test file. Could we extract it to a "testEvent" function?

function testEvent({eventName, elementType}) {
  const node = document.createElement(elementType)
  const spy = jest.fn()
  node.addEventListener(eventName.toLowerCase(), spy)
  fireEvent[eventName](node)
  expect(spy).toHaveBeenCalledTimes(1)
}

Then most of the tests could be dramatically simplified to something like:

describe('Clipboard Events', () => {
  ;['copy', 'paste'].forEach(
    eventName => {
      it(`fires ${eventName}`, () => {
        testEvent({eventName, elementType: 'input'})
      })
    },
  )
})

Alternatively it may even not be a bad idea to abstract it further:

const eventTypes = [
  {type: 'Clipboard', events: ['copy', 'paste'], elementType: 'input'},
  {
    type: 'Composition',
    events: ['compositionEnd', 'compositionStart', 'compositionUpdate'],
    elementType: 'input',
  },
  // etc...
]

eventTypes.forEach(({type, events, elementType}) => {
  describe(`${type} Events`, () => {
    events.forEach(eventName => {
      it(`fires ${eventName}`, () => {
        const node = document.createElement(elementType)
        const spy = jest.fn()
        node.addEventListener(eventName.toLowerCase(), spy)
        fireEvent[eventName](node)
        expect(spy).toHaveBeenCalledTimes(1)
      })
    })
  })
})

I think doing this will make it easier to add cases and know which tests are for one-off situations 👍 It will also make this file shorter probably :)

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, meant that review to be "Request changes" 😅

@@ -0,0 +1,13 @@
import ReactDOM from 'react-dom'

const node = document.body.appendChild(document.createElement('div'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this okay to be done in the module scope rather than in a function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll move the document.body.appendChild call to mount

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also add the mirroring removeChild to unmount then?

src/events.js Outdated
@@ -0,0 +1,352 @@
// fallback to Event
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const Event = global.Event missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should probably do:

const g = typeof window === 'undefined' ? global : window

And then reference g instead. That way this runs in the browser properly as well.

})
})

describe('React Events', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be we need to have these test cases in react-testing-library? I'm just wondering because, anyway people are gonna use dom-testing-library for several frameworks (angular,preact,vue etc), do we have to add those frameworks testing here in future too?

Copy link
Contributor

@antsmartian antsmartian Apr 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also by moving it to react-testing-library, I guess people have good chance of looking at these test cases and understanding how to test events with react. Want to see, what others think..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also moving it to react-testing-library will move the respective package dependencies that don't make sense in just the dom-testing-library

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, also what we have created in src/__tests__/helpers/react.js is already part of react-testing-library (render,unmount).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think it'd be better if we could avoid react in here and have a simple render method that creates DOM nodes manually. It doesn't need to be complicated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, i can open a PR to react-testing-library as well. there are a couple events we'll need to fallback to triggering synthetic events for (change, select, mouseenter, mouseleave) so I'll add that logic there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. Thanks a bunch @jomaxx. This is so great!

@sompylasar
Copy link
Collaborator

there are a couple events we'll need to fallback to triggering synthetic events for (change, select, mouseenter, mouseleave) so I'll add that logic there

@jomaxx Could you please clarify about synthetic events? How does Cypress handles these events from within the browser?

@jomaxx
Copy link
Collaborator Author

jomaxx commented Apr 10, 2018

@sompylasar let's move discussion about synthetic events to testing-library/react-testing-library#48

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! Thanks so much for adding this!

@kentcdodds kentcdodds merged commit d45b449 into testing-library:master Apr 10, 2018
@kentcdodds
Copy link
Member

Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the other/MAINTAINING.md and CONTRIBUTING.md files (specifically the bit about the commit messages and the git hooks) and familiarize yourself with the code of conduct (we're using the contributor covenant). You might also want to watch the repo to be notified when someone files an issue/PR. Please continue to make PRs as you feel the need (you can make your branches directly on the repo rather than your fork if you want). Thanks! And welcome to the team :)

@kentcdodds
Copy link
Member

🎉 This PR is included in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

alexkrolick pushed a commit to alexkrolick/dom-testing-library that referenced this pull request Sep 13, 2018
* Improving API's for testing.

* Improving Apis

* Adding all contributions

* Fixing wrong url

* Fixing review comments & making colorful assertions :)

* Removing unwanted changes

* Fixing review comments

* removing unwanted comments

* Adding test cases for the coverage

* removing commented code and making few changes to the contribution file

* Updating the readme

* Making line break changes

* Update README.md
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

Successfully merging this pull request may close these issues.

4 participants