diff --git a/packages/pinia/__tests__/lifespan.spec.ts b/packages/pinia/__tests__/lifespan.spec.ts index fcb25043c1..1faa059e5a 100644 --- a/packages/pinia/__tests__/lifespan.spec.ts +++ b/packages/pinia/__tests__/lifespan.spec.ts @@ -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) + }) })