From de0b2564298543fe2376888075ea207f68d6ce1b Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 31 Mar 2022 17:36:00 +0200 Subject: [PATCH] test: add nested stores test Discussion at #1177 --- packages/testing/src/testing.spec.ts | 32 ++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/testing/src/testing.spec.ts b/packages/testing/src/testing.spec.ts index ce48a8c2..d828ae6c 100644 --- a/packages/testing/src/testing.spec.ts +++ b/packages/testing/src/testing.spec.ts @@ -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 }, + }) + }) }) -- 2.47.2