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

Fix infinite loop during unit testing #154

Closed
wants to merge 3 commits into from

Conversation

silvenon
Copy link
Contributor

Fixes #107.

During unit testing Toaster is being updated infinitely because it's waiting for height of toast messages to be truthy, which will never happen in jsdom because it's not possible to measure elements there, so it always returns 0.

const ref = t.height
? undefined
: createRectRef((rect) => {
handlers.updateHeight(t.id, rect.height);
});

This part is now more specific, it checks whether the height is a number or not, which includes 0, and in addition with some memoizations it no longer causes an infinite loop. I added a test to verify this.

The only remaining tricky part is that immediately dismissing the toast doesn't work because the store is yet to be updated with the toast height, which clears the toast message from the remove queue, so I worked around this by adding aria-hidden to the close button until the height has been initialized, so that I have something to wait for before I dismiss the toast message.

People testing react-hot-toast will also have to do something like this if they want to close immediately after opening.

In unit testing measuring dimensions is impossible, so toast height
evaluates to `0`, which causes Toaster to endlessly recompute the height
and update the store. Instead we're being more specific and checking
whether height is `undefined`, and memoizing the ref so that it isn't
being called on every render. This also required other tweaks like
properly memoizing handlers like `updateHeight` so that they can be used as hook
dependencies. (We only needed `updateHeight` to be memoized, though.)
@vercel
Copy link

vercel bot commented Feb 14, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/timo/react-hot-toast/EgN8uEsGqf7egSW79PcVxsV6fgZR
✅ Preview: https://react-hot-toast-git-fork-silvenon-testing-library-loop-timo1.vercel.app

) => {
const { reverseOrder = false, gutter = 8, defaultPosition } =
opts || {};
const startPause = useCallback(() => {
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 memoized all of these handlers individually because I needed updateHeight to be properly memoized instead of changing every time one of the other handlers change.

@szamanr
Copy link

szamanr commented Mar 18, 2022

@timolins could you or whoever is maintaining the repo review this please? 🙏

@JavierMartinz
Copy link

Review this 🙏

timolins added a commit that referenced this pull request Jul 10, 2022
* Toast height will be re-calculated `onLayoutEffect` - #133
* Memoize height ref - #107 - inspired by #154
* Restructure handlers for better memoization

Not entirely happy with the internal API yet. Might need to rework.
@silvenon silvenon deleted the testing-library-loop branch September 23, 2022 08:10
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.

Unable to test with React Testing Library
3 participants