Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions src/shared/utils/__tests__/traverseAllChildren-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,39 @@ describe('traverseAllChildren', function() {
}
});

it('should allow extension of native prototypes', function() {
/*eslint-disable no-extend-native */
String.prototype.key = 'react';
Number.prototype.key = 'rocks';
/*eslint-enable no-extend-native */

var instance = (
<div>
{'a'}
{13}
</div>
);

var traverseFn = jasmine.createSpy();

traverseAllChildren(instance.props.children, traverseFn, null);
expect(traverseFn.calls.length).toBe(2);

expect(traverseFn).toHaveBeenCalledWith(
null,
'a',
'.0'
);
expect(traverseFn).toHaveBeenCalledWith(
null,
13,
'.1'
);

delete String.prototype.key;
delete Number.prototype.key;
});

it('should throw on object', function() {
expect(function() {
traverseAllChildren({a: 1, b: 2}, function() {}, null);
Expand Down
4 changes: 3 additions & 1 deletion src/shared/utils/traverseAllChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ function userProvidedKeyEscaper(match) {
* @return {string}
*/
function getComponentKey(component, index) {
if (component && component.key != null) {
// Do some typechecking here since we call this blindly. We want to ensure
// that we don't block potential future ES APIs.
if (component && typeof component === 'object' && component.key != null) {
// Explicit key
return wrapUserProvidedKey(component.key);
}
Expand Down