]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: lifespan test
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 3 Mar 2021 14:27:28 +0000 (15:27 +0100)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 4 Mar 2021 15:43:47 +0000 (16:43 +0100)
__tests__/lifespan.spec.ts [new file with mode: 0644]

diff --git a/__tests__/lifespan.spec.ts b/__tests__/lifespan.spec.ts
new file mode 100644 (file)
index 0000000..6c50190
--- /dev/null
@@ -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)
+  })
+})