From 0d00a2734fa11d8a4fad6c9fb796f8c9c3a25f83 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 28 Jun 2021 12:54:52 +0200 Subject: [PATCH] feat(testing): allows faking an app --- __tests__/testing.spec.ts | 21 +++++++++++++++++++++ src/testing.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/__tests__/testing.spec.ts b/__tests__/testing.spec.ts index 509295fd..56410451 100644 --- a/__tests__/testing.spec.ts +++ b/__tests__/testing.spec.ts @@ -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)) + }) }) diff --git a/src/testing.ts b/src/testing.ts index 4e060ee8..a09a44f1 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -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 ) -- 2.47.2