Skip to content

Commit 05da6ad

Browse files
committed
Rewrite another test to not use SimulateNative
1 parent 52e981c commit 05da6ad

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

packages/react-dom/src/__tests__/EventPluginHub-test.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,61 @@ jest.mock('../events/isEventSupported');
1313

1414
describe('EventPluginHub', () => {
1515
let React;
16-
let ReactTestUtils;
16+
let ReactDOM;
17+
let container;
1718

1819
beforeEach(() => {
1920
jest.resetModules();
2021
React = require('react');
21-
ReactTestUtils = require('react-dom/test-utils');
22+
ReactDOM = require('react-dom');
23+
24+
container = document.createElement('div');
25+
document.body.appendChild(container);
26+
});
27+
28+
afterEach(() => {
29+
document.body.removeChild(container);
30+
container = null;
2231
});
2332

2433
it('should prevent non-function listeners, at dispatch', () => {
2534
let node;
2635
expect(() => {
27-
node = ReactTestUtils.renderIntoDocument(
28-
<div onClick="not a function" />,
29-
);
36+
node = ReactDOM.render(<div onClick="not a function" />, container);
3037
}).toWarnDev(
3138
'Expected `onClick` listener to be a function, instead got a value of `string` type.',
3239
);
33-
expect(() => ReactTestUtils.SimulateNative.click(node)).toThrowError(
34-
'Expected `onClick` listener to be a function, instead got a value of `string` type.',
40+
41+
let uncaughtErrors = [];
42+
function handleWindowError(e) {
43+
uncaughtErrors.push(e.error);
44+
}
45+
window.addEventListener('error', handleWindowError);
46+
try {
47+
node.dispatchEvent(
48+
new MouseEvent('click', {
49+
bubbles: true,
50+
}),
51+
);
52+
} finally {
53+
window.removeEventListener('error', handleWindowError);
54+
}
55+
expect(uncaughtErrors.length).toBe(1);
56+
expect(uncaughtErrors[0]).toEqual(
57+
expect.objectContaining({
58+
message:
59+
'Expected `onClick` listener to be a function, ' +
60+
'instead got a value of `string` type.',
61+
}),
3562
);
3663
});
3764

3865
it('should not prevent null listeners, at dispatch', () => {
39-
const node = ReactTestUtils.renderIntoDocument(<div onClick={null} />);
40-
expect(function() {
41-
ReactTestUtils.SimulateNative.click(node);
42-
}).not.toThrow();
66+
const node = ReactDOM.render(<div onClick={null} />, container);
67+
node.dispatchEvent(
68+
new MouseEvent('click', {
69+
bubbles: true,
70+
}),
71+
);
4372
});
4473
});

0 commit comments

Comments
 (0)