Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add test for disposePinia #2454

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions packages/pinia/__tests__/lifespan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it, expect, vi } from 'vitest'
import {
createPinia,
defineStore,
disposePinia,
getActivePinia,
setActivePinia,
} from '../src'
Expand Down Expand Up @@ -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()
Expand All @@ -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++
})
Expand All @@ -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)
})

Expand Down Expand Up @@ -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)
})
})
Loading