Skip to content

Commit

Permalink
Add a test for mouseover being continuous
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Feb 9, 2021
1 parent d919e2c commit 212b1c7
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let React;

let ReactDOM;
let Scheduler;
let act;

describe('ReactDOMNativeEventHeuristic-test', () => {
let container;
Expand All @@ -23,6 +24,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
React = require('react');
ReactDOM = require('react-dom');
Scheduler = require('scheduler');
act = require('react-dom/test-utils').unstable_concurrentAct;

document.body.appendChild(container);
});
Expand Down Expand Up @@ -225,4 +227,35 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
// Therefore the form should have been submitted.
expect(formSubmitted).toBe(true);
});

// @gate experimental
// @gate enableDiscreteEventMicroTasks && enableNativeEventPriorityInference
it('mouse over should be user-blocking but not discrete', async () => {
const root = ReactDOM.unstable_createRoot(container);

const target = React.createRef(null);
function Foo() {
const [isHover, setHover] = React.useState(false);
React.useLayoutEffect(() => {
target.current.onmouseover = () => setHover(true);
});
return <div ref={target}>{isHover ? 'hovered' : 'not hovered'}</div>;
}

await act(async () => {
root.render(<Foo />);
});
expect(container.textContent).toEqual('not hovered');

await act(async () => {
const mouseOverEvent = document.createEvent('MouseEvents');
mouseOverEvent.initEvent('mouseover', true, true);
dispatchAndSetCurrentEvent(target.current, mouseOverEvent);

// 3s should be enough to expire the updates
Scheduler.unstable_advanceTime(3000);
expect(Scheduler).toFlushExpired([]);
expect(container.textContent).toEqual('hovered');
});
});
});

0 comments on commit 212b1c7

Please sign in to comment.