Skip to content

Commit

Permalink
Add a test for non-passive event handlers for events affected by the …
Browse files Browse the repository at this point in the history
…browsers' intervention
  • Loading branch information
koba04 committed Aug 20, 2020
1 parent 64ddef4 commit 60bfae7
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,65 @@ describe('DOMPluginEventSystem', () => {

expect(onClick).toHaveBeenCalledTimes(1);
});

// @gate experimental
it('should be able to register non-passive handlers for events affected by the intervention', () => {
const rootContainer = document.createElement('div');
container.appendChild(rootContainer);

const defaultPreventedEvents = [];
const handler = e => defaultPreventedEvents.push(e.type);

container.addEventListener('touchstart', handler);
container.addEventListener('touchmove', handler);
container.addEventListener('wheel', handler);

const ref = React.createRef();
const setTouchStart = ReactDOM.unstable_createEventHandle(
'touchstart',
{passive: false},
);
const setTouchMove = ReactDOM.unstable_createEventHandle(
'touchmove',
{passive: false},
);
const setWheel = ReactDOM.unstable_createEventHandle('wheel', {
passive: false,
});

function Component() {
React.useEffect(() => {
const clearTouchStart = setTouchStart(ref.current, e =>
e.preventDefault(),
);
const clearTouchMove = setTouchMove(ref.current, e =>
e.preventDefault(),
);
const clearWheel = setWheel(ref.current, e =>
e.preventDefault(),
);
return () => {
clearTouchStart();
clearTouchMove();
clearWheel();
};
});
return <div ref={ref}>test</div>;
}

ReactDOM.render(<Component />, rootContainer);
Scheduler.unstable_flushAll();

dispatchEvent(ref.current, 'touchstart');
dispatchEvent(ref.current, 'touchmove');
dispatchEvent(ref.current, 'wheel');

expect(defaultPreventedEvents).toEqual([
'touchstart',
'touchmove',
'wheel',
]);
});
});
});
},
Expand Down

0 comments on commit 60bfae7

Please sign in to comment.