]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: sync mutation + $subscribe
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 Nov 2025 10:25:34 +0000 (11:25 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 Nov 2025 10:25:34 +0000 (11:25 +0100)
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

packages/pinia/__tests__/subscriptions.spec.ts

index 1c280a52493b35cef3b45d4b5bc31e16a279bfed..782871b58672010ac788b089d3bf567cbb2f1056 100644 (file)
@@ -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)
+    })
   })
 })