From: Eduardo San Martin Morote Date: Fri, 3 Dec 2021 17:41:07 +0000 (+0100) Subject: feat: can set an initialState for tests X-Git-Tag: @pinia/testing@0.0.8~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=028e0cae2f46744f90c98914cfca13daa7ce36c1;p=thirdparty%2Fvuejs%2Fpinia.git feat: can set an initialState for tests --- diff --git a/packages/testing/src/initialState.spec.ts b/packages/testing/src/initialState.spec.ts new file mode 100644 index 00000000..9b317ff8 --- /dev/null +++ b/packages/testing/src/initialState.spec.ts @@ -0,0 +1,60 @@ +import { createTestingPinia, TestingOptions } from './testing' +import { defineStore } from 'pinia' +import { mount } from '@vue/test-utils' +import { defineComponent } from 'vue' + +describe('Testing: initial state', () => { + const useCounter = defineStore('counter', { + state: () => ({ n: 0, nested: { n: 0, other: false } }), + actions: { + increment(amount = 1) { + this.n += amount + }, + }, + }) + + const Counter = defineComponent({ + setup() { + const counter = useCounter() + return { counter } + }, + template: ` + + {{ counter.n }} + + `, + }) + + function factory(options?: TestingOptions) { + const wrapper = mount(Counter, { + global: { + plugins: [createTestingPinia(options)], + }, + }) + + const counter = useCounter() + + return { wrapper, counter } + } + + it('can set an initial state', () => { + const { counter } = factory({ + initialState: { counter: { n: 10 } }, + }) + expect(counter.nested).toEqual({ n: 0, other: false }) + expect(counter.n).toBe(10) + counter.n++ + expect(counter.n).toBe(11) + }) + + it('can provide objects', () => { + const { counter } = factory({ + initialState: { counter: { nested: { n: 10 } } }, + }) + expect(counter.n).toBe(0) + expect(counter.nested.other).toBe(false) + expect(counter.nested.n).toBe(10) + counter.nested.n++ + expect(counter.nested.n).toBe(11) + }) +}) diff --git a/packages/testing/src/testing.ts b/packages/testing/src/testing.ts index a2cea05e..d891c3a4 100644 --- a/packages/testing/src/testing.ts +++ b/packages/testing/src/testing.ts @@ -1,7 +1,19 @@ import { App, createApp } from 'vue-demi' -import { Pinia, PiniaPlugin, setActivePinia, createPinia } from 'pinia' +import { + Pinia, + PiniaPlugin, + setActivePinia, + createPinia, + StateTree, +} from 'pinia' export interface TestingOptions { + /** + * Allows defining a partial initial state of all your stores. This state gets applied after a store is created, + * allowing you to only set a few properties that are required in your test. + */ + initialState?: StateTree + /** * Plugins to be installed before the testing plugin. Add any plugins used in * your application that will be used while testing. @@ -60,6 +72,7 @@ export interface TestingPinia extends Pinia { * @returns a augmented pinia instance */ export function createTestingPinia({ + initialState = {}, plugins = [], stubActions = true, stubPatch = false, @@ -68,6 +81,12 @@ export function createTestingPinia({ }: TestingOptions = {}): TestingPinia { const pinia = createPinia() + pinia.use(({ store }) => { + if (initialState[store.$id]) { + store.$patch(initialState[store.$id]) + } + }) + plugins.forEach((plugin) => pinia.use(plugin)) const createSpy = _createSpy || (typeof jest !== 'undefined' && jest.fn)