Skip to content

Commit

Permalink
Rewrite another test to not use SimulateNative
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Aug 15, 2018
1 parent 52e981c commit 05da6ad
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions packages/react-dom/src/__tests__/EventPluginHub-test.js
Expand Up @@ -13,32 +13,61 @@ jest.mock('../events/isEventSupported');

describe('EventPluginHub', () => {
let React;
let ReactTestUtils;
let ReactDOM;
let container;

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

container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('should prevent non-function listeners, at dispatch', () => {
let node;
expect(() => {
node = ReactTestUtils.renderIntoDocument(
<div onClick="not a function" />,
);
node = ReactDOM.render(<div onClick="not a function" />, container);
}).toWarnDev(
'Expected `onClick` listener to be a function, instead got a value of `string` type.',
);
expect(() => ReactTestUtils.SimulateNative.click(node)).toThrowError(
'Expected `onClick` listener to be a function, instead got a value of `string` type.',

let uncaughtErrors = [];
function handleWindowError(e) {
uncaughtErrors.push(e.error);
}
window.addEventListener('error', handleWindowError);
try {
node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
}),
);
} finally {
window.removeEventListener('error', handleWindowError);
}
expect(uncaughtErrors.length).toBe(1);
expect(uncaughtErrors[0]).toEqual(
expect.objectContaining({
message:
'Expected `onClick` listener to be a function, ' +
'instead got a value of `string` type.',
}),
);
});

it('should not prevent null listeners, at dispatch', () => {
const node = ReactTestUtils.renderIntoDocument(<div onClick={null} />);
expect(function() {
ReactTestUtils.SimulateNative.click(node);
}).not.toThrow();
const node = ReactDOM.render(<div onClick={null} />, container);
node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
}),
);
});
});

0 comments on commit 05da6ad

Please sign in to comment.