From 3a2a334110dadb94cc1dddc10bd7673aa79b358f Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 19 Nov 2021 13:46:19 +0100 Subject: [PATCH] fix: accept reactive with storeToRefs Close #799 --- packages/pinia/__tests__/storeToRefs.spec.ts | 6 +++--- packages/pinia/src/store.ts | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/pinia/__tests__/storeToRefs.spec.ts b/packages/pinia/__tests__/storeToRefs.spec.ts index eb7e1d6d..8db6e197 100644 --- a/packages/pinia/__tests__/storeToRefs.spec.ts +++ b/packages/pinia/__tests__/storeToRefs.spec.ts @@ -45,7 +45,7 @@ describe('storeToRefs', () => { expect(d.value).toBe('e') }) - it.skip('setup store', () => { + it('setup store', () => { const store = defineStore('a', () => { return { a: ref(null), @@ -80,8 +80,8 @@ describe('storeToRefs', () => { expect(r.value).toEqual({ n: 2 }) expect(store.r).toEqual({ n: 2 }) store.r.n++ - expect(r.value).toEqual({ n: 2 }) - expect(store.r).toEqual({ n: 2 }) + expect(r.value).toEqual({ n: 3 }) + expect(store.r).toEqual({ n: 3 }) }) it('empty getters', () => { diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index b1ea2484..45234ad2 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -13,6 +13,7 @@ import { effectScope, EffectScope, ComputedRef, + toRaw, toRef, toRefs, Ref, @@ -526,6 +527,9 @@ function createSetupStore< }) } else { assign(store, setupStore) + // allows retrieving reactive objects with `storeToRefs()`. Must be called after assigning to the reactive object. + // https://github.com/posva/pinia/issues/799 + assign(toRaw(store), setupStore) } // use this instead of a computed with setter to be able to create it anywhere -- 2.47.3