Skip to content

Conversation

@alexwendte
Copy link
Collaborator

@alexwendte alexwendte commented Oct 10, 2018

What:
waitForDomChange is implemented along with tests, documentation, and types.
waitForElement is changed to require a callback function

Why:
Resolves issue number #111

How:
I copied much of what was in waitForElement. They are very similar

Checklist:

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

Copy link
Collaborator

@sompylasar sompylasar left a comment

Choose a reason for hiding this comment

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

LGTM overall, questions inside.

README.md Outdated
- [`waitForDomChange`](#waitfordomchange)
- [`fireEvent`](#fireevent)
- [`fireEvent[eventName]`](#fireeventeventname)
- [`getNodeText`](#getnodetext)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should the indentation be less on this line? getNodeText looks independent of fireEvent.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, let's remove one of the # from getNodeText 👍 Do you mind doing that @themostcolm?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Got it.

await skipSomeTimeForMutationObserver()

expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledWith(true)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is it called with true?

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, that's a good question. Why don't we have the success handler be called with the mutationsList which the mutation observer will give our callback?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This would be a good use case of toMatchSnapshot right? I was just copying the other ones from waitForElement, but this one would make sense to have I believe.

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer toMatchInlineSnapshot. If it's too big for that, then it's probably too big in general and we need a more specific assertion.

}
timer = setTimeout(onTimeout, timeout)
observer = new window.MutationObserver(onMutation)
observer.observe(container, mutationObserverOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this MutationObserver code be reused somehow, not copied, between waitForElement and waitForDomChange?

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer if we could reuse the code 👍

Copy link
Collaborator

Choose a reason for hiding this comment

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

waitForElement could probably wrap around waitForDomChange...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How do you propose we reuse the code?

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.

Thanks! A few comments/questions/requests :)

README.md Outdated
- [`waitForDomChange`](#waitfordomchange)
- [`fireEvent`](#fireevent)
- [`fireEvent[eventName]`](#fireeventeventname)
- [`getNodeText`](#getnodetext)
Copy link
Member

Choose a reason for hiding this comment

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

Yep, let's remove one of the # from getNodeText 👍 Do you mind doing that @themostcolm?

README.md Outdated
| [<img src="https://avatars2.githubusercontent.com/u/21689428?v=4" width="100px;"/><br /><sub><b>Jonathan Stoye</b></sub>](http://jonathanstoye.de)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4126644?v=4" width="100px;"/><br /><sub><b>Sanghyeon Lee</b></sub>](https://github.com/yongdamsh)<br />[💡](#example-yongdamsh "Examples") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba </b></sub>](https://github.com/Dajust)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Documentation") [🤔](#ideas-Dajust "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/340761?v=4" width="100px;"/><br /><sub><b>Wayne Crouch</b></sub>](https://github.com/wgcrouch)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [<img src="https://avatars1.githubusercontent.com/u/4996462?v=4" width="100px;"/><br /><sub><b>Ben Elliott</b></sub>](http://benjaminelliott.co.uk)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [<img src="https://avatars3.githubusercontent.com/u/577921?v=4" width="100px;"/><br /><sub><b>Ruben Costa</b></sub>](http://nuances.co)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") | [<img src="https://avatars2.githubusercontent.com/u/4982001?v=4" width="100px;"/><br /><sub><b>Robert Smith</b></sub>](http://rbrtsmith.com/)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Arbrtsmith "Bug reports") [🤔](#ideas-rbrtsmith "Ideas, Planning, & Feedback") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=rbrtsmith "Documentation") |
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [<img src="https://avatars1.githubusercontent.com/u/186971?v=4" width="100px;"/><br /><sub><b>Neil Kistner</b></sub>](https://neilkistner.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [<img src="https://avatars3.githubusercontent.com/u/1448597?v=4" width="100px;"/><br /><sub><b>Ben Chauvette</b></sub>](http://bdchauvette.net/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [<img src="https://avatars2.githubusercontent.com/u/777527?v=4" width="100px;"/><br /><sub><b>Jeff Baumgardt</b></sub>](https://github.com/JeffBaumgardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/4658208?v=4" width="100px;"/><br /><sub><b>Matan Kushner</b></sub>](http://matchai.me)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Documentation") [🤔](#ideas-matchai "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Tests") |

| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
Copy link
Member

Choose a reason for hiding this comment

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

I think your editor messed some stuff up here... Could you make sure that the precommit hooks run so prettier will automatically format this properly?

You could also run npx kcd-scripts format before committing to be sure.

Copy link
Member

Choose a reason for hiding this comment

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

Don't forget to run:

npx kcd-scripts contributors generate
npx kcd-scripts format

:)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Darn it. I ran it before and then when I went back in to change the README again I ran format and precommit but not generate.

expect(errorHandler).toHaveBeenCalledTimes(0)

document.body.appendChild(document.createElement('div'))
expect(document.body).toMatchSnapshot()
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the snapshots are necessary.

expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledWith(true)
expect(errorHandler).toHaveBeenCalledTimes(0)
expect(document.body).toMatchSnapshot()
Copy link
Member

Choose a reason for hiding this comment

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

This one isn't necessary either.

expect(errorHandler).toHaveBeenCalledTimes(0)
expect(document.body).toMatchSnapshot()

return promise
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to return this promise. Please remove this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(because async functions always return promises and the return value of this one isn't used)

expect(errorHandler).toHaveBeenCalledTimes(0)
expect(container).toMatchSnapshot()

return promise
Copy link
Member

Choose a reason for hiding this comment

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

Remove this

await skipSomeTimeForMutationObserver()

expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledWith(true)
Copy link
Member

Choose a reason for hiding this comment

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

Agreed, that's a good question. Why don't we have the success handler be called with the mutationsList which the mutation observer will give our callback?

expect(successHandler).toHaveBeenCalledWith(true)
expect(errorHandler).toHaveBeenCalledTimes(0)

return promise
Copy link
Member

Choose a reason for hiding this comment

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

Remove this

}
timer = setTimeout(onTimeout, timeout)
observer = new window.MutationObserver(onMutation)
observer.observe(container, mutationObserverOptions)
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer if we could reuse the code 👍

} = {}) {
return new Promise((resolve, reject) => {
// Disabling eslint prefer-const below: either prefer-const or no-use-before-define triggers.
let lastError, observer, timer // eslint-disable-line prefer-const
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure that if you remove observer and timer from here and use a const declaration below you should be fine :)

@alexwendte
Copy link
Collaborator Author

alexwendte commented Oct 10, 2018 via email

@kentcdodds
Copy link
Member

You may need to run npx kcd-scripts contributors generate first to get the table fixed up, and then run npx kcd-scripts format 👍

@alexkrolick
Copy link
Collaborator

alexkrolick commented Oct 10, 2018

It would be nice to see some tests around this in react-testing-library. Do all mutation events fire at the same time in a React render or could there be race conditions if you are expecting a mutation deeper in the tree?

Copy link
Collaborator Author

@alexwendte alexwendte left a comment

Choose a reason for hiding this comment

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

I believe I implemented all of the requested features expect for making the mutation observer shared between waitForElement and waitForDomChange

…ectly formats README.md. Changes Promise to return a mutationsList. Adds documentation for mutationsList.
@alexwendte alexwendte force-pushed the pr/wait-for-dom-change branch from 079bffb to cdb4633 Compare October 10, 2018 17:15
kentcdodds
kentcdodds previously approved these changes Oct 10, 2018
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.

Ok, I'm pretty happy with this. Anyone else want to weigh in?

@gnapse
Copy link
Member

gnapse commented Oct 10, 2018

This does not look to be taking care of removing this functionality from waitForElement (and hence the problematic callback-less waitForElement form). Just checking. If it's going to be taken care of in a separate PR, that's fine too.

@kentcdodds
Copy link
Member

Good point @gnapse.

@themostcolm, could you please do that as well. It's technically breaking a use case that we never expected/recommended so I'm not going to make it a major version bump.

@alexwendte
Copy link
Collaborator Author

alexwendte commented Oct 10, 2018

Absolutely!

I didn't do it before because I thought it would be a breaking change, but what you said makes sense @kentcdodds

kentcdodds
kentcdodds previously approved these changes Oct 10, 2018
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.

I think this looks great other than the README formatting thing. What do you all think?

…hanges tests to account for new behavior. removes the documentation around the default callback. adds MutationObserver documentation for waitForDomChange
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.

Perfect 👍

@alexwendte
Copy link
Collaborator Author

Would you like me to update the branch and then squash everything into one commit, or do you would prefer separate commits?

@kentcdodds kentcdodds merged commit 4cf93d2 into testing-library:master Oct 11, 2018
@kentcdodds
Copy link
Member

Nope, we're good. Thanks @alexwendte!

@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 :)

@alexwendte
Copy link
Collaborator Author

alexwendte commented Oct 11, 2018 via email

@kentcdodds
Copy link
Member

🎉 This PR is included in version 3.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

5 participants