-
Notifications
You must be signed in to change notification settings - Fork 10
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
waitForNextUpdate times out with async function #4
Comments
Same problem here, but with events: import { useEffect, useState } from 'preact/hooks';
const useWindowFocus = () => {
const [visible, setVisible] = useState('visible');
useEffect(() => {
const handleVisibilityChange = () => {
setVisible(() => {
console.log('CALLED');
return document.visibilityState;
});
};
document.addEventListener('visibilitychange', handleVisibilityChange);
return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
}, []);
return visible === 'visible';
};
export { useWindowFocus };
import { renderHook, act } from '@testing-library/preact-hooks';
import { useWindowFocus } from '../use-window-focus';
Object.defineProperty(document, 'visibilityState', { value: 'visible', configurable: true, writable: true });
describe('useWindowFocus', () => {
test('given that the visibilitychange event was triggered: should change its value accordingly', async () => {
const { result, waitForNextUpdate } = renderHook(() => useWindowFocus());
expect(result.current.isFocused).toBe(true);
document.visibilityState = 'hidden';
act(() => { document.dispatchEvent(new Event('visibilitychange')) });
await waitForNextUpdate();
expect(result.current.isFocused).toBe(false);
// document.visibilityState = 'visible';
// act(() => { document.dispatchEvent(new Event('visibilitychange')) });
// expect(result.current).toBe(true);
});
}); EDIT: If the hook returns an object instead of a value it works. Here's the new code:import { useEffect, useState } from 'preact/hooks';
const useWindowFocus = () => {
const [visible, setVisible] = useState('visible');
useEffect(() => {
const handleVisibilityChange = () => setVisible(document.visibilityState);
document.addEventListener('visibilitychange', handleVisibilityChange);
return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
}, []);
return { isFocused: visible === 'visible' };
};
describe('useWindowFocus', () => {
test('given that the visibilitychange event was triggered: should change its value accordingly', async () => {
const { result } = renderHook(() => useWindowFocus());
expect(result.current.isFocused).toBe(true);
document.visibilityState = 'hidden';
act(() => { document.dispatchEvent(new Event('visibilitychange')) });
expect(result.current.isFocused).toBe(false);
document.visibilityState = 'visible';
act(() => { document.dispatchEvent(new Event('visibilitychange')) });
expect(result.current.isFocused).toBe(true);
});
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Without
await waitForNextUpdate()
, the second assertion fails.With that call, Jest times out (this works as expected when using React).
The text was updated successfully, but these errors were encountered: