Skip to content

Commit c4590fa

Browse files
committed
getInitialStoreState returns a deep copy of state
1 parent 409cc4c commit c4590fa

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"rimraf": "^2.5.2"
4040
},
4141
"dependencies": {
42+
"lodash.clonedeep": "^4.5.0",
4243
"lodash.findindex": "^4.4.0",
4344
"lodash.flattendeep": "^4.2.0",
4445
"redux": "^3.5.2",

src/initialState.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cloneDeep from 'lodash.cloneDeep';
12
import { createStore } from 'redux';
23

34
let state = null;
@@ -12,7 +13,7 @@ function buildInitialStoreState(reducer) {
1213
}
1314

1415
function getInitialStoreState() {
15-
return state;
16+
return cloneDeep(state);
1617
}
1718

1819
export {

test/general/initialState.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ describe('initialState', () => {
2828
it('should return registered value', () => {
2929
expect(getInitialStoreState()).toEqual(initialStoreState);
3030
});
31+
32+
it('should return deep clone of registered value', () => {
33+
const initialState = getInitialStoreState();
34+
initialState.newInitialStoreStateKey = 'newInitialStoreStateKey';
35+
36+
expect(getInitialStoreState().newInitialStoreStateKey)
37+
.toNotEqual(initialState.newInitialStoreStateKey);
38+
});
3139
});
3240
});
3341

0 commit comments

Comments
 (0)