From 212b1c7c321a28dcc0278481f9cc1957d32ee986 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 9 Feb 2021 19:59:37 +0000 Subject: [PATCH] Add a test for mouseover being continuous --- .../ReactDOMNativeEventHeuristic-test.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/react-dom/src/__tests__/ReactDOMNativeEventHeuristic-test.js b/packages/react-dom/src/__tests__/ReactDOMNativeEventHeuristic-test.js index 9a9c3786b7715..012c122803566 100644 --- a/packages/react-dom/src/__tests__/ReactDOMNativeEventHeuristic-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMNativeEventHeuristic-test.js @@ -13,6 +13,7 @@ let React; let ReactDOM; let Scheduler; +let act; describe('ReactDOMNativeEventHeuristic-test', () => { let container; @@ -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); }); @@ -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
{isHover ? 'hovered' : 'not hovered'}
; + } + + await act(async () => { + root.render(); + }); + 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'); + }); + }); });