mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2024-11-26 03:23:55 +03:00
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:
parent
9be6641d68
commit
5259dee601
|
@ -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;
|
||||||
|
|
|
@ -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',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user