]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: add test for disposePinia (#2454)
authorJörg Bayreuther <jb@visualjerk.de>
Fri, 20 Oct 2023 07:19:48 +0000 (09:19 +0200)
committerGitHub <noreply@github.com>
Fri, 20 Oct 2023 07:19:48 +0000 (09:19 +0200)
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
packages/pinia/__tests__/lifespan.spec.ts

index fcb25043c159a454f59c968e09d975953eb45baa..1faa059e5a89394267441cca428fa94ed542af66 100644 (file)
@@ -2,6 +2,7 @@ import { describe, it, expect, vi } from 'vitest'
 import {
   createPinia,
   defineStore,
+  disposePinia,
   getActivePinia,
   setActivePinia,
 } from '../src'
@@ -67,7 +68,7 @@ describe('Store Lifespan', () => {
     expect(getActivePinia()).toBe(pinia)
   })
 
-  it('state reactivity outlives component life', async () => {
+  it('state reactivity outlives component life', () => {
     const useStore = defineMyStore()
 
     const inComponentWatch = vi.fn()
@@ -76,7 +77,9 @@ describe('Store Lifespan', () => {
       render: () => null,
       setup() {
         const store = useStore()
-        watch(() => store.n, inComponentWatch)
+        watch(() => store.n, inComponentWatch, {
+          flush: 'sync',
+        })
         onMounted(() => {
           store.n++
         })
@@ -90,28 +93,21 @@ describe('Store Lifespan', () => {
     }
 
     let wrapper = mount(Component, options)
-    await nextTick()
-
     wrapper.unmount()
-    await nextTick()
 
     expect(inComponentWatch).toHaveBeenCalledTimes(1)
 
     let store = useStore()
     store.n++
-    await nextTick()
     expect(inComponentWatch).toHaveBeenCalledTimes(1)
 
     wrapper = mount(Component, options)
-    await nextTick()
     wrapper.unmount()
-    await nextTick()
 
     expect(inComponentWatch).toHaveBeenCalledTimes(2)
 
     store = useStore()
     store.n++
-    await nextTick()
     expect(inComponentWatch).toHaveBeenCalledTimes(2)
   })
 
@@ -168,4 +164,26 @@ describe('Store Lifespan', () => {
 
     destroy()
   })
+
+  it('dispose stops store reactivity', () => {
+    const pinia = createPinia()
+    setActivePinia(pinia)
+    const inStoreWatch = vi.fn()
+
+    const useStore = defineStore('a', () => {
+      const n = ref(0)
+      watch(n, inStoreWatch, {
+        flush: 'sync',
+      })
+      return { n }
+    })
+
+    const store = useStore()
+    store.n++
+    expect(inStoreWatch).toHaveBeenCalledTimes(1)
+
+    disposePinia(pinia)
+    store.n++
+    expect(inStoreWatch).toHaveBeenCalledTimes(1)
+  })
 })