Skip to content

Commit a64ef01

Browse files
committed
Make specs work under IE 11
1 parent c673d36 commit a64ef01

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

specs/Modal.spec.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,30 @@ describe('Modal', () => {
270270
unmountModal();
271271
});
272272

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+
273297
describe('should close on overlay click', () => {
274298
afterEach('Unmount modal', () => {
275299
unmountModal();
@@ -365,7 +389,15 @@ describe('Modal', () => {
365389
});
366390
const overlay = TestUtils.scryRenderedDOMComponentsWithClass(modal.portal, 'ReactModal__Overlay');
367391
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);
369401
expect(hasPropagated).toBeTruthy();
370402
});
371403
});
@@ -380,34 +412,19 @@ describe('Modal', () => {
380412
expect(modal.props.isOpen).toEqual(true);
381413
const overlay = TestUtils.scryRenderedDOMComponentsWithClass(modal.portal, 'ReactModal__Overlay');
382414
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+
});
385421
expect(requestCloseCallback.called).toBeTruthy();
386422
// Check if event is passed to onRequestClose callback.
387423
const event = requestCloseCallback.getCall(0).args[0];
388424
expect(event).toBeTruthy();
389425
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');
400427
});
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');
411428
});
412429

413430
// it('adds --before-close for animations', function() {

0 commit comments

Comments
 (0)