]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(testing): allow mocking $reset
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 8 May 2023 15:03:44 +0000 (17:03 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 8 May 2023 15:04:57 +0000 (17:04 +0200)
Fix #2188

packages/testing/src/testing.spec.ts
packages/testing/src/testing.ts

index 307cc901540af0d186c0ad49af15df4249bd6a7e..ba70197460ed7a83c2821ea87b8f592d67a812d1 100644 (file)
@@ -38,6 +38,43 @@ describe('Testing', () => {
     return { wrapper, counter }
   }
 
+  const useSetupStore = defineStore('setup', () => {
+    const n = ref(0)
+    const double = computed(() => n.value * 2)
+    function increment() {
+      n.value++
+    }
+    function $reset() {
+      n.value = 0
+    }
+
+    return { n, double, increment, $reset }
+  })
+
+  const CounterSetup = defineComponent({
+    setup() {
+      const counter = useSetupStore()
+      return { counter }
+    },
+    template: `
+    <button @click="counter.increment()">+1</button>
+    <span>{{ counter.n }}</span>
+    <button @click="counter.increment(10)">+10</button>
+    `,
+  })
+
+  function factorySetupStore(options?: TestingOptions) {
+    const wrapper = mount(CounterSetup, {
+      global: {
+        plugins: [createTestingPinia(options)],
+      },
+    })
+
+    const counter = useSetupStore()
+
+    return { wrapper, counter }
+  }
+
   it('spies with no config', () => {
     const { counter, wrapper } = factory()
 
@@ -108,14 +145,19 @@ describe('Testing', () => {
     expect(counter.n).toBe(0)
   })
 
-  it('can stub $patch calls', () => {
-    const { counter } = factory({ stubPatch: true })
+  it('ignores $reset in option stores', () => {
+    const { counter } = factory()
 
+    counter.n = 5
+    counter.$reset()
     expect(counter.n).toBe(0)
-    expect(counter.$patch).toHaveBeenCalledTimes(0)
-    counter.$patch({ n: 1 })
-    expect(counter.$patch).toHaveBeenCalledTimes(1)
-    expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
+  })
+
+  it('ignores $reset in setup stores', () => {
+    const { counter } = factorySetupStore()
+
+    counter.n = 5
+    expect(() => counter.$reset()).not.toThrow()
     expect(counter.n).toBe(0)
   })
 
index b709bb6b03bd1ba847678370c3a63c637606d8f4..a3e169ab2914f67158c15084def62fa3ec31ed04 100644 (file)
@@ -49,6 +49,12 @@ export interface TestingOptions {
    */
   stubPatch?: boolean
 
+  /**
+   * When set to true, calls to `$reset()` won't change the state. Defaults to
+   * false.
+   */
+  stubReset?: boolean
+
   /**
    * Creates an empty App and calls `app.use(pinia)` with the created testing
    * pinia. This allows you to use plugins while unit testing stores as
@@ -95,6 +101,7 @@ export function createTestingPinia({
   plugins = [],
   stubActions = true,
   stubPatch = false,
+  stubReset = false,
   fakeApp = false,
   createSpy: _createSpy,
 }: TestingOptions = {}): TestingPinia {
@@ -128,10 +135,12 @@ export function createTestingPinia({
   // stub actions
   pinia._p.push(({ store, options }) => {
     Object.keys(options.actions).forEach((action) => {
+      if (action === '$reset') return
       store[action] = stubActions ? createSpy() : createSpy(store[action])
     })
 
     store.$patch = stubPatch ? createSpy() : createSpy(store.$patch)
+    store.$reset = stubReset ? createSpy() : createSpy(store.$reset)
   })
 
   if (fakeApp) {