From 30af008c97b5514bdc4e3328e49cfc0f572272d7 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 27 Aug 2021 17:24:03 +0200 Subject: [PATCH] test: refactor pinia creation --- packages/pinia/__tests__/store.spec.ts | 41 ++++++++++++-------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index a0c06ff5..34c89e48 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -1,33 +1,29 @@ -import { createPinia, defineStore, setActivePinia, Pinia } from '../src' +import { createPinia, defineStore, setActivePinia } from '../src' import { mount } from '@vue/test-utils' import { defineComponent, getCurrentInstance, nextTick, watch } from 'vue' describe('Store', () => { - let pinia: Pinia - const useStore = () => { - // create a new store - pinia = createPinia() - setActivePinia(pinia) - return defineStore({ - id: 'main', - state: () => ({ - a: true, - nested: { - foo: 'foo', - a: { b: 'string' }, - }, - }), - })() - } + beforeEach(() => { + setActivePinia(createPinia()) + }) + + const useStore = defineStore({ + id: 'main', + state: () => ({ + a: true, + nested: { + foo: 'foo', + a: { b: 'string' }, + }, + }), + }) it('reuses a store', () => { - setActivePinia(createPinia()) const useStore = defineStore({ id: 'main' }) expect(useStore()).toBe(useStore()) }) it('works with id as first argument', () => { - setActivePinia(createPinia()) const useStore = defineStore('main', { state: () => ({ a: true, @@ -54,6 +50,7 @@ describe('Store', () => { }) it('works without setting the active pinia', async () => { + setActivePinia(undefined) const pinia = createPinia() const useStore = defineStore({ id: 'main', @@ -123,7 +120,7 @@ describe('Store', () => { a: false, nested: { foo: 'bar', - a: { b: 'string' }, + a: { b: 'string 2' }, }, } @@ -133,7 +130,7 @@ describe('Store', () => { a: false, nested: { foo: 'bar', - a: { b: 'string' }, + a: { b: 'string 2' }, }, }) }) @@ -162,7 +159,7 @@ describe('Store', () => { it('do not share the state between same id store', () => { const store = useStore() - const store2 = useStore() + const store2 = useStore(createPinia()) expect(store.$state).not.toBe(store2.$state) store.$state.nested.a.b = 'hey' expect(store2.$state.nested.a.b).toBe('string') -- 2.47.3