@@ -394,4 +394,139 @@ describe('runtime-dom: v-show directive', () => {
394394 await nextTick ( )
395395 expect ( $div . style . display ) . toEqual ( '' )
396396 } )
397+
398+ describe ( 'stale enter callbacks (with Transition)' , ( ) => {
399+ beforeEach ( ( ) => {
400+ vi . useFakeTimers ( { toFake : [ 'setTimeout' , 'requestAnimationFrame' ] } )
401+ } )
402+
403+ afterEach ( ( ) => {
404+ vi . useRealTimers ( )
405+ } )
406+
407+ function mount ( template : string , setupState : Record < string , any > = { } ) {
408+ const visible = ref ( false )
409+ const { code } = compile ( template , { prefixIdentifiers : true } )
410+ const Component = {
411+ setup : ( ) => ( { visible, ...setupState } ) ,
412+ render : new Function ( 'Vue' , code ) ( runtimeDom ) ,
413+ }
414+ render ( h ( Component ) , root )
415+ return { visible, el : root . children [ 0 ] as HTMLElement }
416+ }
417+
418+ test ( 'should not add enter-to class when hidden before the enter frame' , async ( ) => {
419+ const { visible, el } = mount (
420+ `<Transition name="test" :duration="100">
421+ <div v-show="visible" />
422+ </Transition>` ,
423+ )
424+
425+ visible . value = true
426+ await nextTick ( )
427+ visible . value = false
428+ await nextTick ( )
429+ vi . advanceTimersByTime ( 500 )
430+ expect ( el . style . display ) . toBe ( 'none' )
431+ expect ( el . className ) . toBe ( '' )
432+ } )
433+
434+ test ( 'should not add enter-to class after the enter hook calls done' , async ( ) => {
435+ const afterEnter = vi . fn ( )
436+ const { visible, el } = mount (
437+ `<Transition name="test" @enter="onEnter" @after-enter="afterEnter">
438+ <div v-show="visible" />
439+ </Transition>` ,
440+ {
441+ onEnter : ( _el : Element , done : ( ) => void ) => done ( ) ,
442+ afterEnter,
443+ } ,
444+ )
445+
446+ visible . value = true
447+ await nextTick ( )
448+ vi . advanceTimersByTime ( 500 )
449+ expect ( afterEnter ) . toHaveBeenCalledTimes ( 1 )
450+ expect ( el . style . display ) . toBe ( '' )
451+ expect ( el . className ) . toBe ( '' )
452+ } )
453+
454+ test ( 'should not let the timeout of a cancelled enter end the next enter' , async ( ) => {
455+ const afterEnter = vi . fn ( )
456+ const { visible, el } = mount (
457+ `<Transition name="test" :duration="100" @after-enter="afterEnter">
458+ <div v-show="visible" />
459+ </Transition>` ,
460+ { afterEnter } ,
461+ )
462+
463+ visible . value = true
464+ await nextTick ( )
465+ // the enter frame has run, its timeout is still pending
466+ vi . advanceTimersByTime ( 120 )
467+ visible . value = false
468+ await nextTick ( )
469+ visible . value = true
470+ await nextTick ( )
471+ // the earlier timeout fires before the frame of the new enter
472+ vi . advanceTimersByTime ( 20 )
473+ expect ( el . classList . contains ( 'test-enter-active' ) ) . toBe ( true )
474+ expect ( afterEnter ) . not . toHaveBeenCalled ( )
475+
476+ vi . advanceTimersByTime ( 500 )
477+ expect ( afterEnter ) . toHaveBeenCalledTimes ( 1 )
478+ expect ( el . className ) . toBe ( '' )
479+ } )
480+
481+ test ( 'should not let the timeout of a cancelled enter remove shared leave classes' , async ( ) => {
482+ const afterLeave = vi . fn ( )
483+ const { visible, el } = mount (
484+ `<Transition
485+ :duration="100"
486+ enter-active-class="fade"
487+ leave-active-class="fade"
488+ @after-leave="afterLeave"
489+ >
490+ <div v-show="visible" />
491+ </Transition>` ,
492+ { afterLeave } ,
493+ )
494+
495+ visible . value = true
496+ await nextTick ( )
497+ // the enter frame has run, its timeout is still pending
498+ vi . advanceTimersByTime ( 120 )
499+ visible . value = false
500+ await nextTick ( )
501+ // the earlier timeout fires before the frame of the leave
502+ vi . advanceTimersByTime ( 20 )
503+ expect ( el . classList . contains ( 'fade' ) ) . toBe ( true )
504+
505+ vi . advanceTimersByTime ( 500 )
506+ expect ( afterLeave ) . toHaveBeenCalledTimes ( 1 )
507+ expect ( el . style . display ) . toBe ( 'none' )
508+ expect ( el . className ) . toBe ( '' )
509+ } )
510+
511+ test ( 'should finish a re-enter started before the frame of a cancelled enter' , async ( ) => {
512+ const afterEnter = vi . fn ( )
513+ const { visible, el } = mount (
514+ `<Transition name="test" @after-enter="afterEnter">
515+ <div v-show="visible" />
516+ </Transition>` ,
517+ { afterEnter } ,
518+ )
519+
520+ visible . value = true
521+ await nextTick ( )
522+ visible . value = false
523+ await nextTick ( )
524+ visible . value = true
525+ await nextTick ( )
526+ vi . advanceTimersByTime ( 500 )
527+ expect ( afterEnter ) . toHaveBeenCalledTimes ( 1 )
528+ expect ( el . style . display ) . toBe ( '' )
529+ expect ( el . className ) . toBe ( '' )
530+ } )
531+ } )
397532} )
0 commit comments