Skip to content

Commit

Permalink
Upgrade to Jest 23 (#12894)
Browse files Browse the repository at this point in the history
* Upgrade to Jest 23 beta

* prefer `.toHaveBeenCalledTimes`

* 23 stable
  • Loading branch information
SimenB authored and gaearon committed May 28, 2018
1 parent a32f857 commit aa85b0f
Show file tree
Hide file tree
Showing 28 changed files with 602 additions and 1,004 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"babel-code-frame": "^6.26.0",
"babel-core": "^6.0.0",
"babel-eslint": "^8.0.0",
"babel-jest": "^22.4.4",
"babel-jest": "^23.0.1",
"babel-plugin-check-es2015-constants": "^6.5.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
Expand Down Expand Up @@ -69,8 +69,8 @@
"gzip-js": "~0.3.2",
"gzip-size": "^3.0.0",
"jasmine-check": "^1.0.0-rc.0",
"jest": "^22.4.4",
"jest-diff": "^22.4.3",
"jest": "^23.0.1",
"jest-diff": "^23.0.1",
"merge-stream": "^1.0.0",
"minimatch": "^3.0.4",
"minimist": "^1.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('DOMPropertyOperations', () => {
spyOnDevAndProd(container.firstChild, 'setAttribute');
ReactDOM.render(<progress value={30} />, container);
ReactDOM.render(<progress value="30" />, container);
expect(container.firstChild.setAttribute.calls.count()).toBe(2);
expect(container.firstChild.setAttribute).toHaveBeenCalledTimes(2);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ describe('ReactBrowserEventEmitter', () => {
it('should invoke a simple handler registered on a node', () => {
registerSimpleTestHandler();
ReactTestUtils.Simulate.click(CHILD);
expect(LISTENER.mock.calls.length).toBe(1);
expect(LISTENER).toHaveBeenCalledTimes(1);
});

it('should not invoke handlers if ReactBrowserEventEmitter is disabled', () => {
registerSimpleTestHandler();
ReactBrowserEventEmitter.setEnabled(false);
ReactTestUtils.SimulateNative.click(CHILD);
expect(LISTENER.mock.calls.length).toBe(0);
expect(LISTENER).toHaveBeenCalledTimes(0);
ReactBrowserEventEmitter.setEnabled(true);
ReactTestUtils.SimulateNative.click(CHILD);
expect(LISTENER.mock.calls.length).toBe(1);
expect(LISTENER).toHaveBeenCalledTimes(1);
});

it('should bubble simply', () => {
Expand Down Expand Up @@ -301,7 +301,7 @@ describe('ReactBrowserEventEmitter', () => {
putListener(CHILD, ON_CLICK_KEY, handleChildClick);
putListener(PARENT, ON_CLICK_KEY, handleParentClick);
ReactTestUtils.Simulate.click(CHILD);
expect(handleParentClick.mock.calls.length).toBe(1);
expect(handleParentClick).toHaveBeenCalledTimes(1);
});

it('should not invoke newly inserted handlers while bubbling', () => {
Expand All @@ -311,7 +311,7 @@ describe('ReactBrowserEventEmitter', () => {
};
putListener(CHILD, ON_CLICK_KEY, handleChildClick);
ReactTestUtils.Simulate.click(CHILD);
expect(handleParentClick.mock.calls.length).toBe(0);
expect(handleParentClick).toHaveBeenCalledTimes(0);
});

it('should have mouse enter simulated by test utils', () => {
Expand All @@ -325,7 +325,7 @@ describe('ReactBrowserEventEmitter', () => {
spyOnDevAndProd(EventTarget.prototype, 'addEventListener');
ReactBrowserEventEmitter.listenTo(ON_CLICK_KEY, document);
ReactBrowserEventEmitter.listenTo(ON_CLICK_KEY, document);
expect(EventTarget.prototype.addEventListener.calls.count()).toBe(1);
expect(EventTarget.prototype.addEventListener).toHaveBeenCalledTimes(1);
});

it('should work with event plugins without dependencies', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-dom/src/__tests__/ReactComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ describe('ReactComponent', () => {
const callback = jest.fn();
const container = document.createElement('div');
ReactDOM.render(<div />, container, callback);
expect(callback.mock.calls.length).toBe(1);
expect(callback).toHaveBeenCalledTimes(1);
ReactDOM.render(<div className="foo" />, container, callback);
expect(callback.mock.calls.length).toBe(2);
expect(callback).toHaveBeenCalledTimes(2);
ReactDOM.render(<span />, container, callback);
expect(callback.mock.calls.length).toBe(3);
expect(callback).toHaveBeenCalledTimes(3);
});

it('throws usefully when rendering badly-typed elements', () => {
Expand Down
54 changes: 27 additions & 27 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,28 +738,28 @@ describe('ReactDOMComponent', () => {
node.removeAttribute.mockImplementation(nodeRemoveAttribute);

ReactDOM.render(<div id="" />, container);
expect(node.setAttribute.mock.calls.length).toBe(0);
expect(node.removeAttribute.mock.calls.length).toBe(0);
expect(node.setAttribute).toHaveBeenCalledTimes(0);
expect(node.removeAttribute).toHaveBeenCalledTimes(0);

ReactDOM.render(<div id="foo" />, container);
expect(node.setAttribute.mock.calls.length).toBe(1);
expect(node.removeAttribute.mock.calls.length).toBe(0);
expect(node.setAttribute).toHaveBeenCalledTimes(1);
expect(node.removeAttribute).toHaveBeenCalledTimes(0);

ReactDOM.render(<div id="foo" />, container);
expect(node.setAttribute.mock.calls.length).toBe(1);
expect(node.removeAttribute.mock.calls.length).toBe(0);
expect(node.setAttribute).toHaveBeenCalledTimes(1);
expect(node.removeAttribute).toHaveBeenCalledTimes(0);

ReactDOM.render(<div />, container);
expect(node.setAttribute.mock.calls.length).toBe(1);
expect(node.removeAttribute.mock.calls.length).toBe(1);
expect(node.setAttribute).toHaveBeenCalledTimes(1);
expect(node.removeAttribute).toHaveBeenCalledTimes(1);

ReactDOM.render(<div id="" />, container);
expect(node.setAttribute.mock.calls.length).toBe(2);
expect(node.removeAttribute.mock.calls.length).toBe(1);
expect(node.setAttribute).toHaveBeenCalledTimes(2);
expect(node.removeAttribute).toHaveBeenCalledTimes(1);

ReactDOM.render(<div />, container);
expect(node.setAttribute.mock.calls.length).toBe(2);
expect(node.removeAttribute.mock.calls.length).toBe(2);
expect(node.setAttribute).toHaveBeenCalledTimes(2);
expect(node.removeAttribute).toHaveBeenCalledTimes(2);
});

it('should not incur unnecessary DOM mutations for string properties', () => {
Expand All @@ -768,7 +768,7 @@ describe('ReactDOMComponent', () => {

const node = container.firstChild;

const nodeValueSetter = jest.genMockFn();
const nodeValueSetter = jest.fn();

const oldSetAttribute = node.setAttribute.bind(node);
node.setAttribute = function(key, value) {
Expand All @@ -777,22 +777,22 @@ describe('ReactDOMComponent', () => {
};

ReactDOM.render(<div value="foo" />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter).toHaveBeenCalledTimes(1);

ReactDOM.render(<div value="foo" />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter).toHaveBeenCalledTimes(1);

ReactDOM.render(<div />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter).toHaveBeenCalledTimes(1);

ReactDOM.render(<div value={null} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter).toHaveBeenCalledTimes(1);

ReactDOM.render(<div value="" />, container);
expect(nodeValueSetter.mock.calls.length).toBe(2);
expect(nodeValueSetter).toHaveBeenCalledTimes(2);

ReactDOM.render(<div />, container);
expect(nodeValueSetter.mock.calls.length).toBe(2);
expect(nodeValueSetter).toHaveBeenCalledTimes(2);
});

it('should not incur unnecessary DOM mutations for boolean properties', () => {
Expand All @@ -812,16 +812,16 @@ describe('ReactDOMComponent', () => {
});

ReactDOM.render(<div checked={true} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(0);
expect(nodeValueSetter).toHaveBeenCalledTimes(0);

ReactDOM.render(<div />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter).toHaveBeenCalledTimes(1);

ReactDOM.render(<div checked={false} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(2);
expect(nodeValueSetter).toHaveBeenCalledTimes(2);

ReactDOM.render(<div checked={true} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(3);
expect(nodeValueSetter).toHaveBeenCalledTimes(3);
});

it('should ignore attribute whitelist for elements with the "is" attribute', () => {
Expand Down Expand Up @@ -850,9 +850,9 @@ describe('ReactDOMComponent', () => {
ReactDOM.render(<div dir={null} />, container);
ReactDOM.render(<div dir={undefined} />, container);
ReactDOM.render(<div />, container);
expect(setter.mock.calls.length).toBe(0);
expect(setter).toHaveBeenCalledTimes(0);
ReactDOM.render(<div dir="ltr" />, container);
expect(setter.mock.calls.length).toBe(1);
expect(setter).toHaveBeenCalledTimes(1);
});

it('handles multiple child updates without interference', () => {
Expand Down Expand Up @@ -984,7 +984,7 @@ describe('ReactDOMComponent', () => {
container.getElementsByTagName('source')[0].dispatchEvent(errorEvent);

if (__DEV__) {
expect(console.log.calls.count()).toBe(1);
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log.calls.argsFor(0)[0]).toContain('onError called');
}
});
Expand Down Expand Up @@ -1315,7 +1315,7 @@ describe('ReactDOMComponent', () => {
container.getElementsByTagName('image')[0].dispatchEvent(loadEvent);

if (__DEV__) {
expect(console.log.calls.count()).toBe(2);
expect(console.log).toHaveBeenCalledTimes(2);
expect(console.log.calls.argsFor(0)[0]).toContain('onError called');
expect(console.log.calls.argsFor(1)[0]).toContain('onLoad called');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('ReactDOMEventListener', () => {
childNode.dispatchEvent(nativeEvent);

expect(mouseOut).toBeCalled();
expect(mouseOut.mock.calls.length).toBe(2);
expect(mouseOut).toHaveBeenCalledTimes(2);
expect(mouseOut.mock.calls[0][0]).toEqual(childNode);
expect(mouseOut.mock.calls[1][0]).toEqual(parentNode);

Expand Down Expand Up @@ -97,7 +97,7 @@ describe('ReactDOMEventListener', () => {
childNode.dispatchEvent(nativeEvent);

expect(mouseOut).toBeCalled();
expect(mouseOut.mock.calls.length).toBe(3);
expect(mouseOut).toHaveBeenCalledTimes(3);
expect(mouseOut.mock.calls[0][0]).toEqual(childNode);
expect(mouseOut.mock.calls[1][0]).toEqual(parentNode);
expect(mouseOut.mock.calls[2][0]).toEqual(grandParentNode);
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('ReactDOMEventListener', () => {
childNode.dispatchEvent(nativeEvent);

// Child and parent should both call from event handlers.
expect(mock.mock.calls.length).toBe(2);
expect(mock).toHaveBeenCalledTimes(2);
// The first call schedules a render of '1' into the 'Child'.
// However, we're batching so it isn't flushed yet.
expect(mock.mock.calls[0][0]).toBe('Child');
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('ReactDOMEventListener', () => {
instance.getInner().dispatchEvent(nativeEvent);

expect(mouseOut).toBeCalled();
expect(mouseOut.mock.calls.length).toBe(1);
expect(mouseOut).toHaveBeenCalledTimes(1);
expect(mouseOut.mock.calls[0][0]).toEqual(instance.getInner());
document.body.removeChild(container);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactDOMIframe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('ReactDOMIframe', () => {
});

it('should trigger load events', () => {
const onLoadSpy = jasmine.createSpy();
const onLoadSpy = jest.fn();
let iframe = React.createElement('iframe', {onLoad: onLoadSpy});
iframe = ReactTestUtils.renderIntoDocument(iframe);

Expand Down
14 changes: 7 additions & 7 deletions packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ describe('ReactDOMInput', () => {

const node = container.firstChild;
let nodeValue = 'a';
const nodeValueSetter = jest.genMockFn();
const nodeValueSetter = jest.fn();
Object.defineProperty(node, 'value', {
get: function() {
return nodeValue;
Expand All @@ -546,10 +546,10 @@ describe('ReactDOMInput', () => {
});

ReactDOM.render(<input value="a" onChange={() => {}} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(0);
expect(nodeValueSetter).toHaveBeenCalledTimes(0);

ReactDOM.render(<input value="b" onChange={() => {}} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter).toHaveBeenCalledTimes(1);
});

it('should not incur unnecessary DOM mutations for numeric type conversion', () => {
Expand All @@ -558,7 +558,7 @@ describe('ReactDOMInput', () => {

const node = container.firstChild;
let nodeValue = '0';
const nodeValueSetter = jest.genMockFn();
const nodeValueSetter = jest.fn();
Object.defineProperty(node, 'value', {
get: function() {
return nodeValue;
Expand All @@ -569,7 +569,7 @@ describe('ReactDOMInput', () => {
});

ReactDOM.render(<input value={0} onChange={() => {}} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(0);
expect(nodeValueSetter).toHaveBeenCalledTimes(0);
});

it('should not incur unnecessary DOM mutations for the boolean type conversion', () => {
Expand All @@ -578,7 +578,7 @@ describe('ReactDOMInput', () => {

const node = container.firstChild;
let nodeValue = 'true';
const nodeValueSetter = jest.genMockFn();
const nodeValueSetter = jest.fn();
Object.defineProperty(node, 'value', {
get: function() {
return nodeValue;
Expand All @@ -589,7 +589,7 @@ describe('ReactDOMInput', () => {
});

ReactDOM.render(<input value={true} onChange={() => {}} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(0);
expect(nodeValueSetter).toHaveBeenCalledTimes(0);
});

it('should properly control a value of number `0`', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-dom/src/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('ReactDOMTextarea', () => {

const node = container.firstChild;
let nodeValue = 'a';
const nodeValueSetter = jest.genMockFn();
const nodeValueSetter = jest.fn();
Object.defineProperty(node, 'value', {
get: function() {
return nodeValue;
Expand All @@ -229,10 +229,10 @@ describe('ReactDOMTextarea', () => {
});

ReactDOM.render(<textarea value="a" onChange={emptyFunction} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(0);
expect(nodeValueSetter).toHaveBeenCalledTimes(0);

ReactDOM.render(<textarea value="b" onChange={emptyFunction} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter).toHaveBeenCalledTimes(1);
});

it('should properly control a value of number `0`', () => {
Expand Down
Loading

0 comments on commit aa85b0f

Please sign in to comment.