From: Eduardo San Martin Morote Date: Mon, 3 Nov 2025 10:25:34 +0000 (+0100) Subject: test: sync mutation + $subscribe X-Git-Tag: @pinia/nuxt@0.11.3~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1b5c8320d5306722d464488346f3645f3ce3806d;p=thirdparty%2Fvuejs%2Fpinia.git test: sync mutation + $subscribe Extracted from https://github.com/vuejs/pinia/pull/2870 to fix https://github.com/vuejs/pinia/issues/992 but can't be merged because the sync flush introduced a perf regression --- diff --git a/packages/pinia/__tests__/subscriptions.spec.ts b/packages/pinia/__tests__/subscriptions.spec.ts index 1c280a52..782871b5 100644 --- a/packages/pinia/__tests__/subscriptions.spec.ts +++ b/packages/pinia/__tests__/subscriptions.spec.ts @@ -353,5 +353,27 @@ describe('Subscriptions', () => { expect(spy1).toHaveBeenCalledTimes(1) expect(spy2).toHaveBeenCalledTimes(2) }) + + // https://github.com/vuejs/pinia/issues/992 + it('triggers sync subscription when state is synchronously mutated after patch', async () => { + const store = useStore() + const syncSpy = vi.fn() + const preSpy = vi.fn() + const postSpy = vi.fn() + store.$subscribe(syncSpy, { flush: 'sync' }) + store.$subscribe(preSpy, { flush: 'pre' }) + store.$subscribe(postSpy, { flush: 'post' }) + + store.$patch({ user: 'Edu' }) + store.user = 'a' + expect(syncSpy).toHaveBeenCalledTimes(2) + + // FIXME: ideally, these should be 2 but we cannot use + // a sync flush within the store's $subscribe method + // https://github.com/vuejs/pinia/issues/610 + await nextTick() + expect(preSpy).toHaveBeenCalledTimes(1) + expect(postSpy).toHaveBeenCalledTimes(1) + }) }) })