]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: can set an initialState for tests
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 3 Dec 2021 17:41:07 +0000 (18:41 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 3 Dec 2021 17:41:07 +0000 (18:41 +0100)
packages/testing/src/initialState.spec.ts [new file with mode: 0644]
packages/testing/src/testing.ts

diff --git a/packages/testing/src/initialState.spec.ts b/packages/testing/src/initialState.spec.ts
new file mode 100644 (file)
index 0000000..9b317ff
--- /dev/null
@@ -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: `
+    <button @click="counter.increment()">+1</button>
+    <span>{{ counter.n }}</span>
+    <button @click="counter.increment(10)">+10</button>
+    `,
+  })
+
+  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)
+  })
+})
index a2cea05e951952037bce24255f94bdc9326b6ef7..d891c3a4b4bf191d6f6071ae6696da2aad522f08 100644 (file)
@@ -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)