@@ -11,74 +11,71 @@ import {
11
11
mouseUpAt ,
12
12
escKeyDown ,
13
13
tabKeyDown ,
14
- renderModal ,
15
- emptyDOM ,
16
- unmountModal
14
+ withModal
17
15
} from "./helper" ;
18
16
19
17
export default ( ) => {
20
- afterEach ( "Unmount modal" , emptyDOM ) ;
21
-
22
18
it ( "should trigger the onAfterOpen callback" , ( ) => {
23
19
const afterOpenCallback = sinon . spy ( ) ;
24
- renderModal ( { isOpen : true , onAfterOpen : afterOpenCallback } ) ;
20
+ const props = { isOpen : true , onAfterOpen : afterOpenCallback } ;
21
+ withModal ( props , null , ( ) => { } ) ;
25
22
afterOpenCallback . called . should . be . ok ( ) ;
26
23
} ) ;
27
24
28
25
it ( "should call onAfterOpen with overlay and content references" , ( ) => {
29
26
const afterOpenCallback = sinon . spy ( ) ;
30
- const modal = renderModal ( { isOpen : true , onAfterOpen : afterOpenCallback } ) ;
31
-
32
- sinon . assert . calledWith ( afterOpenCallback , {
33
- overlayEl : modal . portal . overlay ,
34
- contentEl : modal . portal . content
27
+ const props = { isOpen : true , onAfterOpen : afterOpenCallback } ;
28
+ withModal ( props , null , modal => {
29
+ sinon . assert . calledWith ( afterOpenCallback , {
30
+ overlayEl : modal . portal . overlay ,
31
+ contentEl : modal . portal . content
32
+ } ) ;
35
33
} ) ;
36
34
} ) ;
37
35
38
36
it ( "should trigger the onAfterClose callback" , ( ) => {
39
37
const onAfterCloseCallback = sinon . spy ( ) ;
40
- const modal = renderModal ( {
38
+ withModal ( {
41
39
isOpen : true ,
42
40
onAfterClose : onAfterCloseCallback
43
41
} ) ;
44
-
45
- modal . portal . close ( ) ;
46
-
47
42
onAfterCloseCallback . called . should . be . ok ( ) ;
48
43
} ) ;
49
44
50
45
it ( "should not trigger onAfterClose callback when unmounting a closed modal" , ( ) => {
51
46
const onAfterCloseCallback = sinon . spy ( ) ;
52
- renderModal ( { isOpen : false , onAfterClose : onAfterCloseCallback } ) ;
53
- unmountModal ( ) ;
47
+ withModal ( { isOpen : false , onAfterClose : onAfterCloseCallback } ) ;
54
48
onAfterCloseCallback . called . should . not . be . ok ( ) ;
55
49
} ) ;
56
50
57
51
it ( "should trigger onAfterClose callback when unmounting an opened modal" , ( ) => {
58
52
const onAfterCloseCallback = sinon . spy ( ) ;
59
- renderModal ( { isOpen : true , onAfterClose : onAfterCloseCallback } ) ;
60
- unmountModal ( ) ;
53
+ withModal ( { isOpen : true , onAfterClose : onAfterCloseCallback } ) ;
61
54
onAfterCloseCallback . called . should . be . ok ( ) ;
62
55
} ) ;
63
56
64
57
it ( "keeps focus inside the modal when child has no tabbable elements" , ( ) => {
65
58
let tabPrevented = false ;
66
- const modal = renderModal ( { isOpen : true } , "hello" ) ;
67
- const content = mcontent ( modal ) ;
68
- document . activeElement . should . be . eql ( content ) ;
69
- tabKeyDown ( content , {
70
- preventDefault ( ) {
71
- tabPrevented = true ;
72
- }
59
+ const props = { isOpen : true } ;
60
+ withModal ( props , "hello" , modal => {
61
+ const content = mcontent ( modal ) ;
62
+ document . activeElement . should . be . eql ( content ) ;
63
+ tabKeyDown ( content , {
64
+ preventDefault ( ) {
65
+ tabPrevented = true ;
66
+ }
67
+ } ) ;
68
+ tabPrevented . should . be . eql ( true ) ;
73
69
} ) ;
74
- tabPrevented . should . be . eql ( true ) ;
75
70
} ) ;
76
71
77
72
it ( "handles case when child has no tabbable elements" , ( ) => {
78
- const modal = renderModal ( { isOpen : true } , "hello" ) ;
79
- const content = mcontent ( modal ) ;
80
- tabKeyDown ( content ) ;
81
- document . activeElement . should . be . eql ( content ) ;
73
+ const props = { isOpen : true } ;
74
+ withModal ( props , "hello" , modal => {
75
+ const content = mcontent ( modal ) ;
76
+ tabKeyDown ( content ) ;
77
+ document . activeElement . should . be . eql ( content ) ;
78
+ } ) ;
82
79
} ) ;
83
80
84
81
it ( "traps tab in the modal on shift + tab" , ( ) => {
@@ -90,120 +87,142 @@ export default () => {
90
87
{ bottomButton }
91
88
</ div >
92
89
) ;
93
- const modal = renderModal ( { isOpen : true } , modalContent ) ;
94
- const content = mcontent ( modal ) ;
95
- tabKeyDown ( content , { shiftKey : true } ) ;
96
- document . activeElement . textContent . should . be . eql ( "bottom" ) ;
90
+ const props = { isOpen : true } ;
91
+ withModal ( props , modalContent , modal => {
92
+ const content = mcontent ( modal ) ;
93
+ tabKeyDown ( content , { shiftKey : true } ) ;
94
+ document . activeElement . textContent . should . be . eql ( "bottom" ) ;
95
+ } ) ;
97
96
} ) ;
98
97
99
98
describe ( "shouldCloseOnEsc" , ( ) => {
100
99
context ( "when true" , ( ) => {
101
100
it ( "should close on Esc key event" , ( ) => {
102
101
const requestCloseCallback = sinon . spy ( ) ;
103
- const modal = renderModal ( {
104
- isOpen : true ,
105
- shouldCloseOnEsc : true ,
106
- onRequestClose : requestCloseCallback
107
- } ) ;
108
- escKeyDown ( mcontent ( modal ) ) ;
109
- requestCloseCallback . called . should . be . ok ( ) ;
110
- // Check if event is passed to onRequestClose callback.
111
- const event = requestCloseCallback . getCall ( 0 ) . args [ 0 ] ;
112
- event . should . be . ok ( ) ;
102
+ withModal (
103
+ {
104
+ isOpen : true ,
105
+ shouldCloseOnEsc : true ,
106
+ onRequestClose : requestCloseCallback
107
+ } ,
108
+ null ,
109
+ modal => {
110
+ escKeyDown ( mcontent ( modal ) ) ;
111
+ requestCloseCallback . called . should . be . ok ( ) ;
112
+ // Check if event is passed to onRequestClose callback.
113
+ const event = requestCloseCallback . getCall ( 0 ) . args [ 0 ] ;
114
+ event . should . be . ok ( ) ;
115
+ }
116
+ ) ;
113
117
} ) ;
114
118
} ) ;
115
119
116
120
context ( "when false" , ( ) => {
117
121
it ( "should not close on Esc key event" , ( ) => {
118
122
const requestCloseCallback = sinon . spy ( ) ;
119
- const modal = renderModal ( {
123
+ const props = {
120
124
isOpen : true ,
121
125
shouldCloseOnEsc : false ,
122
126
onRequestClose : requestCloseCallback
127
+ } ;
128
+ withModal ( props , null , modal => {
129
+ escKeyDown ( mcontent ( modal ) ) ;
130
+ requestCloseCallback . called . should . be . false ;
123
131
} ) ;
124
- escKeyDown ( mcontent ( modal ) ) ;
125
- requestCloseCallback . called . should . be . false ;
126
132
} ) ;
127
133
} ) ;
128
134
} ) ;
129
135
130
136
describe ( "shouldCloseOnoverlayClick" , ( ) => {
131
137
it ( "when false, click on overlay should not close" , ( ) => {
132
138
const requestCloseCallback = sinon . spy ( ) ;
133
- const modal = renderModal ( {
139
+ const props = {
134
140
isOpen : true ,
135
141
shouldCloseOnOverlayClick : false
142
+ } ;
143
+ withModal ( props , null , modal => {
144
+ const overlay = moverlay ( modal ) ;
145
+ clickAt ( overlay ) ;
146
+ requestCloseCallback . called . should . not . be . ok ( ) ;
136
147
} ) ;
137
- const overlay = moverlay ( modal ) ;
138
- clickAt ( overlay ) ;
139
- requestCloseCallback . called . should . not . be . ok ( ) ;
140
148
} ) ;
141
149
142
150
it ( "when true, click on overlay must close" , ( ) => {
143
151
const requestCloseCallback = sinon . spy ( ) ;
144
- const modal = renderModal ( {
152
+ const props = {
145
153
isOpen : true ,
146
154
shouldCloseOnOverlayClick : true ,
147
155
onRequestClose : requestCloseCallback
156
+ } ;
157
+ withModal ( props , null , modal => {
158
+ clickAt ( moverlay ( modal ) ) ;
159
+ requestCloseCallback . called . should . be . ok ( ) ;
148
160
} ) ;
149
- clickAt ( moverlay ( modal ) ) ;
150
- requestCloseCallback . called . should . be . ok ( ) ;
151
161
} ) ;
152
162
153
163
it ( "overlay mouse down and content mouse up, should not close" , ( ) => {
154
164
const requestCloseCallback = sinon . spy ( ) ;
155
- const modal = renderModal ( {
165
+ const props = {
156
166
isOpen : true ,
157
167
shouldCloseOnOverlayClick : true ,
158
168
onRequestClose : requestCloseCallback
169
+ } ;
170
+ withModal ( props , null , modal => {
171
+ mouseDownAt ( moverlay ( modal ) ) ;
172
+ mouseUpAt ( mcontent ( modal ) ) ;
173
+ requestCloseCallback . called . should . not . be . ok ( ) ;
159
174
} ) ;
160
- mouseDownAt ( moverlay ( modal ) ) ;
161
- mouseUpAt ( mcontent ( modal ) ) ;
162
- requestCloseCallback . called . should . not . be . ok ( ) ;
163
175
} ) ;
164
176
165
177
it ( "content mouse down and overlay mouse up, should not close" , ( ) => {
166
178
const requestCloseCallback = sinon . spy ( ) ;
167
- const modal = renderModal ( {
179
+ const props = {
168
180
isOpen : true ,
169
181
shouldCloseOnOverlayClick : true ,
170
182
onRequestClose : requestCloseCallback
183
+ } ;
184
+ withModal ( props , null , modal => {
185
+ mouseDownAt ( mcontent ( modal ) ) ;
186
+ mouseUpAt ( moverlay ( modal ) ) ;
187
+ requestCloseCallback . called . should . not . be . ok ( ) ;
171
188
} ) ;
172
- mouseDownAt ( mcontent ( modal ) ) ;
173
- mouseUpAt ( moverlay ( modal ) ) ;
174
- requestCloseCallback . called . should . not . be . ok ( ) ;
175
189
} ) ;
176
190
} ) ;
177
191
178
192
it ( "should not stop event propagation" , ( ) => {
179
193
let hasPropagated = false ;
180
- const modal = renderModal ( {
194
+ const props = {
181
195
isOpen : true ,
182
196
shouldCloseOnOverlayClick : true
197
+ } ;
198
+ withModal ( props , null , modal => {
199
+ const propagated = ( ) => ( hasPropagated = true ) ;
200
+ window . addEventListener ( "click" , propagated ) ;
201
+ const event = new MouseEvent ( "click" , { bubbles : true } ) ;
202
+ moverlay ( modal ) . dispatchEvent ( event ) ;
203
+ hasPropagated . should . be . ok ( ) ;
204
+ window . removeEventListener ( "click" , propagated ) ;
183
205
} ) ;
184
- window . addEventListener ( "click" , ( ) => {
185
- hasPropagated = true ;
186
- } ) ;
187
- moverlay ( modal ) . dispatchEvent ( new MouseEvent ( "click" , { bubbles : true } ) ) ;
188
- hasPropagated . should . be . ok ( ) ;
189
206
} ) ;
190
207
191
208
it ( "verify event passing on overlay click" , ( ) => {
192
209
const requestCloseCallback = sinon . spy ( ) ;
193
- const modal = renderModal ( {
210
+ const props = {
194
211
isOpen : true ,
195
212
shouldCloseOnOverlayClick : true ,
196
213
onRequestClose : requestCloseCallback
214
+ } ;
215
+ withModal ( props , null , modal => {
216
+ // click the overlay
217
+ clickAt ( moverlay ( modal ) , {
218
+ // Used to test that this was the event received
219
+ fakeData : "ABC"
220
+ } ) ;
221
+ requestCloseCallback . called . should . be . ok ( ) ;
222
+ // Check if event is passed to onRequestClose callback.
223
+ const event = requestCloseCallback . getCall ( 0 ) . args [ 0 ] ;
224
+ event . should . be . ok ( ) ;
197
225
} ) ;
198
- // click the overlay
199
- clickAt ( moverlay ( modal ) , {
200
- // Used to test that this was the event received
201
- fakeData : "ABC"
202
- } ) ;
203
- requestCloseCallback . called . should . be . ok ( ) ;
204
- // Check if event is passed to onRequestClose callback.
205
- const event = requestCloseCallback . getCall ( 0 ) . args [ 0 ] ;
206
- event . should . be . ok ( ) ;
207
226
} ) ;
208
227
209
228
it ( "on nested modals, only the topmost should handle ESC key." , ( ) => {
@@ -214,7 +233,7 @@ export default () => {
214
233
innerModal = ref ;
215
234
} ;
216
235
217
- renderModal (
236
+ withModal (
218
237
{
219
238
isOpen : true ,
220
239
onRequestClose : requestCloseCallback
@@ -225,12 +244,13 @@ export default () => {
225
244
ref = { innerModalRef }
226
245
>
227
246
< span > Test</ span >
228
- </ Modal >
247
+ </ Modal > ,
248
+ ( ) => {
249
+ const content = mcontent ( innerModal ) ;
250
+ escKeyDown ( content ) ;
251
+ innerRequestCloseCallback . called . should . be . ok ( ) ;
252
+ requestCloseCallback . called . should . not . be . ok ( ) ;
253
+ }
229
254
) ;
230
-
231
- const content = mcontent ( innerModal ) ;
232
- escKeyDown ( content ) ;
233
- innerRequestCloseCallback . called . should . be . ok ( ) ;
234
- requestCloseCallback . called . should . not . be . ok ( ) ;
235
255
} ) ;
236
256
} ;
0 commit comments