From: Eduardo San Martin Morote Date: Mon, 8 May 2023 15:03:44 +0000 (+0200) Subject: feat(testing): allow mocking $reset X-Git-Tag: @pinia/nuxt@0.4.10~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f526a33ab0ac441fe865344977a11e0e471ce17;p=thirdparty%2Fvuejs%2Fpinia.git feat(testing): allow mocking $reset Fix #2188 --- diff --git a/packages/testing/src/testing.spec.ts b/packages/testing/src/testing.spec.ts index 307cc901..ba701974 100644 --- a/packages/testing/src/testing.spec.ts +++ b/packages/testing/src/testing.spec.ts @@ -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: ` + + {{ counter.n }} + + `, + }) + + 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) }) diff --git a/packages/testing/src/testing.ts b/packages/testing/src/testing.ts index b709bb6b..a3e169ab 100644 --- a/packages/testing/src/testing.ts +++ b/packages/testing/src/testing.ts @@ -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) {