]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: add skipped test for #1129 [skip ci]
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 31 Mar 2022 16:08:03 +0000 (18:08 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 31 Mar 2022 16:08:03 +0000 (18:08 +0200)
packages/pinia/__tests__/subscriptions.spec.ts

index cacd8c2961511157ec7de05df197cfd5e6a851ee..6846f1ca16a707b5dbe74b8e88b41413adefaa28 100644 (file)
@@ -2,6 +2,8 @@ import { createPinia, defineStore, MutationType, setActivePinia } from '../src'
 import { mount } from '@vue/test-utils'
 import { nextTick } from 'vue'
 
+const delay = (t: number) => new Promise((r) => setTimeout(r, t))
+
 describe('Subscriptions', () => {
   const useStore = defineStore({
     id: 'main',
@@ -200,6 +202,38 @@ describe('Subscriptions', () => {
       expect(spy2).toHaveBeenCalledTimes(1)
     })
 
+    it.skip('triggers pre subscriptions only once on $patch', async () => {
+      const s1 = useStore()
+      const spy1 = jest.fn()
+
+      s1.$subscribe(spy1, { flush: 'pre' })
+
+      // First mutation: works as expected
+      s1.$patch({ user: 'Edu' })
+      // anything else than awaiting a non promise or Promise.resolve() works
+      await false
+      // await Promise.resolve(false)
+      // adding an extra await works
+      // await false
+      // adding any other delay also works
+      // await delay(20)
+      // await nextTick()
+      expect(spy1).toHaveBeenCalledTimes(1)
+      expect(spy1).not.toHaveBeenCalledWith(
+        expect.objectContaining({ type: MutationType.direct }),
+        s1.$state
+      )
+
+      s1.$patch({ user: 'Myk' })
+      await nextTick()
+
+      expect(spy1).toHaveBeenCalledTimes(2)
+      expect(spy1).not.toHaveBeenCalledWith(
+        expect.objectContaining({ type: MutationType.direct }),
+        s1.$state
+      )
+    })
+
     it('removes on unmount', async () => {
       const pinia = createPinia()
       setActivePinia(pinia)