@@ -339,3 +339,63 @@ test("should not trigger on untrusted events when 'trusted' option is true", asy
339339 const triggered = await page . evaluate ( ( ) => window . hotkeyTriggered ) ;
340340 expect ( triggered ) . toBe ( false ) ;
341341} ) ;
342+
343+ test ( "should not be consumed by non-matching keys when 'once' option is set" , async ( { page } ) => {
344+ await page . evaluate ( ( ) => {
345+ window . hotkeyCount = 0 ;
346+ const el = document . getElementById ( "text" ) ;
347+
348+ window . registerHotkey ( el , "Ctrl + K" , ( ) => {
349+ window . hotkeyCount ++ ;
350+ } , "keydown" , { once : true } ) ;
351+ } ) ;
352+
353+ await page . locator ( "#text" ) . focus ( ) ;
354+
355+ await page . keyboard . press ( "A" ) ;
356+ await page . keyboard . press ( "B" ) ;
357+
358+ await page . keyboard . press ( "Control+k" ) ;
359+
360+ const count = await page . evaluate ( ( ) => window . hotkeyCount ) ;
361+ expect ( count ) . toBe ( 1 ) ;
362+ } ) ;
363+
364+ test ( "should trigger only once with 'once' option" , async ( { page } ) => {
365+ await page . evaluate ( ( ) => {
366+ window . hotkeyCount = 0 ;
367+ const el = document . getElementById ( "text" ) ;
368+
369+ window . registerHotkey ( el , "Ctrl + K" , ( ) => {
370+ window . hotkeyCount ++ ;
371+ } , "keydown" , { once : true } ) ;
372+ } ) ;
373+
374+ await page . locator ( "#text" ) . focus ( ) ;
375+
376+ // Press the hotkey twice
377+ await page . keyboard . press ( "Control+k" ) ;
378+ await page . keyboard . press ( "Control+k" ) ;
379+
380+ const count = await page . evaluate ( ( ) => window . hotkeyCount ) ;
381+ expect ( count ) . toBe ( 1 ) ;
382+ } ) ;
383+
384+ test ( "should trigger multiple times without 'once' option" , async ( { page } ) => {
385+ await page . evaluate ( ( ) => {
386+ window . hotkeyCount = 0 ;
387+ const el = document . getElementById ( "text" ) ;
388+
389+ window . registerHotkey ( el , "Ctrl + K" , ( ) => {
390+ window . hotkeyCount ++ ;
391+ } ) ;
392+ } ) ;
393+
394+ await page . locator ( "#text" ) . focus ( ) ;
395+
396+ await page . keyboard . press ( "Control+k" ) ;
397+ await page . keyboard . press ( "Control+k" ) ;
398+
399+ const count = await page . evaluate ( ( ) => window . hotkeyCount ) ;
400+ expect ( count ) . toBe ( 2 ) ;
401+ } ) ;
0 commit comments