]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(testing): allow stubing $patch
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Jun 2021 10:37:17 +0000 (12:37 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Jun 2021 10:37:17 +0000 (12:37 +0200)
__tests__/testing.spec.ts
src/testing.ts

index e0ddab4a90bd48bb62be2872cd63d0275c112364..509295fdeace2bef582776560edff97f3a5e2a70 100644 (file)
@@ -62,7 +62,7 @@ describe('Testing', () => {
   })
 
   it('can execute actions', () => {
-    const { counter, wrapper } = factory({ bypassActions: false })
+    const { counter, wrapper } = factory({ stubActions: false })
 
     counter.increment()
     expect(counter.n).toBe(1)
@@ -84,4 +84,37 @@ describe('Testing', () => {
     expect(counter.increment).toHaveBeenCalledTimes(4)
     expect(counter.increment).toHaveBeenLastCalledWith(10)
   })
+
+  it('spies $patch calls', () => {
+    const { counter } = factory()
+
+    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 })
+    expect(counter.n).toBe(1)
+  })
+
+  it('can stub $patch calls', () => {
+    const { counter } = factory({ stubPatch: true })
+
+    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 })
+    expect(counter.n).toBe(0)
+  })
+
+  it('can stub $patch calls', () => {
+    const { counter } = factory({ stubPatch: true })
+
+    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 })
+    expect(counter.n).toBe(0)
+  })
 })
index 274642ac3eb7785069165b59d333638564baac88..4e060ee89599ff65adf278226fec77e708646afa 100644 (file)
@@ -12,7 +12,13 @@ export interface TestingOptions {
    * set to true, actions will be replaced with spies, resulting in their code
    * not being executed. Defaults to true.
    */
-  bypassActions?: boolean
+  stubActions?: boolean
+
+  /**
+   * When set to true, calls to `$patch()` won't change the state. Defaults to
+   * false.
+   */
+  stubPatch?: boolean
 
   createSpy?: (fn?: (...args: any[]) => any) => (...args: any[]) => any
 }
@@ -28,7 +34,7 @@ export interface TestingPinia extends Pinia {
  * Creates a pinia instance designed for unit tests that **requires mocking**
  * the stores. By default, **all actions are mocked** and therefore not
  * executed. This allows you to unit test your store and components separately.
- * You can change this with the `bypassActions` option. If you are using jest,
+ * You can change this with the `stubActions` option. If you are using jest,
  * they are replaced with `jest.fn()`, otherwise, you must provide your own
  * `createSpy` option.
  *
@@ -37,7 +43,8 @@ export interface TestingPinia extends Pinia {
  */
 export function createTestingPinia({
   plugins = [],
-  bypassActions = true,
+  stubActions = true,
+  stubPatch = false,
   createSpy,
 }: TestingOptions = {}): TestingPinia {
   const pinia = createPinia()
@@ -62,13 +69,15 @@ export function createTestingPinia({
     Object.keys(options.actions || {}).forEach((action) => {
       actionsCache[action] =
         actionsCache[action] ||
-        (bypassActions
+        (stubActions
           ? createSpy!()
           : // @ts-expect-error:
             createSpy!(store[action]))
       // @ts-expect-error:
       store[action] = actionsCache[action]
     })
+
+    store.$patch = stubPatch ? createSpy!() : createSpy!(store.$patch)
   })
 
   setActivePinia(pinia)