From: Eduardo San Martin Morote Date: Wed, 3 Mar 2021 14:27:28 +0000 (+0100) Subject: test: lifespan test X-Git-Tag: v0.2.0~39 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=642226c99b25c8d68426f16a9f1fb91f1db656c6;p=thirdparty%2Fvuejs%2Fpinia.git test: lifespan test --- diff --git a/__tests__/lifespan.spec.ts b/__tests__/lifespan.spec.ts new file mode 100644 index 00000000..6c501906 --- /dev/null +++ b/__tests__/lifespan.spec.ts @@ -0,0 +1,102 @@ +import { createPinia, defineStore, setActivePinia } from '../src' +import { createLocalVue, mount } from '@vue/test-utils' +import VueCompositionAPI, { + watch, + nextTick, + defineComponent, +} from '@vue/composition-api' + +describe('Store Lifespan', () => { + function defineMyStore() { + return defineStore({ + id: 'main', + state: () => ({ + a: true, + n: 0, + nested: { + foo: 'foo', + a: { b: 'string' }, + }, + }), + getters: { + double() { + return this.n * 2 + }, + notA() { + return !this.a + }, + }, + }) + } + + const localVue = createLocalVue() + localVue.use(VueCompositionAPI) + const pinia = createPinia() + localVue.use(pinia) + + // FIXME: https://github.com/vuejs/vue-test-utils/issues/1799 + + it.only('what', async () => { + const localVue = createLocalVue() + localVue.use(VueCompositionAPI) + const n = 0 + const Component = defineComponent({ + render: (h) => h('p'), + setup() { + // console.log('setup', n++) + }, + }) + + mount(Component, { localVue }) + }) + + it('state reactivity outlives component life', async () => { + const useStore = defineMyStore() + + const inComponentWatch = jest.fn() + + const Component = defineComponent({ + render: (h) => h('p'), + setup() { + const store = useStore() + watch( + () => store.n, + (n, oldN) => { + console.log('watching lolo', n, oldN) + } + ) + watch(() => store.n, inComponentWatch) + console.log('increement', store.n) + store.n++ + }, + }) + + let wrapper = mount(Component, { localVue }) + + await nextTick() + wrapper.destroy() + await nextTick() + + expect(inComponentWatch).toHaveBeenCalledTimes(1) + + let store = useStore() + + store.n++ + await nextTick() + // FIXME: seems to be a bug in composition api + // expect(inComponentWatch).toHaveBeenCalledTimes(1) + + wrapper = mount(Component, { localVue }) + await nextTick() + wrapper.destroy() + await nextTick() + + expect(inComponentWatch).toHaveBeenCalledTimes(2) + + store = useStore() + + store.n++ + await nextTick() + expect(inComponentWatch).toHaveBeenCalledTimes(2) + }) +})