From 819b72fc93fec2c071ae28875af3aadc2dcafbdc Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 20 Jul 2021 16:05:56 +0200 Subject: [PATCH] test: add watch on state test --- __tests__/state.spec.ts | 12 ++++++++++++ src/store.ts | 12 +++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/__tests__/state.spec.ts b/__tests__/state.spec.ts index 3f3e6e0e..b39dd088 100644 --- a/__tests__/state.spec.ts +++ b/__tests__/state.spec.ts @@ -117,6 +117,18 @@ describe('State', () => { expect(spy).toHaveBeenCalledTimes(1) }) + it('state can be watched when a ref is given', async () => { + const store = useStore() + const spy = jest.fn() + watch(() => store.name, spy) + expect(spy).not.toHaveBeenCalled() + const nameRef = ref('Ed') + // @ts-expect-error + store.$state.name = nameRef + await nextTick() + expect(spy).toHaveBeenCalledTimes(1) + }) + it('can be given a ref', () => { const pinia = createPinia() const store = useStore(pinia) diff --git a/src/store.ts b/src/store.ts index 6954103b..281fe49d 100644 --- a/src/store.ts +++ b/src/store.ts @@ -210,8 +210,6 @@ function createSetupStore< const setupStore = pinia._e.run(() => { scope = effectScope() return scope.run(() => { - const store = setup() - // skip setting up the watcher on HMR if (!__DEV__ || !hot) { watch( @@ -232,7 +230,7 @@ function createSetupStore< )! } - return store + return setup() }) })! @@ -365,8 +363,7 @@ function createSetupStore< } } - // TODO: PURE to tree shake? - const _hmrPayload = markRaw({ + const _hmrPayload = /*#__PURE__*/ markRaw({ actions: {} as Record, getters: {} as Record, state: [] as string[], @@ -423,6 +420,7 @@ function createSetupStore< const partialStore = { _p: pinia, + // _s: scope, $id, $onAction, $patch, @@ -726,10 +724,6 @@ export function defineStore(idOrOptions: any, setup?: any, setupOptions?: any) { id = idOrOptions.id } - if (__DEV__) { - // TODO: check duplicated ids - } - function useStore(pinia?: Pinia | null, hot?: Store): Store { const currentInstance = getCurrentInstance() pinia = -- 2.47.2