@@ -2,6 +2,8 @@ import { createPinia, defineStore, MutationType, setActivePinia } from '../src'
22import { mount } from '@vue/test-utils'
33import { nextTick } from 'vue'
44
5+ const delay = ( t : number ) => new Promise ( ( r ) => setTimeout ( r , t ) )
6+
57describe ( 'Subscriptions' , ( ) => {
68 const useStore = defineStore ( {
79 id : 'main' ,
@@ -200,6 +202,38 @@ describe('Subscriptions', () => {
200202 expect ( spy2 ) . toHaveBeenCalledTimes ( 1 )
201203 } )
202204
205+ it . skip ( 'triggers pre subscriptions only once on $patch' , async ( ) => {
206+ const s1 = useStore ( )
207+ const spy1 = jest . fn ( )
208+
209+ s1 . $subscribe ( spy1 , { flush : 'pre' } )
210+
211+ // First mutation: works as expected
212+ s1 . $patch ( { user : 'Edu' } )
213+ // anything else than awaiting a non promise or Promise.resolve() works
214+ await false
215+ // await Promise.resolve(false)
216+ // adding an extra await works
217+ // await false
218+ // adding any other delay also works
219+ // await delay(20)
220+ // await nextTick()
221+ expect ( spy1 ) . toHaveBeenCalledTimes ( 1 )
222+ expect ( spy1 ) . not . toHaveBeenCalledWith (
223+ expect . objectContaining ( { type : MutationType . direct } ) ,
224+ s1 . $state
225+ )
226+
227+ s1 . $patch ( { user : 'Myk' } )
228+ await nextTick ( )
229+
230+ expect ( spy1 ) . toHaveBeenCalledTimes ( 2 )
231+ expect ( spy1 ) . not . toHaveBeenCalledWith (
232+ expect . objectContaining ( { type : MutationType . direct } ) ,
233+ s1 . $state
234+ )
235+ } )
236+
203237 it ( 'removes on unmount' , async ( ) => {
204238 const pinia = createPinia ( )
205239 setActivePinia ( pinia )
0 commit comments