Merge pull request #3275 from spicyj/gh-3222

Don't use undefined as parent name in key warning
This commit is contained in:
Paul O’Shannessy
2015-03-02 12:47:52 -08:00
2 changed files with 46 additions and 4 deletions
+6 -4
View File
@@ -134,7 +134,8 @@ function validatePropertyKey(name, element, parentType) {
*/
function warnAndMonitorForKeyUse(message, element, parentType) {
var ownerName = getCurrentOwnerDisplayName();
var parentName = parentType.displayName || parentType.name;
var parentName = typeof parentType === 'string' ?
parentType : parentType.displayName || parentType.name;
var useName = ownerName || parentName;
var memoizer = ownerHasKeyUseWarning[message] || (
@@ -145,9 +146,10 @@ function warnAndMonitorForKeyUse(message, element, parentType) {
}
memoizer[useName] = true;
message += ownerName ?
` Check the render method of ${ownerName}.` :
` Check the React.render call using <${parentName}>.`;
message +=
ownerName ? ` Check the render method of ${ownerName}.` :
parentName ? ` Check the React.render call using <${parentName}>.` :
'';
// Usually the current owner is the offender, but if it accepts children as a
// property, it may be the creator of the child that's responsible for
@@ -80,6 +80,46 @@ describe('ReactElementValidator', function() {
);
});
it('warns for keys for arrays with no owner or parent info', function() {
spyOn(console, 'warn');
var Anonymous = React.createClass({
displayName: undefined,
render: function() {
return <div />;
}
});
var divs = [
<div />,
<div />
];
ReactTestUtils.renderIntoDocument(<Anonymous>{divs}</Anonymous>);
expect(console.warn.argsForCall.length).toBe(1);
expect(console.warn.argsForCall[0][0]).toBe(
'Warning: Each child in an array or iterator should have a unique ' +
'"key" prop. See http://fb.me/react-warning-keys for more information.'
);
});
it('warns for keys for arrays of elements with no owner info', function() {
spyOn(console, 'warn');
var divs = [
<div />,
<div />
];
ReactTestUtils.renderIntoDocument(<div>{divs}</div>);
expect(console.warn.argsForCall.length).toBe(1);
expect(console.warn.argsForCall[0][0]).toBe(
'Warning: Each child in an array or iterator should have a unique ' +
'"key" prop. Check the React.render call using <div>. See ' +
'http://fb.me/react-warning-keys for more information.'
);
});
it('warns for keys for iterables of elements in rest args', function() {
spyOn(console, 'warn');
var Component = React.createFactory(ComponentClass);