From 10bef8ab2fa951fbaab0afe38b58a1396f23db8b Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 28 Jun 2021 12:37:17 +0200 Subject: [PATCH] feat(testing): allow stubing $patch --- __tests__/testing.spec.ts | 35 ++++++++++++++++++++++++++++++++++- src/testing.ts | 17 +++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/__tests__/testing.spec.ts b/__tests__/testing.spec.ts index e0ddab4a..509295fd 100644 --- a/__tests__/testing.spec.ts +++ b/__tests__/testing.spec.ts @@ -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) + }) }) diff --git a/src/testing.ts b/src/testing.ts index 274642ac..4e060ee8 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -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) -- 2.47.2