Skip to content

Commit 028e0ca

Browse files
committed
feat: can set an initialState for tests
1 parent 2778d47 commit 028e0ca

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createTestingPinia, TestingOptions } from './testing'
2+
import { defineStore } from 'pinia'
3+
import { mount } from '@vue/test-utils'
4+
import { defineComponent } from 'vue'
5+
6+
describe('Testing: initial state', () => {
7+
const useCounter = defineStore('counter', {
8+
state: () => ({ n: 0, nested: { n: 0, other: false } }),
9+
actions: {
10+
increment(amount = 1) {
11+
this.n += amount
12+
},
13+
},
14+
})
15+
16+
const Counter = defineComponent({
17+
setup() {
18+
const counter = useCounter()
19+
return { counter }
20+
},
21+
template: `
22+
<button @click="counter.increment()">+1</button>
23+
<span>{{ counter.n }}</span>
24+
<button @click="counter.increment(10)">+10</button>
25+
`,
26+
})
27+
28+
function factory(options?: TestingOptions) {
29+
const wrapper = mount(Counter, {
30+
global: {
31+
plugins: [createTestingPinia(options)],
32+
},
33+
})
34+
35+
const counter = useCounter()
36+
37+
return { wrapper, counter }
38+
}
39+
40+
it('can set an initial state', () => {
41+
const { counter } = factory({
42+
initialState: { counter: { n: 10 } },
43+
})
44+
expect(counter.nested).toEqual({ n: 0, other: false })
45+
expect(counter.n).toBe(10)
46+
counter.n++
47+
expect(counter.n).toBe(11)
48+
})
49+
50+
it('can provide objects', () => {
51+
const { counter } = factory({
52+
initialState: { counter: { nested: { n: 10 } } },
53+
})
54+
expect(counter.n).toBe(0)
55+
expect(counter.nested.other).toBe(false)
56+
expect(counter.nested.n).toBe(10)
57+
counter.nested.n++
58+
expect(counter.nested.n).toBe(11)
59+
})
60+
})

packages/testing/src/testing.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import { App, createApp } from 'vue-demi'
2-
import { Pinia, PiniaPlugin, setActivePinia, createPinia } from 'pinia'
2+
import {
3+
Pinia,
4+
PiniaPlugin,
5+
setActivePinia,
6+
createPinia,
7+
StateTree,
8+
} from 'pinia'
39

410
export interface TestingOptions {
11+
/**
12+
* Allows defining a partial initial state of all your stores. This state gets applied after a store is created,
13+
* allowing you to only set a few properties that are required in your test.
14+
*/
15+
initialState?: StateTree
16+
517
/**
618
* Plugins to be installed before the testing plugin. Add any plugins used in
719
* your application that will be used while testing.
@@ -60,6 +72,7 @@ export interface TestingPinia extends Pinia {
6072
* @returns a augmented pinia instance
6173
*/
6274
export function createTestingPinia({
75+
initialState = {},
6376
plugins = [],
6477
stubActions = true,
6578
stubPatch = false,
@@ -68,6 +81,12 @@ export function createTestingPinia({
6881
}: TestingOptions = {}): TestingPinia {
6982
const pinia = createPinia()
7083

84+
pinia.use(({ store }) => {
85+
if (initialState[store.$id]) {
86+
store.$patch(initialState[store.$id])
87+
}
88+
})
89+
7190
plugins.forEach((plugin) => pinia.use(plugin))
7291

7392
const createSpy = _createSpy || (typeof jest !== 'undefined' && jest.fn)

0 commit comments

Comments
 (0)