From: Eduardo San Martin Morote Date: Mon, 25 Apr 2022 13:06:08 +0000 (+0200) Subject: fix(testing): correct order of plugin installation X-Git-Tag: @pinia/nuxt@0.1.9~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0f789fe1591ef8d2d10a8616c7abac8ad09cdf98;p=thirdparty%2Fvuejs%2Fpinia.git fix(testing): correct order of plugin installation --- diff --git a/packages/testing/src/testing.spec.ts b/packages/testing/src/testing.spec.ts index b46ec82e..4b0b4364 100644 --- a/packages/testing/src/testing.spec.ts +++ b/packages/testing/src/testing.spec.ts @@ -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) + }) }) diff --git a/packages/testing/src/testing.ts b/packages/testing/src/testing.ts index 1a2a6d64..11f85f41 100644 --- a/packages/testing/src/testing.ts +++ b/packages/testing/src/testing.ts @@ -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 ||