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

Remove ReactTestUtils from ReactBrowserEventEmitter-test #28533

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one is from invokeGuardedCallback

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 () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to ReactTestUtils-test since it was specific to test-utils. For synthetic event dispatches we have a test in

it('onMouseEnter and onMouseLeave', async () => {
const log = [];
const targetRef = React.createRef();
await render(
<Fixture
type="div"
targetRef={targetRef}
targetProps={{
onMouseEnter: e => {
log.push('---- inner enter');
},
onMouseLeave: e => {
log.push('---- inner leave');
},
}}
parentProps={{
onMouseEnter: e => {
log.push('--- inner parent enter');
},
onMouseLeave: e => {
log.push('--- inner parent leave');
},
}}
outerProps={{
onMouseEnter: e => {
log.push('-- outer enter');
},
onMouseLeave: e => {
log.push('-- outer leave');
},
}}
outerParentProps={{
onMouseEnter: e => {
log.push('- outer parent enter');
},
onMouseLeave: e => {
log.push('- outer parent leave');
},
}}
/>,
);
expect(log.length).toBe(0);
targetRef.current.dispatchEvent(
new MouseEvent('mouseover', {
bubbles: true,
cancelable: true,
relatedTarget: null,
}),
);
// This order isn't ideal because each root
// has a separate traversal.
expect(log).toEqual(unindent`
--- inner parent enter
---- inner enter
- outer parent enter
-- outer enter
`);
log.length = 0;
targetRef.current.dispatchEvent(
new MouseEvent('mouseout', {
bubbles: true,
cancelable: true,
relatedTarget: document.body,
}),
);
expect(log).toEqual(unindent`
---- inner leave
--- inner parent leave
-- outer leave
- outer parent leave
`);
});

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