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

Add a test for non-passive event handlers for events affected by the browsers' intervention #19658

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3137,6 +3137,67 @@ 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 => {
if (e.defaultPrevented) 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