]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: add watch on state test
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 20 Jul 2021 14:05:56 +0000 (16:05 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 20 Jul 2021 14:05:56 +0000 (16:05 +0200)
__tests__/state.spec.ts
src/store.ts

index 3f3e6e0ea8b052eaa1f3685f79ce061fbdc9dd8d..b39dd088c7378cb44bb4fb2f0260601e6946fe6d 100644 (file)
@@ -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)
index 6954103b68a067a86498813d7eb93808986556ad..281fe49d7ee4bf35877df9fe5c998ab9c23d7c74 100644 (file)
@@ -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<string, any>,
     getters: {} as Record<string, Ref>,
     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 =