From ccef072ce3dcea41b15d9aeba418e466d650146e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Bayreuther?= Date: Thu, 19 Oct 2023 20:17:41 +0200 Subject: [PATCH 1/3] chore: test dispose stops store reactivity --- packages/pinia/__tests__/lifespan.spec.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/pinia/__tests__/lifespan.spec.ts b/packages/pinia/__tests__/lifespan.spec.ts index fcb25043c1..56113d890b 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' @@ -168,4 +169,26 @@ describe('Store Lifespan', () => { destroy() }) + + it('dispose stops store reactivity', () => { + const n = ref(0) + const pinia = createPinia() + setActivePinia(pinia) + const inStoreWatch = vi.fn() + + const useStore = defineStore('a', () => { + watch(n, inStoreWatch, { + flush: 'sync', + }) + return { n } + }) + + const store = useStore() + store.n++ + expect(inStoreWatch).toHaveBeenCalledTimes(1) + + disposePinia(pinia) + store.n++ + expect(inStoreWatch).toHaveBeenCalledTimes(1) + }) }) From 4215f502cc095d2f2c7127eceadecd0f7703996b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Bayreuther?= Date: Thu, 19 Oct 2023 20:31:24 +0200 Subject: [PATCH 2/3] chore: use flush sync instead of nextTicks --- packages/pinia/__tests__/lifespan.spec.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/pinia/__tests__/lifespan.spec.ts b/packages/pinia/__tests__/lifespan.spec.ts index 56113d890b..6aa7f98824 100644 --- a/packages/pinia/__tests__/lifespan.spec.ts +++ b/packages/pinia/__tests__/lifespan.spec.ts @@ -68,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() @@ -77,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++ }) @@ -91,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) }) From fbb8ffcac64737cebe8a037ddc8061eb6a8cfb18 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 20 Oct 2023 09:10:54 +0200 Subject: [PATCH 3/3] Update packages/pinia/__tests__/lifespan.spec.ts --- packages/pinia/__tests__/lifespan.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pinia/__tests__/lifespan.spec.ts b/packages/pinia/__tests__/lifespan.spec.ts index 6aa7f98824..1faa059e5a 100644 --- a/packages/pinia/__tests__/lifespan.spec.ts +++ b/packages/pinia/__tests__/lifespan.spec.ts @@ -166,12 +166,12 @@ describe('Store Lifespan', () => { }) it('dispose stops store reactivity', () => { - const n = ref(0) const pinia = createPinia() setActivePinia(pinia) const inStoreWatch = vi.fn() const useStore = defineStore('a', () => { + const n = ref(0) watch(n, inStoreWatch, { flush: 'sync', })