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

toWarnInDev matcher; throw on unexpected console.error #11786

Merged
merged 19 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
77 changes: 28 additions & 49 deletions packages/react-art/src/__tests__/ReactART-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,6 @@ describe('ReactART', () => {
});

describe('ReactARTComponents', () => {
function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
}

it('should generate a <Shape> with props for drawing the Circle', () => {
const circle = renderer.create(
<Circle radius={10} stroke="green" strokeWidth={3} fill="blue" />,
Expand All @@ -348,16 +344,13 @@ describe('ReactARTComponents', () => {
});

it('should warn if radius is missing on a Circle component', () => {
spyOnDev(console, 'error');
renderer.create(<Circle stroke="green" strokeWidth={3} fill="blue" />);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: Failed prop type: The prop `radius` is marked as required in `Circle`, ' +
'but its value is `undefined`.' +
'\n in Circle (at **)',
);
}
expect(() =>
renderer.create(<Circle stroke="green" strokeWidth={3} fill="blue" />),
).toWarnDev(
'Warning: Failed prop type: The prop `radius` is marked as required in `Circle`, ' +
'but its value is `undefined`.' +
'\n in Circle (at **)',
);
});

it('should generate a <Shape> with props for drawing the Rectangle', () => {
Expand All @@ -368,21 +361,16 @@ describe('ReactARTComponents', () => {
});

it('should warn if width/height is missing on a Rectangle component', () => {
spyOnDev(console, 'error');
renderer.create(<Rectangle stroke="green" fill="blue" />);
if (__DEV__) {
expect(console.error.calls.count()).toBe(2);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: Failed prop type: The prop `width` is marked as required in `Rectangle`, ' +
'but its value is `undefined`.' +
'\n in Rectangle (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toEqual(
'Warning: Failed prop type: The prop `height` is marked as required in `Rectangle`, ' +
'but its value is `undefined`.' +
'\n in Rectangle (at **)',
);
}
expect(() =>
renderer.create(<Rectangle stroke="green" fill="blue" />),
).toWarnDev([
'Warning: Failed prop type: The prop `width` is marked as required in `Rectangle`, ' +
'but its value is `undefined`.' +
'\n in Rectangle (at **)',
'Warning: Failed prop type: The prop `height` is marked as required in `Rectangle`, ' +
'but its value is `undefined`.' +
'\n in Rectangle (at **)',
]);
});

it('should generate a <Shape> with props for drawing the Wedge', () => {
Expand All @@ -400,25 +388,16 @@ describe('ReactARTComponents', () => {
});

it('should warn if outerRadius/startAngle/endAngle is missing on a Wedge component', () => {
spyOnDev(console, 'error');
renderer.create(<Wedge fill="blue" />);
if (__DEV__) {
expect(console.error.calls.count()).toBe(3);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: Failed prop type: The prop `outerRadius` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toEqual(
'Warning: Failed prop type: The prop `startAngle` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(2)[0])).toEqual(
'Warning: Failed prop type: The prop `endAngle` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
);
}
expect(() => renderer.create(<Wedge fill="blue" />)).toWarnDev([
'Warning: Failed prop type: The prop `outerRadius` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
'Warning: Failed prop type: The prop `startAngle` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
'Warning: Failed prop type: The prop `endAngle` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
]);
});
});
131 changes: 47 additions & 84 deletions packages/react-dom/src/__tests__/CSSPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ const React = require('react');
const ReactDOM = require('react-dom');
const ReactDOMServer = require('react-dom/server');

function normalizeCodeLocInfo(str) {
return str && str.replace(/at .+?:\d+/g, 'at **');
}

describe('CSSPropertyOperations', () => {
it('should automatically append `px` to relevant styles', () => {
const styles = {
Expand Down Expand Up @@ -91,17 +87,13 @@ describe('CSSPropertyOperations', () => {
}
}

spyOnDev(console, 'error');
const root = document.createElement('div');
ReactDOM.render(<Comp />, root);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: Unsupported style property background-color. Did you mean backgroundColor?' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
}

expect(() => ReactDOM.render(<Comp />, root)).toWarnDev(
'Warning: Unsupported style property background-color. Did you mean backgroundColor?' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
});

it('should warn when updating hyphenated style names', () => {
Expand All @@ -113,28 +105,21 @@ describe('CSSPropertyOperations', () => {
}
}

spyOnDev(console, 'error');
const styles = {
'-ms-transform': 'translate3d(0, 0, 0)',
'-webkit-transform': 'translate3d(0, 0, 0)',
};
const root = document.createElement('div');
ReactDOM.render(<Comp />, root);
ReactDOM.render(<Comp style={styles} />, root);

if (__DEV__) {
expect(console.error.calls.count()).toBe(2);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: Unsupported style property -ms-transform. Did you mean msTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toEqual(
'Warning: Unsupported style property -webkit-transform. Did you mean WebkitTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
}

expect(() => ReactDOM.render(<Comp style={styles} />, root)).toWarnDev([
'Warning: Unsupported style property -ms-transform. Did you mean msTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
'Warning: Unsupported style property -webkit-transform. Did you mean WebkitTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
]);
});

it('warns when miscapitalizing vendored style names', () => {
Expand All @@ -154,25 +139,19 @@ describe('CSSPropertyOperations', () => {
}
}

spyOnDev(console, 'error');
const root = document.createElement('div');
ReactDOM.render(<Comp />, root);
if (__DEV__) {

expect(() => ReactDOM.render(<Comp />, root)).toWarnDev([
// msTransform is correct already and shouldn't warn
expect(console.error.calls.count()).toBe(2);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: Unsupported vendor-prefixed style property oTransform. ' +
'Did you mean OTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toEqual(
'Warning: Unsupported vendor-prefixed style property webkitTransform. ' +
'Did you mean WebkitTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
}
'Warning: Unsupported vendor-prefixed style property oTransform. ' +
'Did you mean OTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
'Warning: Unsupported vendor-prefixed style property webkitTransform. ' +
'Did you mean WebkitTransform?' +
'\n in div (at **)' +
'\n in Comp (at **)',
]);
});

it('should warn about style having a trailing semicolon', () => {
Expand All @@ -193,24 +172,18 @@ describe('CSSPropertyOperations', () => {
}
}

spyOnDev(console, 'error');
const root = document.createElement('div');
ReactDOM.render(<Comp />, root);
if (__DEV__) {
expect(console.error.calls.count()).toBe(2);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
"Warning: Style property values shouldn't contain a semicolon. " +
'Try "backgroundColor: blue" instead.' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toEqual(
"Warning: Style property values shouldn't contain a semicolon. " +
'Try "color: red" instead.' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
}

expect(() => ReactDOM.render(<Comp />, root)).toWarnDev([
"Warning: Style property values shouldn't contain a semicolon. " +
'Try "backgroundColor: blue" instead.' +
'\n in div (at **)' +
'\n in Comp (at **)',
"Warning: Style property values shouldn't contain a semicolon. " +
'Try "color: red" instead.' +
'\n in div (at **)' +
'\n in Comp (at **)',
]);
});

it('should warn about style containing a NaN value', () => {
Expand All @@ -222,18 +195,13 @@ describe('CSSPropertyOperations', () => {
}
}

spyOnDev(console, 'error');
const root = document.createElement('div');
ReactDOM.render(<Comp />, root);

if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: `NaN` is an invalid value for the `fontSize` css style property.' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
}
expect(() => ReactDOM.render(<Comp />, root)).toWarnDev(
'Warning: `NaN` is an invalid value for the `fontSize` css style property.' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
});

it('should not warn when setting CSS custom properties', () => {
Expand All @@ -256,18 +224,13 @@ describe('CSSPropertyOperations', () => {
}
}

spyOnDev(console, 'error');
const root = document.createElement('div');
ReactDOM.render(<Comp />, root);

if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: `Infinity` is an invalid value for the `fontSize` css style property.' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
}
expect(() => ReactDOM.render(<Comp />, root)).toWarnDev(
'Warning: `Infinity` is an invalid value for the `fontSize` css style property.' +
'\n in div (at **)' +
'\n in Comp (at **)',
);
});

it('should not add units to CSS custom properties', () => {
Expand Down
17 changes: 7 additions & 10 deletions packages/react-dom/src/__tests__/DOMPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,22 @@ describe('DOMPropertyOperations', () => {

it('should not remove attributes for special properties', () => {
const container = document.createElement('div');
spyOnDev(console, 'error');
ReactDOM.render(
<input type="text" value="foo" onChange={function() {}} />,
container,
);
expect(container.firstChild.getAttribute('value')).toBe('foo');
expect(container.firstChild.value).toBe('foo');
ReactDOM.render(
<input type="text" onChange={function() {}} />,
container,
expect(() =>
ReactDOM.render(
<input type="text" onChange={function() {}} />,
container,
),
).toWarnDev(
'A component is changing a controlled input of type text to be uncontrolled',
);
expect(container.firstChild.getAttribute('value')).toBe('foo');
expect(container.firstChild.value).toBe('foo');
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'A component is changing a controlled input of type text to be uncontrolled',
);
}
});
});
});
20 changes: 8 additions & 12 deletions packages/react-dom/src/__tests__/EventPluginHub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ describe('EventPluginHub', () => {
});

it('should prevent non-function listeners, at dispatch', () => {
spyOnDev(console, 'error');
const node = ReactTestUtils.renderIntoDocument(
<div onClick="not a function" />,
let node;
expect(() => {
node = ReactTestUtils.renderIntoDocument(
<div onClick="not a function" />,
);
}).toWarnDev(
'Expected `onClick` listener to be a function, instead got a value of `string` type.',
);
expect(function() {
ReactTestUtils.SimulateNative.click(node);
}).toThrowError(
expect(() => ReactTestUtils.SimulateNative.click(node)).toThrowError(
'Expected `onClick` listener to be a function, instead got a value of `string` type.',
);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'Expected `onClick` listener to be a function, instead got a value of `string` type.',
);
}
});

it('should not prevent null listeners, at dispatch', () => {
Expand Down
Loading