]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: fix lifespan test
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 3 Mar 2021 12:43:59 +0000 (13:43 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 3 Mar 2021 12:43:59 +0000 (13:43 +0100)
__tests__/lifespan.spec.ts

index 0e25e93b4b4a2fc2f0a13df2090969f86c5a9853..212da3d878c1e0ac4cfec09c15a4f36eba7e2f57 100644 (file)
@@ -1,6 +1,6 @@
 import { createPinia, defineStore, setActivePinia } from '../src'
 import { mount } from '@vue/test-utils'
-import { watch, nextTick, ref } from 'vue'
+import { watch, nextTick, defineComponent } from 'vue'
 
 describe('Store Lifespan', () => {
   function defineMyStore() {
@@ -26,80 +26,47 @@ describe('Store Lifespan', () => {
   }
 
   const pinia = createPinia()
-  // let pinia: object
 
-  // const useStore = () => {
-  //   // create a new store
-  //   pinia = {}
-  //   setActivePinia(pinia)
-  //   return defineMyStore()()
-  // }
+  it('state reactivity outlives component life', async () => {
+    const useStore = defineMyStore()
+    setActivePinia(createPinia())
 
-  it('bug report', async () => {
     const inComponentWatch = jest.fn()
 
-    const n = ref(0)
+    const Component = defineComponent({
+      render: () => null,
+      setup() {
+        const store = useStore()
+        watch(() => store.n, inComponentWatch)
+        store.n++
+      },
+    })
 
-    const wrapper = mount(
-      {
-        render: () => null,
-        setup() {
-          watch(() => n.value, inComponentWatch)
-          n.value++
-        },
+    const options = {
+      global: {
+        plugins: [pinia],
       },
-      {
-        global: {
-          plugins: [pinia],
-        },
-      }
-    )
+    }
+
+    let wrapper = mount(Component, options)
 
     await wrapper.unmount()
 
     expect(inComponentWatch).toHaveBeenCalledTimes(1)
 
-    // store!.n++
-    n.value++
+    let store = useStore()
+    store.n++
     await nextTick()
     expect(inComponentWatch).toHaveBeenCalledTimes(1)
-  })
-
-  it('state reactivity outlives component life', async () => {
-    const useStore = defineMyStore()
-    setActivePinia(createPinia())
-
-    const inComponentWatch = jest.fn()
-
-    let store: ReturnType<typeof useStore>
-
-    const n = ref(0)
-
-    const wrapper = mount(
-      {
-        render: () => null,
-        setup() {
-          store = useStore()
-          // watch(() => store.n, inComponentWatch)
-          watch(() => n.value, inComponentWatch)
-          store.n++
-          n.value++
-        },
-      },
-      {
-        global: {
-          plugins: [pinia],
-        },
-      }
-    )
 
+    wrapper = mount(Component, options)
     await wrapper.unmount()
 
-    expect(inComponentWatch).toHaveBeenCalledTimes(1)
+    expect(inComponentWatch).toHaveBeenCalledTimes(2)
 
-    // store!.n++
-    n.value++
+    store = useStore()
+    store.n++
     await nextTick()
-    expect(inComponentWatch).toHaveBeenCalledTimes(1)
+    expect(inComponentWatch).toHaveBeenCalledTimes(2)
   })
 })