From: Eduardo San Martin Morote Date: Wed, 29 Sep 2021 09:34:04 +0000 (+0200) Subject: fix: use assign to set $state X-Git-Tag: pinia@2.0.0-rc.10~13 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f3a732f86fbd0399f7b7ebc6a762a2425d08bb4c;p=thirdparty%2Fvuejs%2Fpinia.git fix: use assign to set $state Fix #682 --- diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index 34c89e48..82592688 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -137,6 +137,11 @@ describe('Store', () => { it('can replace its state', () => { const store = useStore() + const spy = jest.fn() + watch(() => store.a, spy, { flush: 'sync' }) + expect(store.a).toBe(true) + + expect(spy).toHaveBeenCalledTimes(0) // TODO: remove once plugin state achieve generics // @ts-expect-error store.$state = { @@ -148,6 +153,8 @@ describe('Store', () => { }, }, } + expect(spy).toHaveBeenCalledTimes(1) + expect(store.$state).toEqual({ a: false, nested: { diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 06f7cc6e..1082d91b 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -532,7 +532,9 @@ function createSetupStore< if (__DEV__ && hot) { throw new Error('cannot set hotState') } - pinia.state.value[$id] = state + $patch(($state) => { + assign($state, state) + }) }, })