From fe792193fee1eae40ae1f0945008447b36db9141 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 12 May 2021 14:19:08 +0200 Subject: [PATCH] test: lifespan with ref --- __tests__/lifespan.spec.ts | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/__tests__/lifespan.spec.ts b/__tests__/lifespan.spec.ts index 572ea4fb..36ae2ef8 100644 --- a/__tests__/lifespan.spec.ts +++ b/__tests__/lifespan.spec.ts @@ -5,6 +5,8 @@ import VueCompositionAPI, { nextTick, defineComponent, onUnmounted, + ref, + Ref, } from '@vue/composition-api' describe('Store Lifespan', () => { @@ -14,6 +16,7 @@ describe('Store Lifespan', () => { state: () => ({ a: true, n: 0, + aRef: ref(0), nested: { foo: 'foo', a: { b: 'string' }, @@ -103,4 +106,54 @@ describe('Store Lifespan', () => { await nextTick() expect(inComponentWatch).toHaveBeenCalledTimes(2) }) + + it('ref in state reactivity outlives component life', async () => { + let n: Ref + const globalWatch = jest.fn() + const destroy = watch(() => pinia.state.value.a?.n, globalWatch) + + const useStore = defineStore({ + id: 'a', + state: () => { + n = n || ref(0) + return { n } + }, + }) + + const Component = defineComponent({ + // @ts-expect-error + render: () => null, + setup() { + const store = useStore() + store.n++ + }, + }) + + const options = { + pinia, + localVue, + } + + let wrapper = mount(Component, options) + await wrapper.destroy() + + expect(globalWatch).toHaveBeenCalledTimes(1) + + let store = useStore() + store.n++ + await nextTick() + expect(globalWatch).toHaveBeenCalledTimes(2) + + wrapper = mount(Component, options) + await wrapper.destroy() + + expect(globalWatch).toHaveBeenCalledTimes(3) + + store = useStore() + store.n++ + await nextTick() + expect(globalWatch).toHaveBeenCalledTimes(4) + + destroy() + }) }) -- 2.47.3