]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(testing): correct order of plugin installation
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 25 Apr 2022 13:06:08 +0000 (15:06 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 25 Apr 2022 13:06:08 +0000 (15:06 +0200)
packages/testing/src/testing.spec.ts
packages/testing/src/testing.ts

index b46ec82e659307ec68501a75c03868b0783d92bc..4b0b4364624fb01271bb22f1124c28f9304a0272 100644 (file)
@@ -224,4 +224,61 @@ describe('Testing', () => {
     store.n++
     expect(store.double).toBe(6)
   })
+
+  it('actions are stubbed even when replaced by other plugins', () => {
+    const spy = jest.fn()
+    mount(Counter, {
+      global: {
+        plugins: [
+          createTestingPinia({
+            plugins: [
+              ({ store }) => {
+                const { increment } = store.increment
+                store.increment = spy
+                spy.mockImplementation(increment)
+              },
+            ],
+          }),
+        ],
+      },
+    })
+    const counter = useCounter()
+
+    counter.increment()
+    counter.increment(5)
+    expect(counter.n).toBe(0)
+    expect(counter.increment).toHaveBeenCalledTimes(2)
+    expect(counter.increment).toHaveBeenLastCalledWith(5)
+    // the actual spy is never called because we stub the action
+    expect(spy).toHaveBeenCalledTimes(0)
+  })
+
+  it('pass through replaced actions in plugins', () => {
+    const spy = jest.fn()
+    mount(Counter, {
+      global: {
+        plugins: [
+          createTestingPinia({
+            stubActions: false,
+            plugins: [
+              ({ store }) => {
+                const { increment } = store.increment
+                store.increment = spy
+                spy.mockImplementation(increment)
+              },
+            ],
+          }),
+        ],
+      },
+    })
+    const counter = useCounter()
+
+    counter.increment()
+    counter.increment(5)
+    expect(counter.n).toBe(0)
+    expect(counter.increment).toHaveBeenCalledTimes(2)
+    expect(counter.increment).toHaveBeenLastCalledWith(5)
+    expect(spy).toHaveBeenCalledTimes(2)
+    expect(spy).toHaveBeenLastCalledWith(5)
+  })
 })
index 1a2a6d64d033ad787006882b914a5eda348c09e3..11f85f4199726ca58f14054c996db4b480c59657 100644 (file)
@@ -109,7 +109,8 @@ export function createTestingPinia({
   // allow computed to be manually overridden
   pinia._p.push(WritableComputed)
 
-  plugins.forEach((plugin) => pinia.use(plugin))
+  // bypass waiting for the app to be installed to ensure the action stubbing happens last
+  plugins.forEach((plugin) => pinia._p.push(plugin))
 
   const createSpy =
     _createSpy ||