11import { describe , expect , it , vi } from "vite-plus/test" ;
2+ import { fakeTimers } from "../../test/render" ;
23import { observeSelectionActions } from "./selectionActions" ;
34
45describe ( "observeSelectionActions" , ( ) => {
@@ -25,4 +26,37 @@ describe("observeSelectionActions", () => {
2526
2627 await vi . waitFor ( ( ) => expect ( onSelection ) . toHaveBeenCalledWith ( null ) ) ;
2728 } ) ;
29+
30+ it ( "keeps a dismissed touch selection hidden when the viewport scrolls" , ( ) => {
31+ const element = document . createElement ( "div" ) ;
32+ element . textContent = "Select this text on a phone" ;
33+ document . body . append ( element ) ;
34+ const onSelection = vi . fn ( ) ;
35+ const actions = observeSelectionActions ( {
36+ element,
37+ restoreAfterScroll : true ,
38+ onSelection,
39+ onDismiss : vi . fn ( ) ,
40+ } ) ;
41+ using _timers = fakeTimers ( ) ;
42+ using _cleanup = {
43+ [ Symbol . dispose ] : ( ) => {
44+ actions . dispose ( ) ;
45+ element . remove ( ) ;
46+ } ,
47+ } ;
48+
49+ const touch = { bubbles : true , isPrimary : true , pointerType : "touch" , button : 0 } ;
50+ element . dispatchEvent ( new PointerEvent ( "pointerdown" , touch ) ) ;
51+ window . dispatchEvent ( new PointerEvent ( "pointercancel" , touch ) ) ;
52+ actions . selectionChanged ( ) ;
53+ vi . advanceTimersByTime ( 1_000 ) ;
54+ expect ( onSelection ) . toHaveBeenCalledTimes ( 1 ) ;
55+
56+ document . dispatchEvent ( new KeyboardEvent ( "keydown" , { key : "Escape" } ) ) ;
57+ element . dispatchEvent ( new Event ( "scroll" ) ) ;
58+ actions . selectionChanged ( ) ;
59+ vi . advanceTimersByTime ( 1_000 ) ;
60+ expect ( onSelection ) . toHaveBeenCalledTimes ( 1 ) ;
61+ } ) ;
2862} ) ;
0 commit comments