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

[test] Stay busy until document.fonts is ready #23736

Merged
merged 2 commits into from
Nov 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion test/regressions/TestViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,35 @@ function TestViewer(props) {
// React doesn't have any such guarantee outside of `act()` so we're approximating it.
const [ready, setReady] = React.useState(false);
React.useEffect(() => {
function handleFontsEvent(event) {
if (event.type === 'loading') {
setReady(false);
} else if (event.type === 'loadingdone') {
// Don't know if there could be multiple loaded events after we started loading multiple times.
// So make sure we're only ready if fonts are actually ready.
if (document.fonts.status === 'loaded') {
setReady(true);
}
}
}

document.fonts.addEventListener('loading', handleFontsEvent);
document.fonts.addEventListener('loadingdone', handleFontsEvent);

// Use a "real timestamp" so that we see a useful date instead of "00:00"
// eslint-disable-next-line react-hooks/rules-of-hooks -- not a React hook
const clock = useFakeTimers(new Date('Mon Aug 18 14:11:54 2014 -0500'));
// and wait `load-css` timeouts to be flushed
clock.runToLast();
setReady(true);
// In case the child triggered font fetching we're not ready yet.
// The fonts event handler will mark the test as ready on `loadingdone`
if (document.fonts.status === 'loaded') {
setReady(true);
}

return () => {
document.fonts.removeEventListener('loading', handleFontsEvent);
document.fonts.removeEventListener('loadingdone', handleFontsEvent);
clock.restore();
};
}, []);
Expand Down