@samitouri / QOS-React / commits / 7d6f1e3c13

Remove ReactTestUtils from ReactBrowserEventEmitter-test (#28533)

Sebastian Silbermann committed Mar 11, 2024 at 22:11 UTC 7d6f1e3c13686272c1d76813992d4e782fcb84e6
2 files changed +72 -23
packages/react-dom/src/__tests__/ReactBrowserEventEmitter-test.js
+31 -23
@@ -11,7 +11,6 @@
11
12 let React;
13 let ReactDOMClient;
14 -let ReactTestUtils;
14 let act;
15
16 let idCallOrder;
@@ -28,7 +27,6 @@ const recordIDAndReturnFalse = function (id, event) {
27 };
28 const LISTENER = jest.fn();
29 const ON_CLICK_KEY = 'onClick';
31 -const ON_MOUSE_ENTER_KEY = 'onMouseEnter';
30
31 let GRANDPARENT;
32 let PARENT;
@@ -50,7 +48,6 @@ describe('ReactBrowserEventEmitter', () => {
48
49 React = require('react');
50 ReactDOMClient = require('react-dom/client');
53 - ReactTestUtils = require('react-dom/test-utils');
51 act = require('internal-test-utils').act;
52 container = document.createElement('div');
53 document.body.appendChild(container);
@@ -184,7 +181,7 @@ describe('ReactBrowserEventEmitter', () => {
181 it('should continue bubbling if an error is thrown', async () => {
182 await renderTree();
183 await putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));
187 - await putListener(PARENT, ON_CLICK_KEY, function () {
184 + await putListener(PARENT, ON_CLICK_KEY, function (event) {
185 recordID(PARENT);
186 throw new Error('Handler interrupted');
187 });
@@ -193,15 +190,36 @@ describe('ReactBrowserEventEmitter', () => {
190 ON_CLICK_KEY,
191 recordID.bind(null, GRANDPARENT),
192 );
196 - await expect(
197 - act(() => {
198 - ReactTestUtils.Simulate.click(CHILD);
199 - }),
200 - ).rejects.toThrow();
201 - expect(idCallOrder.length).toBe(3);
202 - expect(idCallOrder[0]).toBe(CHILD);
203 - expect(idCallOrder[1]).toBe(PARENT);
204 - expect(idCallOrder[2]).toBe(GRANDPARENT);
193 + const errorHandler = jest.fn(event => {
194 + event.preventDefault();
195 + });
196 + window.addEventListener('error', errorHandler);
197 + try {
198 + await act(() => {
199 + CHILD.click();
200 + });
201 + expect(idCallOrder.length).toBe(3);
202 + expect(idCallOrder[0]).toBe(CHILD);
203 + expect(idCallOrder[1]).toBe(PARENT);
204 + expect(idCallOrder[2]).toBe(GRANDPARENT);
205 + expect(errorHandler).toHaveBeenCalledTimes(__DEV__ ? 2 : 1);
206 + expect(errorHandler.mock.calls[0][0]).toEqual(
207 + expect.objectContaining({
208 + error: expect.any(Error),
209 + message: 'Handler interrupted',
210 + }),
211 + );
212 + if (__DEV__) {
213 + expect(errorHandler.mock.calls[1][0]).toEqual(
214 + expect.objectContaining({
215 + error: expect.any(Error),
216 + message: 'Handler interrupted',
217 + }),
218 + );
219 + }
220 + } finally {
221 + window.removeEventListener('error', errorHandler);
222 + }
223 });
224
225 it('should set currentTarget', async () => {
@@ -347,14 +365,4 @@ describe('ReactBrowserEventEmitter', () => {
365 });
366 expect(handleParentClick).toHaveBeenCalledTimes(0);
367 });
350 -
351 - it('should have mouse enter simulated by test utils', async () => {
352 - await renderTree();
353 - await putListener(CHILD, ON_MOUSE_ENTER_KEY, recordID.bind(null, CHILD));
354 - await act(() => {
355 - ReactTestUtils.Simulate.mouseEnter(CHILD);
356 - });
357 - expect(idCallOrder.length).toBe(1);
358 - expect(idCallOrder[0]).toBe(CHILD);
359 - });
368 });
packages/react-dom/src/__tests__/ReactTestUtils-test.js
+41
@@ -519,6 +519,47 @@ describe('ReactTestUtils', () => {
519 expect.objectContaining({target: input}),
520 );
521 });
522 +
523 + it('should have mouse enter simulated by test utils', async () => {
524 + const idCallOrder = [];
525 + const recordID = function (id) {
526 + idCallOrder.push(id);
527 + };
528 + let CHILD;
529 + function Child(props) {
530 + return (
531 + <div
532 + ref={current => (CHILD = current)}
533 + onMouseEnter={() => {
534 + recordID(CHILD);
535 + }}
536 + />
537 + );
538 + }
539 +
540 + class ChildWrapper extends React.PureComponent {
541 + render() {
542 + return <Child />;
543 + }
544 + }
545 +
546 + const container = document.createElement('div');
547 + const root = ReactDOMClient.createRoot(container);
548 + await act(() => {
549 + root.render(
550 + <div>
551 + <div>
552 + <ChildWrapper />
553 + <button disabled={true} />
554 + </div>
555 + </div>,
556 + );
557 + });
558 + await act(() => {
559 + ReactTestUtils.Simulate.mouseEnter(CHILD);
560 + });
561 + expect(idCallOrder).toEqual([CHILD]);
562 + });
563 });
564
565 it('should call setState callback with no arguments', async () => {