@@ -270,6 +270,30 @@ describe('Modal', () => {
270
270
unmountModal ( ) ;
271
271
} ) ;
272
272
273
+ it ( 'should close on Esc key event' , ( ) => {
274
+ const requestCloseCallback = sinon . spy ( ) ;
275
+ const modal = renderModal ( {
276
+ isOpen : true ,
277
+ shouldCloseOnOverlayClick : true ,
278
+ onRequestClose : requestCloseCallback
279
+ } ) ;
280
+ expect ( modal . props . isOpen ) . toEqual ( true ) ;
281
+ expect ( ( ) => {
282
+ Simulate . keyDown ( modal . portal . content , {
283
+ // The keyCode is all that matters, so this works
284
+ key : 'FakeKeyToTestLater' ,
285
+ keyCode : 27 ,
286
+ which : 27
287
+ } ) ;
288
+ } ) . toNotThrow ( ) ;
289
+ expect ( requestCloseCallback . called ) . toBeTruthy ( ) ;
290
+ // Check if event is passed to onRequestClose callback.
291
+ const event = requestCloseCallback . getCall ( 0 ) . args [ 0 ] ;
292
+ expect ( event ) . toBeTruthy ( ) ;
293
+ expect ( event . constructor ) . toBeTruthy ( ) ;
294
+ expect ( event . key ) . toEqual ( 'FakeKeyToTestLater' ) ;
295
+ } ) ;
296
+
273
297
describe ( 'should close on overlay click' , ( ) => {
274
298
afterEach ( 'Unmount modal' , ( ) => {
275
299
unmountModal ( ) ;
@@ -365,7 +389,15 @@ describe('Modal', () => {
365
389
} ) ;
366
390
const overlay = TestUtils . scryRenderedDOMComponentsWithClass ( modal . portal , 'ReactModal__Overlay' ) ;
367
391
window . addEventListener ( 'click' , ( ) => { hasPropagated = true ; } ) ;
368
- overlay [ 0 ] . dispatchEvent ( new MouseEvent ( 'click' , { bubbles : true } ) ) ;
392
+ // Work around for running the spec in IE 11
393
+ let mouseEvent = null ;
394
+ try {
395
+ mouseEvent = new MouseEvent ( 'click' , { bubbles : true } ) ;
396
+ } catch ( err ) {
397
+ mouseEvent = document . createEvent ( 'MouseEvent' ) ;
398
+ mouseEvent . initEvent ( 'click' , true , false ) ;
399
+ }
400
+ overlay [ 0 ] . dispatchEvent ( mouseEvent ) ;
369
401
expect ( hasPropagated ) . toBeTruthy ( ) ;
370
402
} ) ;
371
403
} ) ;
@@ -380,34 +412,19 @@ describe('Modal', () => {
380
412
expect ( modal . props . isOpen ) . toEqual ( true ) ;
381
413
const overlay = TestUtils . scryRenderedDOMComponentsWithClass ( modal . portal , 'ReactModal__Overlay' ) ;
382
414
expect ( overlay . length ) . toEqual ( 1 ) ;
383
- Simulate . mouseDown ( overlay [ 0 ] ) ; // click the overlay
384
- Simulate . mouseUp ( overlay [ 0 ] ) ;
415
+ // click the overlay
416
+ Simulate . mouseDown ( overlay [ 0 ] ) ;
417
+ Simulate . mouseUp ( overlay [ 0 ] , {
418
+ // Used to test that this was the event received
419
+ fakeData : 'ABC'
420
+ } ) ;
385
421
expect ( requestCloseCallback . called ) . toBeTruthy ( ) ;
386
422
// Check if event is passed to onRequestClose callback.
387
423
const event = requestCloseCallback . getCall ( 0 ) . args [ 0 ] ;
388
424
expect ( event ) . toBeTruthy ( ) ;
389
425
expect ( event . constructor ) . toBeTruthy ( ) ;
390
- expect ( event . constructor . name ) . toEqual ( 'SyntheticEvent' ) ;
391
- } ) ;
392
- } ) ;
393
-
394
- it ( 'should close on Esc key event' , ( ) => {
395
- const requestCloseCallback = sinon . spy ( ) ;
396
- const modal = renderModal ( {
397
- isOpen : true ,
398
- shouldCloseOnOverlayClick : true ,
399
- onRequestClose : requestCloseCallback
426
+ expect ( event . fakeData ) . toEqual ( 'ABC' ) ;
400
427
} ) ;
401
- expect ( modal . props . isOpen ) . toEqual ( true ) ;
402
- expect ( ( ) => {
403
- Simulate . keyDown ( modal . portal . content , { key : 'Esc' , keyCode : 27 , which : 27 } ) ;
404
- } ) . toNotThrow ( ) ;
405
- expect ( requestCloseCallback . called ) . toBeTruthy ( ) ;
406
- // Check if event is passed to onRequestClose callback.
407
- const event = requestCloseCallback . getCall ( 0 ) . args [ 0 ] ;
408
- expect ( event ) . toBeTruthy ( ) ;
409
- expect ( event . constructor ) . toBeTruthy ( ) ;
410
- expect ( event . constructor . name ) . toEqual ( 'SyntheticEvent' ) ;
411
428
} ) ;
412
429
413
430
// it('adds --before-close for animations', function() {
0 commit comments