From 2dadd6a9e33e9a2a87ee101e4cf70e17c5f367a6 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 19 Aug 2021 18:40:03 +0200 Subject: [PATCH] test: better coverage --- packages/pinia/__tests__/lifespan.spec.ts | 42 +++++++++++++++++++++-- packages/pinia/src/store.ts | 5 +++ packages/testing/src/testing.ts | 31 ++++------------- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/packages/pinia/__tests__/lifespan.spec.ts b/packages/pinia/__tests__/lifespan.spec.ts index 1b589a77..8c1c7c2f 100644 --- a/packages/pinia/__tests__/lifespan.spec.ts +++ b/packages/pinia/__tests__/lifespan.spec.ts @@ -1,6 +1,19 @@ -import { createPinia, defineStore, setActivePinia } from '../src' +import { + createPinia, + defineStore, + getActivePinia, + setActivePinia, +} from '../src' import { mount } from '@vue/test-utils' -import { watch, nextTick, defineComponent, ref, Ref, onMounted } from 'vue' +import { + watch, + nextTick, + defineComponent, + ref, + Ref, + onMounted, + getCurrentInstance, +} from 'vue' describe('Store Lifespan', () => { function defineMyStore() { @@ -28,6 +41,31 @@ describe('Store Lifespan', () => { const pinia = createPinia() + it('gets the active pinia outside of setup', () => { + setActivePinia(pinia) + expect(getCurrentInstance()).toBeFalsy() + expect(getActivePinia()).toBe(pinia) + }) + + it('gets the active pinia inside of setup', () => { + expect.assertions(3) + const pinia = createPinia() + setActivePinia(undefined) + expect(getActivePinia()).toBe(undefined) + + mount( + { + template: 'no', + setup() { + expect(getActivePinia()).toBe(pinia) + }, + }, + { global: { plugins: [pinia] } } + ) + // and outside too + expect(getActivePinia()).toBe(pinia) + }) + it('state reactivity outlives component life', async () => { const useStore = defineMyStore() diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index aca79319..02545365 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -212,6 +212,7 @@ function createSetupStore< const hotState = ref({} as S) + /* istanbul ignore if */ if (__DEV__ && !pinia._e.active) { throw new Error('Pinia destroyed') } @@ -261,6 +262,7 @@ function createSetupStore< ) } + /* istanbul ignore next */ const $reset = __DEV__ ? () => { throw new Error( @@ -478,6 +480,7 @@ function createSetupStore< Object.defineProperty(store, '$state', { get: () => (__DEV__ && hot ? hotState.value : pinia.state.value[$id]), set: (state) => { + /* istanbul ignore if */ if (__DEV__ && hot) { throw new Error('cannot set hotState') } @@ -486,6 +489,7 @@ function createSetupStore< }) // add the hotUpdate before plugins to allow them to override it + /* istanbul ignore else */ if (__DEV__) { store._hotUpdate = markRaw((newStore) => { store._hotUpdating = true @@ -583,6 +587,7 @@ function createSetupStore< // apply all plugins pinia._p.forEach((extender) => { + /* istanbul ignore else */ if (__DEV__ && IS_CLIENT) { const extensions = scope.run(() => extender({ diff --git a/packages/testing/src/testing.ts b/packages/testing/src/testing.ts index b9b8a801..e159a31a 100644 --- a/packages/testing/src/testing.ts +++ b/packages/testing/src/testing.ts @@ -44,11 +44,6 @@ export interface TestingOptions { * {@link Pinia} instance with test specific properties. */ export interface TestingPinia extends Pinia { - /** - * Clears the cache of spies used for actions. - */ - resetSpyCache(): void - /** App used by Pinia */ app: App } @@ -72,35 +67,24 @@ export function createTestingPinia({ stubActions = true, stubPatch = false, fakeApp = false, - createSpy, + createSpy: _createSpy, }: TestingOptions = {}): TestingPinia { const pinia = createPinia() plugins.forEach((plugin) => pinia.use(plugin)) - // @ts-ignore: this can fail in TS depending of the existence of jest - createSpy = createSpy || (typeof jest !== undefined && jest.fn) + const createSpy = _createSpy || (typeof jest !== undefined && jest.fn) + /* istanbul ignore if */ if (!createSpy) { throw new Error('You must configure the `createSpy` option.') } - // Cache of all actions to share them across all stores - const spiedActions = new Map>() - pinia.use(({ store, options }) => { - if (!spiedActions.has(store.$id)) { - spiedActions.set(store.$id, {}) - } - const actionsCache = spiedActions.get(store.$id)! - Object.keys(options.actions || {}).forEach((action) => { - actionsCache[action] = - actionsCache[action] || - (stubActions ? createSpy!() : createSpy!(store[action])) - store[action] = actionsCache[action] + store[action] = stubActions ? createSpy() : createSpy(store[action]) }) - store.$patch = stubPatch ? createSpy!() : createSpy!(store.$patch) + store.$patch = stubPatch ? createSpy() : createSpy(store.$patch) }) if (fakeApp) { @@ -114,10 +98,7 @@ export function createTestingPinia({ return Object.assign( { - resetSpyCache() { - spiedActions.clear() - }, - get app() { + get app(): App { return (this as TestingPinia)._a }, }, -- 2.47.3