Skip to content

Commit 3bc4719

Browse files
committed
[chore] refactoring tests...
* separate tests for events, styles and state. * added helpers to find dom nodes, dispatch events... * simplify assertitions.
1 parent fb89f8b commit 3bc4719

File tree

4 files changed

+443
-331
lines changed

4 files changed

+443
-331
lines changed

specs/Modal.events.spec.js

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import sinon from 'sinon';
2+
import expect from 'expect';
3+
import React from 'react';
4+
import ReactDOM from 'react-dom';
5+
import TestUtils from 'react-addons-test-utils';
6+
import Modal from '../lib/components/Modal';
7+
import * as ariaAppHider from '../lib/helpers/ariaAppHider';
8+
import {
9+
isBodyWithReactModalOpenClass, findDOMWithClass,
10+
contentAttribute, overlayAttribute,
11+
moverlay, mcontent,
12+
clickAt, mouseDownAt, mouseUpAt, escKeyDown, tabKeyDown,
13+
renderModal, unmountModal, emptyDOM
14+
} from './helper';
15+
16+
describe('Events', () => {
17+
afterEach('Unmount modal', emptyDOM);
18+
19+
it('should trigger the onAfterOpen callback', () => {
20+
var afterOpenCallback = sinon.spy();
21+
renderModal({ isOpen: true, onAfterOpen: afterOpenCallback });
22+
expect(afterOpenCallback.called).toBeTruthy();
23+
});
24+
25+
it('keeps focus inside the modal when child has no tabbable elements', () => {
26+
var tabPrevented = false;
27+
var modal = renderModal({ isOpen: true }, 'hello');
28+
const content = mcontent(modal);
29+
expect(document.activeElement).toEqual(content);
30+
tabKeyDown(content, {
31+
preventDefault: function() { tabPrevented = true; }
32+
});
33+
expect(tabPrevented).toEqual(true);
34+
});
35+
36+
it('handles case when child has no tabbable elements', () => {
37+
var modal = renderModal({ isOpen: true }, 'hello');
38+
const content = mcontent(modal);
39+
tabKeyDown(content);
40+
expect(document.activeElement).toEqual(content);
41+
});
42+
43+
it('should close on Esc key event', () => {
44+
var requestCloseCallback = sinon.spy();
45+
var modal = renderModal({
46+
isOpen: true,
47+
shouldCloseOnOverlayClick: true,
48+
onRequestClose: requestCloseCallback
49+
});
50+
escKeyDown(mcontent(modal));
51+
expect(requestCloseCallback.called).toBeTruthy();
52+
// Check if event is passed to onRequestClose callback.
53+
var event = requestCloseCallback.getCall(0).args[0];
54+
expect(event).toExist();
55+
});
56+
57+
it('verify overlay click when shouldCloseOnOverlayClick sets to false', () => {
58+
const requestCloseCallback = sinon.spy();
59+
const modal = renderModal({
60+
isOpen: true,
61+
shouldCloseOnOverlayClick: false
62+
});
63+
var overlay = moverlay(modal);
64+
clickAt(overlay);
65+
expect(!requestCloseCallback.called).toBeTruthy();
66+
});
67+
68+
it('verify overlay click when shouldCloseOnOverlayClick sets to true', () => {
69+
var requestCloseCallback = sinon.spy();
70+
var modal = renderModal({
71+
isOpen: true,
72+
shouldCloseOnOverlayClick: true,
73+
onRequestClose: function() {
74+
requestCloseCallback();
75+
}
76+
});
77+
clickAt(moverlay(modal));
78+
expect(requestCloseCallback.called).toBeTruthy();
79+
});
80+
81+
it('verify overlay mouse down and content mouse up when shouldCloseOnOverlayClick sets to true', () => {
82+
const requestCloseCallback = sinon.spy();
83+
const modal = renderModal({
84+
isOpen: true,
85+
shouldCloseOnOverlayClick: true,
86+
onRequestClose: requestCloseCallback
87+
});
88+
mouseDownAt(moverlay(modal));
89+
mouseUpAt(mcontent(modal));
90+
expect(!requestCloseCallback.called).toBeTruthy();
91+
});
92+
93+
it('verify content mouse down and overlay mouse up when shouldCloseOnOverlayClick sets to true', () => {
94+
var requestCloseCallback = sinon.spy();
95+
var modal = renderModal({
96+
isOpen: true,
97+
shouldCloseOnOverlayClick: true,
98+
onRequestClose: function() {
99+
requestCloseCallback();
100+
}
101+
});
102+
mouseDownAt(mcontent(modal));
103+
mouseUpAt(moverlay(modal));
104+
expect(!requestCloseCallback.called).toBeTruthy();
105+
});
106+
107+
it('should not stop event propagation', () => {
108+
var hasPropagated = false;
109+
var modal = renderModal({
110+
isOpen: true,
111+
shouldCloseOnOverlayClick: true
112+
});
113+
window.addEventListener('click', () => {
114+
hasPropagated = true;
115+
});
116+
moverlay(modal).dispatchEvent(new MouseEvent('click', { bubbles: true }));
117+
expect(hasPropagated).toBeTruthy();
118+
});
119+
120+
it('verify event passing on overlay click', () => {
121+
var requestCloseCallback = sinon.spy();
122+
var modal = renderModal({
123+
isOpen: true,
124+
shouldCloseOnOverlayClick: true,
125+
onRequestClose: requestCloseCallback
126+
});
127+
// click the overlay
128+
clickAt(moverlay(modal), {
129+
// Used to test that this was the event received
130+
fakeData: 'ABC'
131+
});
132+
expect(requestCloseCallback.called).toBeTruthy();
133+
// Check if event is passed to onRequestClose callback.
134+
var event = requestCloseCallback.getCall(0).args[0];
135+
expect(event).toExist();
136+
});
137+
});

0 commit comments

Comments
 (0)