]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: better coverage
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 19 Aug 2021 16:40:03 +0000 (18:40 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 19 Aug 2021 16:40:03 +0000 (18:40 +0200)
packages/pinia/__tests__/lifespan.spec.ts
packages/pinia/src/store.ts
packages/testing/src/testing.ts

index 1b589a771095df2a47dcf92d3597bd9ff0ab7d2c..8c1c7c2f19a481c9b6fb538ad0a9edea596982f7 100644 (file)
@@ -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()
 
index aca79319d9d9b35df8b10636e5cf8429ea535a9f..02545365ab003303de451f251d15b88aedb20b6b 100644 (file)
@@ -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({
index b9b8a80159232e50d35135f3c2deb91cd500285d..e159a31ac240c2ece007ef3a82a2eb5e886077f7 100644 (file)
@@ -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<string, Record<string, any>>()
-
   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
       },
     },