]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: add nested stores test
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 31 Mar 2022 15:36:00 +0000 (17:36 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 31 Mar 2022 15:36:08 +0000 (17:36 +0200)
Discussion at #1177

packages/testing/src/testing.spec.ts

index ce48a8c2cbe4753c643fd7ccae1922e641e5b5ac..d828ae6cebc4d85f48cd2aa3f0125f1f9b288bdf 100644 (file)
@@ -1,7 +1,7 @@
 import { createTestingPinia, TestingOptions } from './testing'
-import { createPinia, defineStore } from 'pinia'
+import { createPinia, defineStore, setActivePinia } from 'pinia'
 import { mount } from '@vue/test-utils'
-import { defineComponent } from 'vue'
+import { defineComponent, ref, computed } from 'vue'
 
 describe('Testing', () => {
   const useCounter = defineStore('counter', {
@@ -171,4 +171,32 @@ describe('Testing', () => {
 
     expect(wrapper.text()).toBe('empty')
   })
+
+  it('works with nested stores', () => {
+    const useA = defineStore('a', () => {
+      const n = ref(0)
+      return { n }
+    })
+
+    const useB = defineStore('b', () => {
+      const a = useA()
+      const n = ref(0)
+      const doubleA = computed(() => a.n * 2)
+      return { n, doubleA }
+    })
+
+    const pinia = createTestingPinia()
+    setActivePinia(pinia)
+
+    const b = useB()
+    const a = useA()
+
+    expect(a.n).toBe(0)
+    a.n++
+    expect(b.doubleA).toBe(2)
+    expect(pinia.state.value).toEqual({
+      a: { n: 1 },
+      b: { n: 0 },
+    })
+  })
 })