-
Notifications
You must be signed in to change notification settings - Fork 468
waitForDomChange Implementation #118
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
waitForDomChange Implementation #118
Conversation
sompylasar
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.
src/__tests__/wait-for-dom-change.js
Outdated
| await skipSomeTimeForMutationObserver() | ||
|
|
||
| expect(successHandler).toHaveBeenCalledTimes(1) | ||
| expect(successHandler).toHaveBeenCalledWith(true) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/wait-for-dom-change.js
Outdated
| } | ||
| timer = setTimeout(onTimeout, timeout) | ||
| observer = new window.MutationObserver(onMutation) | ||
| observer.observe(container, mutationObserverOptions) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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?
kentcdodds
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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") | |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
:)
There was a problem hiding this comment.
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.
src/__tests__/wait-for-dom-change.js
Outdated
| expect(errorHandler).toHaveBeenCalledTimes(0) | ||
|
|
||
| document.body.appendChild(document.createElement('div')) | ||
| expect(document.body).toMatchSnapshot() |
There was a problem hiding this comment.
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.
src/__tests__/wait-for-dom-change.js
Outdated
| expect(successHandler).toHaveBeenCalledTimes(1) | ||
| expect(successHandler).toHaveBeenCalledWith(true) | ||
| expect(errorHandler).toHaveBeenCalledTimes(0) | ||
| expect(document.body).toMatchSnapshot() |
There was a problem hiding this comment.
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.
src/__tests__/wait-for-dom-change.js
Outdated
| expect(errorHandler).toHaveBeenCalledTimes(0) | ||
| expect(document.body).toMatchSnapshot() | ||
|
|
||
| return promise |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
src/__tests__/wait-for-dom-change.js
Outdated
| expect(errorHandler).toHaveBeenCalledTimes(0) | ||
| expect(container).toMatchSnapshot() | ||
|
|
||
| return promise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this
src/__tests__/wait-for-dom-change.js
Outdated
| await skipSomeTimeForMutationObserver() | ||
|
|
||
| expect(successHandler).toHaveBeenCalledTimes(1) | ||
| expect(successHandler).toHaveBeenCalledWith(true) |
There was a problem hiding this comment.
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?
src/__tests__/wait-for-dom-change.js
Outdated
| expect(successHandler).toHaveBeenCalledWith(true) | ||
| expect(errorHandler).toHaveBeenCalledTimes(0) | ||
|
|
||
| return promise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this
src/wait-for-dom-change.js
Outdated
| } | ||
| timer = setTimeout(onTimeout, timeout) | ||
| observer = new window.MutationObserver(onMutation) | ||
| observer.observe(container, mutationObserverOptions) |
There was a problem hiding this comment.
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 👍
src/wait-for-dom-change.js
Outdated
| } = {}) { | ||
| 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 |
There was a problem hiding this comment.
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 :)
|
Yep, I’m happy to do these things! This is a great learning experience for me! I look forward to revisit everything and seeing what is conducive to good tests and nicely written code.
Per the formatting. I installed doctoc and thought that would format everything in the read me correctly. I will use kcd-scripts format now.
|
|
You may need to run |
|
It would be nice to see some tests around this in |
alexwendte
left a comment
There was a problem hiding this 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.
079bffb to
cdb4633
Compare
kentcdodds
left a comment
There was a problem hiding this 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?
|
This does not look to be taking care of removing this functionality from |
|
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. |
|
Absolutely! I didn't do it before because I thought it would be a breaking change, but what you said makes sense @kentcdodds |
kentcdodds
left a comment
There was a problem hiding this 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
f972da8 to
35fa979
Compare
kentcdodds
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect 👍
|
Would you like me to update the branch and then squash everything into one commit, or do you would prefer separate commits? |
|
Nope, we're good. Thanks @alexwendte! |
|
Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the |
|
Awesome, thanks so much Kent, I appreciate it! I’ve already learned a ton because of your generosity with recording workshops. I absolutely love the way you code, so I’m looking forward to getting to help out with what you’re working on!
|
|
🎉 This PR is included in version 3.10.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
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: