Skip to content

Commit

Permalink
Remove ReactTestUtils from ReactBrowserEventEmitter-test (facebook#28533
Browse files Browse the repository at this point in the history
)
  • Loading branch information
eps1lon authored and AndyPengc12 committed Apr 15, 2024
1 parent 277208b commit 3a13756
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
54 changes: 31 additions & 23 deletions packages/react-dom/src/__tests__/ReactBrowserEventEmitter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

let React;
let ReactDOMClient;
let ReactTestUtils;
let act;

let idCallOrder;
Expand All @@ -28,7 +27,6 @@ const recordIDAndReturnFalse = function (id, event) {
};
const LISTENER = jest.fn();
const ON_CLICK_KEY = 'onClick';
const ON_MOUSE_ENTER_KEY = 'onMouseEnter';

let GRANDPARENT;
let PARENT;
Expand All @@ -50,7 +48,6 @@ describe('ReactBrowserEventEmitter', () => {

React = require('react');
ReactDOMClient = require('react-dom/client');
ReactTestUtils = require('react-dom/test-utils');
act = require('internal-test-utils').act;
container = document.createElement('div');
document.body.appendChild(container);
Expand Down Expand Up @@ -184,7 +181,7 @@ describe('ReactBrowserEventEmitter', () => {
it('should continue bubbling if an error is thrown', async () => {
await renderTree();
await putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));
await putListener(PARENT, ON_CLICK_KEY, function () {
await putListener(PARENT, ON_CLICK_KEY, function (event) {
recordID(PARENT);
throw new Error('Handler interrupted');
});
Expand All @@ -193,15 +190,36 @@ describe('ReactBrowserEventEmitter', () => {
ON_CLICK_KEY,
recordID.bind(null, GRANDPARENT),
);
await expect(
act(() => {
ReactTestUtils.Simulate.click(CHILD);
}),
).rejects.toThrow();
expect(idCallOrder.length).toBe(3);
expect(idCallOrder[0]).toBe(CHILD);
expect(idCallOrder[1]).toBe(PARENT);
expect(idCallOrder[2]).toBe(GRANDPARENT);
const errorHandler = jest.fn(event => {
event.preventDefault();
});
window.addEventListener('error', errorHandler);
try {
await act(() => {
CHILD.click();
});
expect(idCallOrder.length).toBe(3);
expect(idCallOrder[0]).toBe(CHILD);
expect(idCallOrder[1]).toBe(PARENT);
expect(idCallOrder[2]).toBe(GRANDPARENT);
expect(errorHandler).toHaveBeenCalledTimes(__DEV__ ? 2 : 1);
expect(errorHandler.mock.calls[0][0]).toEqual(
expect.objectContaining({
error: expect.any(Error),
message: 'Handler interrupted',
}),
);
if (__DEV__) {
expect(errorHandler.mock.calls[1][0]).toEqual(
expect.objectContaining({
error: expect.any(Error),
message: 'Handler interrupted',
}),
);
}
} finally {
window.removeEventListener('error', errorHandler);
}
});

it('should set currentTarget', async () => {
Expand Down Expand Up @@ -347,14 +365,4 @@ describe('ReactBrowserEventEmitter', () => {
});
expect(handleParentClick).toHaveBeenCalledTimes(0);
});

it('should have mouse enter simulated by test utils', async () => {
await renderTree();
await putListener(CHILD, ON_MOUSE_ENTER_KEY, recordID.bind(null, CHILD));
await act(() => {
ReactTestUtils.Simulate.mouseEnter(CHILD);
});
expect(idCallOrder.length).toBe(1);
expect(idCallOrder[0]).toBe(CHILD);
});
});
41 changes: 41 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,47 @@ describe('ReactTestUtils', () => {
expect.objectContaining({target: input}),
);
});

it('should have mouse enter simulated by test utils', async () => {
const idCallOrder = [];
const recordID = function (id) {
idCallOrder.push(id);
};
let CHILD;
function Child(props) {
return (
<div
ref={current => (CHILD = current)}
onMouseEnter={() => {
recordID(CHILD);
}}
/>
);
}

class ChildWrapper extends React.PureComponent {
render() {
return <Child />;
}
}

const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<div>
<ChildWrapper />
<button disabled={true} />
</div>
</div>,
);
});
await act(() => {
ReactTestUtils.Simulate.mouseEnter(CHILD);
});
expect(idCallOrder).toEqual([CHILD]);
});
});

it('should call setState callback with no arguments', async () => {
Expand Down

0 comments on commit 3a13756

Please sign in to comment.