Skip to content

Commit

Permalink
test: add test for disposePinia (#2454)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
visualjerk and posva authored Oct 20, 2023
1 parent d798b26 commit 8a5bf46
Showing 1 changed file with 27 additions and 9 deletions.
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)
})
})

0 comments on commit 8a5bf46

Please sign in to comment.