-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix infinite loop during unit testing
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.)
- Loading branch information
Showing
5 changed files
with
169 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
import { render, screen, act } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import MatchMediaMock from 'jest-matchmedia-mock'; | ||
import toast, { Toaster } from '../src'; | ||
import { TOAST_EXPIRE_DISMISS_DELAY } from '../src/core/store'; | ||
|
||
let matchMedia: MatchMediaMock; | ||
|
||
beforeAll(() => { | ||
matchMedia = new MatchMediaMock(); | ||
matchMedia.useMediaQuery('(prefers-reduced-motion: reduce)'); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach((done) => { | ||
act(() => { | ||
jest.runAllTimers(); | ||
jest.useRealTimers(); | ||
done(); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
matchMedia.destroy(); | ||
}); | ||
|
||
test('close notification', async () => { | ||
render( | ||
<> | ||
<Toaster /> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
toast((t) => ( | ||
<div> | ||
Example | ||
<button | ||
aria-hidden={typeof t.height !== 'number'} | ||
type="button" | ||
onClick={() => toast.dismiss(t.id)} | ||
title={'close'} | ||
> | ||
Close | ||
</button> | ||
</div> | ||
)); | ||
}} | ||
> | ||
Notify! | ||
</button> | ||
</> | ||
); | ||
userEvent.click(screen.getByRole('button', { name: /notify/i })); | ||
screen.getByText(/example/i); | ||
|
||
userEvent.click(await screen.findByRole('button', { name: /close/i })); | ||
act(() => { | ||
jest.advanceTimersByTime(TOAST_EXPIRE_DISMISS_DELAY); | ||
}); | ||
expect(screen.queryByText(/example/i)).not.toBeInTheDocument(); | ||
}); |