@@ -18,7 +18,12 @@ import {
18
18
defineAsyncComponent ,
19
19
Component ,
20
20
createApp ,
21
- onActivated
21
+ onActivated ,
22
+ onUnmounted ,
23
+ onMounted ,
24
+ reactive ,
25
+ shallowRef ,
26
+ onDeactivated
22
27
} from '@vue/runtime-test'
23
28
import { KeepAliveProps } from '../../src/components/KeepAlive'
24
29
@@ -903,4 +908,73 @@ describe('KeepAlive', () => {
903
908
await nextTick ( )
904
909
expect ( handler ) . toHaveBeenCalledWith ( err , { } , 'activated hook' )
905
910
} )
911
+
912
+ // #3648
913
+ test ( 'should avoid unmount later included components' , async ( ) => {
914
+ const unmountedA = jest . fn ( )
915
+ const mountedA = jest . fn ( )
916
+ const activatedA = jest . fn ( )
917
+ const deactivatedA = jest . fn ( )
918
+ const unmountedB = jest . fn ( )
919
+ const mountedB = jest . fn ( )
920
+
921
+ const A = {
922
+ name : 'A' ,
923
+ setup ( ) {
924
+ onMounted ( mountedA )
925
+ onUnmounted ( unmountedA )
926
+ onActivated ( activatedA )
927
+ onDeactivated ( deactivatedA )
928
+ return ( ) => 'A'
929
+ }
930
+ }
931
+ const B = {
932
+ name : 'B' ,
933
+ setup ( ) {
934
+ onMounted ( mountedB )
935
+ onUnmounted ( unmountedB )
936
+ return ( ) => 'B'
937
+ }
938
+ }
939
+
940
+ const include = reactive < string [ ] > ( [ ] )
941
+ const current = shallowRef ( A )
942
+ const app = createApp ( {
943
+ setup ( ) {
944
+ return ( ) => {
945
+ return [
946
+ h (
947
+ KeepAlive ,
948
+ {
949
+ include
950
+ } ,
951
+ h ( current . value )
952
+ )
953
+ ]
954
+ }
955
+ }
956
+ } )
957
+
958
+ app . mount ( root )
959
+
960
+ expect ( serializeInner ( root ) ) . toBe ( `A` )
961
+ expect ( mountedA ) . toHaveBeenCalledTimes ( 1 )
962
+ expect ( unmountedA ) . toHaveBeenCalledTimes ( 0 )
963
+ expect ( activatedA ) . toHaveBeenCalledTimes ( 0 )
964
+ expect ( deactivatedA ) . toHaveBeenCalledTimes ( 0 )
965
+ expect ( mountedB ) . toHaveBeenCalledTimes ( 0 )
966
+ expect ( unmountedB ) . toHaveBeenCalledTimes ( 0 )
967
+
968
+ include . push ( 'A' ) // cache A
969
+ await nextTick ( )
970
+ current . value = B // toggle to B
971
+ await nextTick ( )
972
+ expect ( serializeInner ( root ) ) . toBe ( `B` )
973
+ expect ( mountedA ) . toHaveBeenCalledTimes ( 1 )
974
+ expect ( unmountedA ) . toHaveBeenCalledTimes ( 0 )
975
+ expect ( activatedA ) . toHaveBeenCalledTimes ( 0 )
976
+ expect ( deactivatedA ) . toHaveBeenCalledTimes ( 1 )
977
+ expect ( mountedB ) . toHaveBeenCalledTimes ( 1 )
978
+ expect ( unmountedB ) . toHaveBeenCalledTimes ( 0 )
979
+ } )
906
980
} )
0 commit comments