This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
Copy pathmodal.spec.js
435 lines (325 loc) · 13.3 KB
/
modal.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
describe('$modal', function () {
var $rootScope, $document, $compile, $templateCache, $timeout, $q;
var $modal, $modalProvider;
var triggerKeyDown = function (element, keyCode) {
var e = $.Event("keydown");
e.which = keyCode;
element.trigger(e);
};
beforeEach(module('ui.bootstrap.modal'));
beforeEach(module('template/modal/backdrop.html'));
beforeEach(module('template/modal/window.html'));
beforeEach(module(function(_$modalProvider_){
$modalProvider = _$modalProvider_;
}));
beforeEach(inject(function (_$rootScope_, _$document_, _$compile_, _$templateCache_, _$timeout_, _$q_, _$modal_) {
$rootScope = _$rootScope_;
$document = _$document_;
$compile = _$compile_;
$templateCache = _$templateCache_;
$timeout = _$timeout_;
$q = _$q_;
$modal = _$modal_;
}));
beforeEach(inject(function ($rootScope) {
this.addMatchers({
toBeResolvedWith: function(value) {
var resolved;
this.message = function() {
return "Expected '" + angular.mock.dump(this.actual) + "' to be resolved with '" + value + "'.";
};
this.actual.then(function(result){
resolved = result;
});
$rootScope.$digest();
return resolved === value;
},
toBeRejectedWith: function(value) {
var rejected;
this.message = function() {
return "Expected '" + angular.mock.dump(this.actual) + "' to be rejected with '" + value + "'.";
};
this.actual.then(angular.noop, function(reason){
rejected = reason;
});
$rootScope.$digest();
return rejected === value;
},
toHaveModalOpenWithContent: function(content, selector) {
var contentToCompare, modalDomEls = this.actual.find('body > div.modal');
this.message = function() {
return "Expected '" + angular.mock.dump(modalDomEls) + "' to be open with '" + content + "'.";
};
contentToCompare = selector ? modalDomEls.find(selector) : modalDomEls;
return modalDomEls.css('display') === 'block' && contentToCompare.html() == content;
},
toHaveModalsOpen: function(noOfModals) {
var modalDomEls = this.actual.find('body > div.modal');
return modalDomEls.length === noOfModals;
},
toHaveBackdrop: function() {
var backdropDomEls = this.actual.find('body > div.modal-backdrop');
this.message = function() {
return "Expected '" + angular.mock.dump(backdropDomEls) + "' to be a backdrop element'.";
};
return backdropDomEls.length === 1;
},
toHaveOneDivWithCertainClassAndZindexInStyleAttr: function(divclass, expectedZindex) {
var domEls = this.actual.find('body > div.'+divclass);
this.message = function() {
return "Expected '" + angular.mock.dump(domEls) + "' to be a div element with class '"+divclass+"' and style containing z-index: "+expectedZindex+".";
};
var divsAtLevel = 0;
for(var i=0; i < domEls.length; i++)
{
var domEl = domEls[i];
var style = domEl.getAttribute('style');
if (style.indexOf('z-index: ' + expectedZindex) != -1) {
divsAtLevel++;
}
}
return divsAtLevel === 1;
}
});
}));
afterEach(function () {
var body = $document.find('body');
body.find('div.modal').remove();
body.find('div.modal-backdrop').remove();
});
function open(modalOptions) {
var modal = $modal.open(modalOptions);
$rootScope.$digest();
return modal;
}
function close(modal, result) {
modal.close(result);
$rootScope.$digest();
}
function dismiss(modal, reason) {
modal.dismiss(reason);
$rootScope.$digest();
}
describe('basic scenarios with default options', function () {
it('should open and dismiss a modal with a minimal set of options', function () {
var modal = open({template: '<div>Content</div>'});
expect($document).toHaveModalsOpen(1);
expect($document).toHaveModalOpenWithContent('Content', 'div');
expect($document).toHaveBackdrop();
dismiss(modal, 'closing in test');
expect($document).toHaveModalsOpen(0);
expect($document).not.toHaveBackdrop();
});
it('should open a modal from templateUrl', function () {
$templateCache.put('content.html', '<div>URL Content</div>');
var modal = open({templateUrl: 'content.html'});
expect($document).toHaveModalsOpen(1);
expect($document).toHaveModalOpenWithContent('URL Content', 'div');
expect($document).toHaveBackdrop();
dismiss(modal, 'closing in test');
expect($document).toHaveModalsOpen(0);
expect($document).not.toHaveBackdrop();
});
it('should support closing on ESC', function () {
var modal = open({template: '<div>Content</div>'});
expect($document).toHaveModalsOpen(1);
triggerKeyDown($document, 27);
$rootScope.$digest();
expect($document).toHaveModalsOpen(0);
});
it('should support closing on backdrop click', function () {
var modal = open({template: '<div>Content</div>'});
expect($document).toHaveModalsOpen(1);
$document.find('body > div.modal-backdrop').click();
$rootScope.$digest();
expect($document).toHaveModalsOpen(0);
});
it('should resolve returned promise on close', function () {
var modal = open({template: '<div>Content</div>'});
close(modal, 'closed ok');
expect(modal.result).toBeResolvedWith('closed ok');
});
it('should reject returned promise on dismiss', function () {
var modal = open({template: '<div>Content</div>'});
dismiss(modal, 'esc');
expect(modal.result).toBeRejectedWith('esc');
});
it('should expose a promise linked to the templateUrl / resolve promises', function () {
var modal = open({template: '<div>Content</div>', resolve: {
ok: function() {return $q.when('ok');}
}}
);
expect(modal.opened).toBeResolvedWith(true);
});
it('should expose a promise linked to the templateUrl / resolve promises and reject it if needed', function () {
var modal = open({template: '<div>Content</div>', resolve: {
ok: function() {return $q.reject('ko');}
}}
);
expect(modal.opened).toBeRejectedWith(false);
});
});
describe('default options can be changed in a provider', function () {
it('should allow overriding default options in a provider', function () {
$modalProvider.options.backdrop = false;
var modal = open({template: '<div>Content</div>'});
expect($document).toHaveModalOpenWithContent('Content', 'div');
expect($document).not.toHaveBackdrop();
});
});
describe('option by option', function () {
describe('template and templateUrl', function () {
it('should throw an error if none of template and templateUrl are provided', function () {
expect(function(){
var modal = open({});
}).toThrow(new Error('One of template or templateUrl options is required.'));
});
it('should not fail if a templateUrl contains leading / trailing white spaces', function () {
$templateCache.put('whitespace.html', ' <div>Whitespaces</div> ');
open({templateUrl: 'whitespace.html'});
expect($document).toHaveModalOpenWithContent('Whitespaces', 'div');
});
});
describe('controllers', function () {
it('should accept controllers and inject modal instances', function () {
var TestCtrl = function($scope, $modalInstance) {
$scope.fromCtrl = 'Content from ctrl';
$scope.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
};
var modal = open({template: '<div>{{fromCtrl}} {{isModalInstance}}</div>', controller: TestCtrl});
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
});
});
describe('resolve', function () {
var ExposeCtrl = function($scope, value) {
$scope.value = value;
};
function modalDefinition(template, resolve) {
return {
template: template,
controller: ExposeCtrl,
resolve: resolve
};
}
it('should resolve simple values', function () {
open(modalDefinition('<div>{{value}}</div>', {
value: function () {
return 'Content from resolve';
}
}));
expect($document).toHaveModalOpenWithContent('Content from resolve', 'div');
});
it('should delay showing modal if one of the resolves is a promise', function () {
open(modalDefinition('<div>{{value}}</div>', {
value: function () {
return $timeout(function(){ return 'Promise'; }, 100);
}
}));
expect($document).toHaveModalsOpen(0);
$timeout.flush();
expect($document).toHaveModalOpenWithContent('Promise', 'div');
});
it('should not open dialog (and reject returned promise) if one of resolve fails', function () {
var deferred = $q.defer();
var modal = open(modalDefinition('<div>{{value}}</div>', {
value: function () {
return deferred.promise;
}
}));
expect($document).toHaveModalsOpen(0);
deferred.reject('error in test');
$rootScope.$digest();
expect($document).toHaveModalsOpen(0);
expect(modal.result).toBeRejectedWith('error in test');
});
it('should support injection with minification-safe syntax in resolve functions', function () {
open(modalDefinition('<div>{{value.id}}</div>', {
value: ['$locale', function (e) {
return e;
}]
}));
expect($document).toHaveModalOpenWithContent('en-us', 'div');
});
//TODO: resolves with dependency injection - do we want to support them?
});
describe('scope', function () {
it('should custom scope if provided', function () {
var $scope = $rootScope.$new();
$scope.fromScope = 'Content from custom scope';
open({
template: '<div>{{fromScope}}</div>',
scope: $scope
});
expect($document).toHaveModalOpenWithContent('Content from custom scope', 'div');
});
});
describe('keyboard', function () {
it('should not close modals if keyboard option is set to false', function () {
open({
template: '<div>No keyboard</div>',
keyboard: false
});
expect($document).toHaveModalsOpen(1);
triggerKeyDown($document, 27);
$rootScope.$digest();
expect($document).toHaveModalsOpen(1);
});
});
describe('backdrop', function () {
it('should not have any backdrop element if backdrop set to false', function () {
open({
template: '<div>No backdrop</div>',
backdrop: false
});
expect($document).toHaveModalOpenWithContent('No backdrop', 'div');
expect($document).not.toHaveBackdrop();
});
it('should not close modal on backdrop click if backdrop is specified as "static"', function () {
open({
template: '<div>Static backdrop</div>',
backdrop: 'static'
});
$document.find('body > div.modal-backdrop').click();
$rootScope.$digest();
expect($document).toHaveModalOpenWithContent('Static backdrop', 'div');
expect($document).toHaveBackdrop();
});
});
});
describe('multiple modals', function () {
it('should allow opening of multiple modals', function () {
var modal1 = open({template: '<div>Modal1</div>'});
var modal2 = open({template: '<div>Modal2</div>'});
expect($document).toHaveModalsOpen(2);
dismiss(modal2);
expect($document).toHaveModalsOpen(1);
expect($document).toHaveModalOpenWithContent('Modal1', 'div');
dismiss(modal1);
expect($document).toHaveModalsOpen(0);
});
it('should occlude any open child modals', function() {
var modal1 = open({template: '<div>Modal1</div>'});
$timeout.flush(); // to call the directives in their natural order
var modal2 = open({template: '<div>Modal2</div>'});
$timeout.flush();
expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal-backdrop','1040');
expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal','1050');
expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal-backdrop','1060');
expect($document).toHaveOneDivWithCertainClassAndZindexInStyleAttr('modal','1070');
});
it('should not close any modals on ESC if the topmost one does not allow it', function () {
var modal1 = open({template: '<div>Modal1</div>'});
var modal2 = open({template: '<div>Modal2</div>', keyboard: false});
triggerKeyDown($document, 27);
$rootScope.$digest();
expect($document).toHaveModalsOpen(2);
});
it('should not close any modals on click if a topmost modal does not have backdrop', function () {
var modal1 = open({template: '<div>Modal1</div>'});
var modal2 = open({template: '<div>Modal2</div>', backdrop: false});
$document.find('body > div.modal-backdrop').click();
$rootScope.$digest();
expect($document).toHaveModalsOpen(2);
});
});
});