]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(testing): allows faking an app
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Jun 2021 10:54:52 +0000 (12:54 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Jun 2021 10:54:52 +0000 (12:54 +0200)
__tests__/testing.spec.ts
src/testing.ts

index 509295fdeace2bef582776560edff97f3a5e2a70..56410451669d35c872a0fc00db92b201b2d4af4a 100644 (file)
@@ -117,4 +117,25 @@ describe('Testing', () => {
     expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
     expect(counter.n).toBe(0)
   })
+
+  it('executes plugins', () => {
+    const { counter, wrapper } = factory({
+      plugins: [() => ({ pluginN: 0 })],
+    })
+
+    expect(counter.pluginN).toBe(0)
+    expect(wrapper.vm.counter.pluginN).toBe(0)
+  })
+
+  it('executes plugins with fakeApp', () => {
+    const pinia = createTestingPinia({
+      plugins: [() => ({ pluginN: 0 })],
+      fakeApp: true,
+    })
+
+    const counter = useCounter(pinia)
+
+    expect(counter.pluginN).toBe(0)
+    expect(pinia.app).toHaveProperty('mount', expect.any(Function))
+  })
 })
index 4e060ee89599ff65adf278226fec77e708646afa..a09a44f140148f8451d6935d54f9038c47ee70cb 100644 (file)
@@ -1,9 +1,11 @@
+import { App, createApp } from 'vue'
 import { createPinia } from './createPinia'
 import { Pinia, PiniaStorePlugin, setActivePinia } from './rootStore'
 
 export interface TestingOptions {
   /**
-   * Plugins to be installed before the testing plugin.
+   * Plugins to be installed before the testing plugin. Add any plugins used in
+   * your application that will be used while testing.
    */
   plugins?: PiniaStorePlugin[]
 
@@ -20,6 +22,18 @@ export interface TestingOptions {
    */
   stubPatch?: boolean
 
+  /**
+   * Creates an empty App and calls `app.use(pinia)` with the created testing
+   * pinia. This is allows you to use plugins while unit testing stores as
+   * plugins **will wait for pinia to be installed in order to be executed**.
+   * Defaults to false.
+   */
+  fakeApp?: boolean
+
+  /**
+   * Function used to create a spy for actions and `$patch()`. Pre-configured
+   * with `jest.fn()` in jest projects.
+   */
   createSpy?: (fn?: (...args: any[]) => any) => (...args: any[]) => any
 }
 
@@ -28,6 +42,9 @@ export interface TestingPinia extends Pinia {
    * Clears the cache of spies used for actions.
    */
   resetSpyCache(): void
+
+  /** App used by Pinia */
+  app: App
 }
 
 /**
@@ -45,6 +62,7 @@ export function createTestingPinia({
   plugins = [],
   stubActions = true,
   stubPatch = false,
+  fakeApp = false,
   createSpy,
 }: TestingOptions = {}): TestingPinia {
   const pinia = createPinia()
@@ -80,6 +98,11 @@ export function createTestingPinia({
     store.$patch = stubPatch ? createSpy!() : createSpy!(store.$patch)
   })
 
+  if (fakeApp) {
+    const app = createApp({})
+    app.use(pinia)
+  }
+
   setActivePinia(pinia)
 
   return Object.assign(
@@ -87,6 +110,9 @@ export function createTestingPinia({
       resetSpyCache() {
         spiedActions.clear()
       },
+      get app() {
+        return this._a as App
+      },
     },
     pinia
   )