@@ -413,4 +413,52 @@ describe('scriptGoogleMapsOverlayView', () => {
413413 expect ( mocks . mockMap . panBy ) . not . toHaveBeenCalled ( )
414414 } )
415415 } )
416+
417+ describe ( 'unmount cleanup' , ( ) => {
418+ // Regression: https://github.com/nuxt/scripts/issues/735
419+ // `<ScriptGoogleMapsOverlayView v-if="x">` did not detach its overlay
420+ // element from the Google Maps pane on unmount, leaving a stale node
421+ // visible on the map. Cause: `onRemove()` read the anchor via
422+ // `useTemplateRef`, which Vue nulls during component unmount before
423+ // `onUnmounted` fires (and thus before `setMap(null)` triggers
424+ // `onRemove`). The fix captures the element at `onAdd` time.
425+ it ( 'detaches the anchor element from its pane when v-if toggles false' , async ( ) => {
426+ const mocks = createOverlayMocks ( )
427+ const Provider = createMapProvider ( mocks )
428+ const show = shallowRef ( true )
429+
430+ const wrapper = await mountSuspended ( Provider , {
431+ slots : {
432+ default : ( ) => ( show . value
433+ ? h (
434+ ScriptGoogleMapsOverlayView ,
435+ { position : { lat : 10 , lng : 20 } } ,
436+ ( ) => h ( 'div' , { class : 'overlay-content' } ) ,
437+ )
438+ : null ) ,
439+ } ,
440+ } )
441+
442+ await nextTick ( )
443+ await nextTick ( )
444+ await nextTick ( )
445+
446+ const overlayWrapper = wrapper . findComponent ( ScriptGoogleMapsOverlayView )
447+ const anchor = ( overlayWrapper . vm as any ) . $refs [ 'overlay-anchor' ] as HTMLElement
448+ expect ( anchor ) . toBeTruthy ( )
449+ // After onAdd, the anchor is reparented into a Google Maps pane, so it
450+ // has a parentNode that is not the component's hidden wrapper.
451+ expect ( anchor . parentNode ) . toBeTruthy ( )
452+ const paneBeforeUnmount = anchor . parentNode !
453+
454+ // Unmount the component via v-if. The cleanup must remove the anchor
455+ // from the pane so it does not linger on the map.
456+ show . value = false
457+ await wrapper . setProps ( { } )
458+ await nextTick ( )
459+ await nextTick ( )
460+
461+ expect ( paneBeforeUnmount . contains ( anchor ) ) . toBe ( false )
462+ } )
463+ } )
416464} )
0 commit comments