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
18 changes: 9 additions & 9 deletions src/formatter/sortObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
return value;
}

// return date, regexp and react element values as is
if (
value instanceof Date ||
value instanceof RegExp ||
React.isValidElement(value)
) {
// return date and regexp values as is
if (value instanceof Date || value instanceof RegExp) {
return value;
}

// return react element as is but remove _owner key because it can lead to recursion
if (React.isValidElement(value)) {
const copyObj = { ...value };
delete copyObj._owner;
return copyObj;
}

seen.add(value);

// make a copy of array with each item passed through the sorting algorithm
Expand All @@ -27,9 +30,6 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
return Object.keys(value)
.sort()
.reduce((result, key) => {
if (key === '_owner') {
return result;
}
if (key === 'current' || seen.has(value[key])) {
// eslint-disable-next-line no-param-reassign
result[key] = '[Circular]';
Expand Down
46 changes: 46 additions & 0 deletions src/formatter/sortObject.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */

import React from 'react';
import sortObject from './sortObject';

describe('sortObject', () => {
Expand Down Expand Up @@ -42,4 +43,49 @@ describe('sortObject', () => {
c: date,
});
});

describe('_owner key', () => {
it('should preserve the _owner key for objects that are not react elements', () => {
const fixture = {
_owner: "_owner that doesn't belong to react element",
foo: 'bar',
};

expect(JSON.stringify(sortObject(fixture))).toEqual(
JSON.stringify({
_owner: "_owner that doesn't belong to react element",
foo: 'bar',
})
);
});

it('should remove the _owner key from top level react element', () => {
const fixture = {
reactElement: (
<div>
<span></span>
</div>
),
};

expect(JSON.stringify(sortObject(fixture))).toEqual(
JSON.stringify({
reactElement: {
type: 'div',
key: null,
props: {
children: {
type: 'span',
key: null,
props: {},
_owner: null,
_store: {},
},
},
_store: {},
},
})
);
});
});
});