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

Always set window.event in test environment #20777

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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 @@ -33,15 +33,6 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
document.body.removeChild(container);
});

function dispatchAndSetCurrentEvent(el, event) {
try {
window.event = event;
el.dispatchEvent(event);
} finally {
window.event = undefined;
}
}

// @gate experimental
// @gate enableDiscreteEventMicroTasks && enableNativeEventPriorityInference
it('ignores discrete events on a pending removed element', async () => {
Expand Down Expand Up @@ -78,9 +69,9 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
// Dispatch a click event on the Disable-button.
const firstEvent = document.createEvent('Event');
firstEvent.initEvent('click', true, true);
expect(() =>
dispatchAndSetCurrentEvent(disableButton, firstEvent),
).toErrorDev(['An update to Form inside a test was not wrapped in act']);
expect(() => disableButton.dispatchEvent(firstEvent)).toErrorDev([
'An update to Form inside a test was not wrapped in act',
]);

// There should now be a pending update to disable the form.
// This should not have flushed yet since it's in concurrent mode.
Expand Down Expand Up @@ -144,7 +135,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
const firstEvent = document.createEvent('Event');
firstEvent.initEvent('click', true, true);
expect(() => {
dispatchAndSetCurrentEvent(disableButton, firstEvent);
disableButton.dispatchEvent(firstEvent);
}).toErrorDev(['An update to Form inside a test was not wrapped in act']);

// There should now be a pending update to disable the form.
Expand All @@ -158,7 +149,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
// Now let's dispatch an event on the submit button.
const secondEvent = document.createEvent('Event');
secondEvent.initEvent('click', true, true);
dispatchAndSetCurrentEvent(submitButton, secondEvent);
submitButton.dispatchEvent(secondEvent);

// Therefore the form should never have been submitted.
expect(formSubmitted).toBe(false);
Expand Down Expand Up @@ -208,7 +199,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
const firstEvent = document.createEvent('Event');
firstEvent.initEvent('click', true, true);
expect(() => {
dispatchAndSetCurrentEvent(enableButton, firstEvent);
enableButton.dispatchEvent(firstEvent);
}).toErrorDev(['An update to Form inside a test was not wrapped in act']);

// There should now be a pending update to enable the form.
Expand All @@ -222,7 +213,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
// Now let's dispatch an event on the submit button.
const secondEvent = document.createEvent('Event');
secondEvent.initEvent('click', true, true);
dispatchAndSetCurrentEvent(submitButton, secondEvent);
submitButton.dispatchEvent(secondEvent);

// Therefore the form should have been submitted.
expect(formSubmitted).toBe(true);
Expand Down Expand Up @@ -250,7 +241,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
await act(async () => {
const mouseOverEvent = document.createEvent('MouseEvents');
mouseOverEvent.initEvent('mouseover', true, true);
dispatchAndSetCurrentEvent(target.current, mouseOverEvent);
target.current.dispatchEvent(mouseOverEvent);

// 3s should be enough to expire the updates
Scheduler.unstable_advanceTime(3000);
Expand Down Expand Up @@ -283,7 +274,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
// but we should still correctly determine their priority.
const mouseEnterEvent = document.createEvent('MouseEvents');
mouseEnterEvent.initEvent('mouseenter', true, true);
dispatchAndSetCurrentEvent(target.current, mouseEnterEvent);
target.current.dispatchEvent(mouseEnterEvent);

// 3s should be enough to expire the updates
Scheduler.unstable_advanceTime(3000);
Expand Down
12 changes: 12 additions & 0 deletions scripts/jest/setupEnvironment.js
Expand Up @@ -22,6 +22,18 @@ global.__EXPERIMENTAL__ =
global.__VARIANT__ = !!process.env.VARIANT;

if (typeof window !== 'undefined') {
// We rely on window.event for heuristics so let's set it in tests.
window.event = undefined;
const oldDispatchEvent = EventTarget.prototype.dispatchEvent;
EventTarget.prototype.dispatchEvent = function(e) {
try {
window.event = e;
return oldDispatchEvent.apply(this, arguments);
} finally {
window.event = undefined;
}
};

global.requestIdleCallback = function(callback) {
return setTimeout(() => {
callback({
Expand Down