fix(redux-devtools-core): don't mutate source object during stringification (#627)

* Add failing test case for stringifyJSON

* Mutate clone of value in stringifyJSON instead of original

Co-authored-by: Nathan Bierema <nbierema@gmail.com>
This commit is contained in:
Warren Seymour 2020-09-04 23:28:36 +01:00 committed by GitHub
parent 9be6641d68
commit 5259dee601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -4,9 +4,10 @@ import { DATA_TYPE_KEY, DATA_REF_KEY } from '../constants/dataTypes';
function replacer(key, value) { function replacer(key, value) {
if (typeof value === 'object' && value !== null && DATA_TYPE_KEY in value) { if (typeof value === 'object' && value !== null && DATA_TYPE_KEY in value) {
const __serializedType__ = value[DATA_TYPE_KEY]; const __serializedType__ = value[DATA_TYPE_KEY];
delete value[DATA_TYPE_KEY]; // eslint-disable-line no-param-reassign const clone = { ...value };
const r = { data: value, __serializedType__ }; delete clone[DATA_TYPE_KEY]; // eslint-disable-line no-param-reassign
if (DATA_REF_KEY in value) r.__serializedRef__ = value[DATA_REF_KEY]; const r = { data: clone, __serializedType__ };
if (DATA_REF_KEY in value) r.__serializedRef__ = clone[DATA_REF_KEY];
return r; return r;
} }
return value; return value;

View File

@ -7,6 +7,8 @@ import App from '../src/app/containers/App';
import api from '../src/app/middlewares/api'; import api from '../src/app/middlewares/api';
import exportState from '../src/app/middlewares/exportState'; import exportState from '../src/app/middlewares/exportState';
import rootReducer from '../src/app/reducers'; import rootReducer from '../src/app/reducers';
import { DATA_TYPE_KEY } from '../src/app/constants/dataTypes';
import stringifyJSON from '../src/app/utils/stringifyJSON';
let wrapper; let wrapper;
@ -43,3 +45,25 @@ describe('App container', () => {
).toMatch(/<div class="actionListRows-\d-\d-\d+"><\/div>/); ).toMatch(/<div class="actionListRows-\d-\d-\d+"><\/div>/);
}); });
}); });
describe('stringifyJSON', () => {
it('should not mutate the source object', () => {
const src = {
isTest: true,
[DATA_TYPE_KEY]: 'Test',
};
const result = {
data: {
isTest: true,
},
__serializedType__: 'Test',
};
expect(stringifyJSON(src, true)).toEqual(JSON.stringify(result));
expect(src).toEqual({
isTest: true,
[DATA_TYPE_KEY]: 'Test',
});
});
});