Skip to content

Commit

Permalink
Convert InvalidEventListeners to createRoot (#28180)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Feb 2, 2024
1 parent e9c13cd commit f28f022
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions packages/react-dom/src/__tests__/InvalidEventListeners-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ jest.mock('react-dom-bindings/src/events/isEventSupported');

describe('InvalidEventListeners', () => {
let React;
let ReactDOM;
let ReactDOMClient;
let act;
let container;

beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
act = require('internal-test-utils').act;

container = document.createElement('div');
document.body.appendChild(container);
Expand All @@ -30,13 +32,16 @@ describe('InvalidEventListeners', () => {
container = null;
});

it('should prevent non-function listeners, at dispatch', () => {
let node;
expect(() => {
node = ReactDOM.render(<div onClick="not a function" />, container);
it('should prevent non-function listeners, at dispatch', async () => {
const root = ReactDOMClient.createRoot(container);
await expect(async () => {
await act(() => {
root.render(<div onClick="not a function" />);
});
}).toErrorDev(
'Expected `onClick` listener to be a function, instead got a value of `string` type.',
);
const node = container.firstChild;

spyOnProd(console, 'error');

Expand All @@ -46,11 +51,13 @@ describe('InvalidEventListeners', () => {
}
window.addEventListener('error', handleWindowError);
try {
node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
}),
);
await act(() => {
node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
}),
);
});
} finally {
window.removeEventListener('error', handleWindowError);
}
Expand All @@ -77,12 +84,19 @@ describe('InvalidEventListeners', () => {
}
});

it('should not prevent null listeners, at dispatch', () => {
const node = ReactDOM.render(<div onClick={null} />, container);
node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
}),
);
it('should not prevent null listeners, at dispatch', async () => {
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<div onClick={null} />);
});

const node = container.firstChild;
await act(() => {
node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
}),
);
});
});
});

0 comments on commit f28f022

Please sign in to comment.