-
Notifications
You must be signed in to change notification settings - Fork 340
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
Conversation
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.)
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/timo/react-hot-toast/EgN8uEsGqf7egSW79PcVxsV6fgZR |
) => { | ||
const { reverseOrder = false, gutter = 8, defaultPosition } = | ||
opts || {}; | ||
const startPause = useCallback(() => { |
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 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.
@timolins could you or whoever is maintaining the repo review this please? 🙏 |
Review this 🙏 |
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 returns0
.react-hot-toast/src/components/toaster.tsx
Lines 84 to 88 in c5e5935
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.