From: Eduardo San Martin Morote Date: Wed, 3 Mar 2021 12:43:59 +0000 (+0100) Subject: test: fix lifespan test X-Git-Tag: v2.0.0-alpha.8~78 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aaee4a61851075a8b817ee8f9873fa69a0791128;p=thirdparty%2Fvuejs%2Fpinia.git test: fix lifespan test --- diff --git a/__tests__/lifespan.spec.ts b/__tests__/lifespan.spec.ts index 0e25e93b..212da3d8 100644 --- a/__tests__/lifespan.spec.ts +++ b/__tests__/lifespan.spec.ts @@ -1,6 +1,6 @@ import { createPinia, defineStore, setActivePinia } from '../src' import { mount } from '@vue/test-utils' -import { watch, nextTick, ref } from 'vue' +import { watch, nextTick, defineComponent } from 'vue' describe('Store Lifespan', () => { function defineMyStore() { @@ -26,80 +26,47 @@ describe('Store Lifespan', () => { } const pinia = createPinia() - // let pinia: object - // const useStore = () => { - // // create a new store - // pinia = {} - // setActivePinia(pinia) - // return defineMyStore()() - // } + it('state reactivity outlives component life', async () => { + const useStore = defineMyStore() + setActivePinia(createPinia()) - it('bug report', async () => { const inComponentWatch = jest.fn() - const n = ref(0) + const Component = defineComponent({ + render: () => null, + setup() { + const store = useStore() + watch(() => store.n, inComponentWatch) + store.n++ + }, + }) - const wrapper = mount( - { - render: () => null, - setup() { - watch(() => n.value, inComponentWatch) - n.value++ - }, + const options = { + global: { + plugins: [pinia], }, - { - global: { - plugins: [pinia], - }, - } - ) + } + + let wrapper = mount(Component, options) await wrapper.unmount() expect(inComponentWatch).toHaveBeenCalledTimes(1) - // store!.n++ - n.value++ + let store = useStore() + store.n++ await nextTick() expect(inComponentWatch).toHaveBeenCalledTimes(1) - }) - - it('state reactivity outlives component life', async () => { - const useStore = defineMyStore() - setActivePinia(createPinia()) - - const inComponentWatch = jest.fn() - - let store: ReturnType - - const n = ref(0) - - const wrapper = mount( - { - render: () => null, - setup() { - store = useStore() - // watch(() => store.n, inComponentWatch) - watch(() => n.value, inComponentWatch) - store.n++ - n.value++ - }, - }, - { - global: { - plugins: [pinia], - }, - } - ) + wrapper = mount(Component, options) await wrapper.unmount() - expect(inComponentWatch).toHaveBeenCalledTimes(1) + expect(inComponentWatch).toHaveBeenCalledTimes(2) - // store!.n++ - n.value++ + store = useStore() + store.n++ await nextTick() - expect(inComponentWatch).toHaveBeenCalledTimes(1) + expect(inComponentWatch).toHaveBeenCalledTimes(2) }) })