@@ -232,4 +232,41 @@ describe("createOverlay", () => {
232232 } ) ;
233233 showOverlayMock . mockRestore ( ) ;
234234 } ) ;
235+ // ESC key test cases
236+
237+ it ( "should dismiss overlay when ESC key is pressed" , ( ) => {
238+ const options = { trustedTypesPolicyName : null , catchRuntimeError : true } ;
239+ const overlay = createOverlay ( options ) ;
240+ const showOverlayMock = jest . spyOn ( overlay , "send" ) ;
241+
242+ const escEvent = new KeyboardEvent ( "keydown" , { key : "Escape" } ) ;
243+ globalThis . window . dispatchEvent ( escEvent ) ;
244+
245+ expect ( showOverlayMock ) . toHaveBeenCalledWith ( { type : "DISMISS" } ) ;
246+ showOverlayMock . mockRestore ( ) ;
247+ } ) ;
248+
249+ it ( "should dismiss overlay when 'Esc' key is pressed (older browsers)" , ( ) => {
250+ const options = { trustedTypesPolicyName : null , catchRuntimeError : true } ;
251+ const overlay = createOverlay ( options ) ;
252+ const showOverlayMock = jest . spyOn ( overlay , "send" ) ;
253+
254+ const escEvent = new KeyboardEvent ( "keydown" , { key : "Esc" } ) ;
255+ globalThis . window . dispatchEvent ( escEvent ) ;
256+
257+ expect ( showOverlayMock ) . toHaveBeenCalledWith ( { type : "DISMISS" } ) ;
258+ showOverlayMock . mockRestore ( ) ;
259+ } ) ;
260+
261+ it ( "should not dismiss overlay for other keys" , ( ) => {
262+ const options = { trustedTypesPolicyName : null , catchRuntimeError : true } ;
263+ const overlay = createOverlay ( options ) ;
264+ const showOverlayMock = jest . spyOn ( overlay , "send" ) ;
265+
266+ const otherKeyEvent = new KeyboardEvent ( "keydown" , { key : "Enter" } ) ;
267+ globalThis . window . dispatchEvent ( otherKeyEvent ) ;
268+
269+ expect ( showOverlayMock ) . not . toHaveBeenCalled ( ) ;
270+ showOverlayMock . mockRestore ( ) ;
271+ } ) ;
235272} ) ;
0 commit comments