Skip to content

Commit 1b5c832

Browse files
committed
test: sync mutation + $subscribe
Extracted from #2870 to fix #992 but can't be merged because the sync flush introduced a perf regression
1 parent 1ea0440 commit 1b5c832

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/pinia/__tests__/subscriptions.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,5 +353,27 @@ describe('Subscriptions', () => {
353353
expect(spy1).toHaveBeenCalledTimes(1)
354354
expect(spy2).toHaveBeenCalledTimes(2)
355355
})
356+
357+
// https://github.com/vuejs/pinia/issues/992
358+
it('triggers sync subscription when state is synchronously mutated after patch', async () => {
359+
const store = useStore()
360+
const syncSpy = vi.fn()
361+
const preSpy = vi.fn()
362+
const postSpy = vi.fn()
363+
store.$subscribe(syncSpy, { flush: 'sync' })
364+
store.$subscribe(preSpy, { flush: 'pre' })
365+
store.$subscribe(postSpy, { flush: 'post' })
366+
367+
store.$patch({ user: 'Edu' })
368+
store.user = 'a'
369+
expect(syncSpy).toHaveBeenCalledTimes(2)
370+
371+
// FIXME: ideally, these should be 2 but we cannot use
372+
// a sync flush within the store's $subscribe method
373+
// https://github.com/vuejs/pinia/issues/610
374+
await nextTick()
375+
expect(preSpy).toHaveBeenCalledTimes(1)
376+
expect(postSpy).toHaveBeenCalledTimes(1)
377+
})
356378
})
357379
})

0 commit comments

Comments
 (0)