Skip to content

Modifies testHook to return unmount function #290

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

Merged
merged 4 commits into from
Feb 11, 2019

Conversation

Andrewmat
Copy link
Contributor

This PR solves #289, in which I asked to have a way to test the useEffect callback.

What:
Adds a way to test hooks behavior when component unmounts.

Why:
To test useEffect callbacks, which are called when component unmounts.

How:
Modifies testHook to return an object with an exposed unmount function in it.
As said in the issue #289, I didn't expose anything else to avoid encouraging unfocused tests. In the future, if someone asks for them, they could be included.

Checklist:

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

I also modified the file examples/react-hooks so useCounter is not a default export, and added useDocumentTitle as example.
I am not confident about the useDocumentTitle though, as its implementation is quirky and not a good example (ironically). But I couldn't think of another simple use of useEffect and its callback.

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! Looking good. Just a few things.

@@ -6,53 +6,76 @@
*/
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 guessing you had to skip the pre-commit hook or something. This should have auto-formatted when you commit it. Could you run npx kcd-scripts format please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran it and it added a linebreak in ISSUE_TEMPLATE.md and mock.react-transition-group.js. Should I also add it to the commit?
captura de tela de 2019-02-10 12-48-41
captura de tela de 2019-02-10 12-48-23

src/index.js Outdated
@@ -67,7 +67,8 @@ function TestHook({callback}) {
}

function testHook(callback) {
render(<TestHook callback={callback} />)
const { unmount } = render(<TestHook callback={callback} />)
Copy link
Member

Choose a reason for hiding this comment

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

Let's also return rerender as well I think.

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.

Chip 👍

@kentcdodds kentcdodds merged commit 9c606da into testing-library:master Feb 11, 2019
@kentcdodds
Copy link
Member

🎉 This PR is included in version 5.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@danielkcz
Copy link
Contributor

Would be nice to update typescript definition when changing API. 🙊

@Andrewmat
Copy link
Contributor Author

I haven't mess with TS typing for a long time now. Can you check if this is correct?
https://github.com/Andrewmat/react-testing-library/blob/master/typings/index.d.ts

@kentcdodds
Copy link
Member

Looks correct to me.

@Andrewmat
Copy link
Contributor Author

Andrewmat commented Feb 11, 2019

Should I also add some guidance inside CONTRIBUTING.md?

If your PR introduced some changes in the API, you are more than welcome to modify the Typescript type definition to reflect those changes. Just modify the /typings/index.d.ts file accordingly. If you have never seen Typescript definitions before, you can read more about it in its documentation pages

I thought about adding a new section in "Commiting and Pushing changes"

@kentcdodds
Copy link
Member

Sure 👍

@Andrewmat Andrewmat mentioned this pull request Feb 11, 2019
1 task
@thealjey
Copy link

wow, mind absolutely blown

I love this library

lucbpz pushed a commit to lucbpz/react-testing-library that referenced this pull request Jul 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants