Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 67f1046

Browse files
Do not clone objects to prevent circular issue. (#93). Fixes #86
1 parent 6d847d1 commit 67f1046

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ module.exports = function(options, storage, key) {
5757

5858
if (typeof savedState === 'object' && savedState !== null) {
5959
store.replaceState(merge(store.state, savedState, {
60-
arrayMerge: function (store, saved) { return saved }
60+
arrayMerge: function (store, saved) { return saved },
61+
clone: false,
6162
}));
6263
}
6364

test.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ it("respects nested values when it replaces store's state on initializing", () =
7676
expect(store.subscribe).toBeCalled();
7777
});
7878

79-
test('persist the changed parial state back to serialized JSON', () => {
79+
it('should persist the changed parial state back to serialized JSON', () => {
8080
const storage = new Storage();
8181
const store = new Vuex.Store({ state: {} });
8282

@@ -133,7 +133,7 @@ it('persist the changed partial state back to serialized JSON under a nested pat
133133
);
134134
});
135135

136-
it('not persist null values', () => {
136+
it('should not persist null values', () => {
137137
const storage = new Storage();
138138
const store = new Vuex.Store({
139139
state: { alpha: { name: null, bravo: { name: null } } }
@@ -153,7 +153,7 @@ it('not persist null values', () => {
153153
);
154154
});
155155

156-
it('persist array values', () => {
156+
it('should not merge array values when rehydrating', () => {
157157
const storage = new Storage();
158158
storage.setItem('vuex', JSON.stringify({ persisted: ['json'] }));
159159

@@ -167,6 +167,29 @@ it('persist array values', () => {
167167
expect(store.replaceState).toBeCalledWith({
168168
persisted: ['json'],
169169
});
170+
171+
expect(store.subscribe).toBeCalled();
172+
});
173+
174+
it('should not clone circular objects when rehydrating', () => {
175+
const circular = { foo: 'bar' };
176+
circular.foo = circular;
177+
178+
const storage = new Storage();
179+
storage.setItem('vuex', JSON.stringify({ persisted: 'baz' }));
180+
181+
const store = new Vuex.Store({ state: { circular } });
182+
store.replaceState = jest.fn();
183+
store.subscribe = jest.fn();
184+
185+
const plugin = createPersistedState({ storage });
186+
plugin(store);
187+
188+
expect(store.replaceState).toBeCalledWith({
189+
circular,
190+
persisted: 'baz',
191+
});
192+
170193
expect(store.subscribe).toBeCalled();
171194
});
172195

0 commit comments

Comments
 (0)