forked from btford/angular-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.spec.js
305 lines (230 loc) · 7.57 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
'use strict';
describe('btfModal', function() {
var container,
btfModal,
$rootScope;
beforeEach(module('btford.modal'));
beforeEach(function () {
container = angular.element('<div></div>');
});
afterEach(function() {
container = null;
});
describe('without animations', function () {
beforeEach(inject(function(_btfModal_, _$rootScope_, $templateCache) {
btfModal = _btfModal_;
$rootScope = _$rootScope_;
$rootScope.greeting = 'こんばんは';
$templateCache.put('test.html', [200, '<div>{{greeting}}</div>', {}]);
}));
it('should not show a modal initially', function() {
var modal = btfModal({
templateUrl: 'test.html',
container: container
});
$rootScope.$digest();
expect(container.text()).toBe('');
});
it('should throw if called without a `template` or `templateUrl` option', function() {
expect(function () { btfModal({}); }).toThrow();
});
it('should throw if called with a text node', function() {
var modal = btfModal({
template: 'hey'
});
expect(function () {
modal.activate();
$rootScope.$digest();
}).toThrow();
});
it('should throw if called with both `template` and `templateUrl` options', function() {
expect(function () {
btfModal({
template: 'foo',
templateUrl: 'foo.html'
});
}).toThrow();
});
describe('#activate', function () {
it('should show a modal when activated with `templateUrl`', function() {
var modal = btfModal({
templateUrl: 'test.html',
container: container
});
modal.activate();
$rootScope.$digest();
expect(container.text()).toBe('こんばんは');
});
it('should show a modal when activated with `template`', function() {
var modal = btfModal({
template: '<span>{{greeting}}</span>',
container: container
});
modal.activate();
$rootScope.$digest();
expect(container.text()).toBe('こんばんは');
});
it('should instantiate a controller via the `controller` option', function() {
var modal = btfModal({
template: '<span>{{greeting}}</span>',
controller: function ($scope) {
$scope.greeting = 'goodnight'
},
container: container
});
modal.activate();
$rootScope.$digest();
expect(container.text()).toBe('goodnight');
});
it('should expose a controller to the scope via the `controllerAs` option', function() {
var modal = btfModal({
template: '<span>{{ctrl.greeting}}</span>',
controller: function () {
this.greeting = 'boa noite'
},
controllerAs: 'ctrl',
container: container
});
modal.activate();
$rootScope.$digest();
expect(container.text()).toBe('boa noite');
});
it('should pass locals to the modal scope', function() {
var modal = btfModal({
template: '<span>{{greeting}}</span>',
container: container
});
modal.activate({
greeting: 'bon soir'
});
$rootScope.$digest();
expect(container.text()).toBe('bon soir');
});
it('should not activate multiple times', function() {
var modal = btfModal({
template: '<span>x</span>',
container: container
});
modal.activate();
$rootScope.$digest();
modal.activate();
$rootScope.$digest();
expect(container.text()).toBe('x');
});
it('should resolve a promise after activating', function() {
var spy = jasmine.createSpy('activated');
var modal = btfModal({
template: '<span>x</span>',
container: container
});
modal.activate().then(spy);
expect(spy).not.toHaveBeenCalled();
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
});
it('should yield the modal element when resolving the promise', function() {
var spy = jasmine.createSpy('activated');
var modal = btfModal({
template: '<span>{{greeting}}</span>',
controller: function ($scope) {
$scope.greeting = 'bonne nuit';
},
container: container
});
modal.activate().then(spy);
$rootScope.$digest();
expect(spy).toHaveBeenCalledWith(jasmine.any(Object));
expect(spy.mostRecentCall.args[0].text()).toEqual('bonne nuit');
});
});
describe('#deactivate', function () {
it('should remove a modal when deactivated', function() {
var modal = btfModal({
template: '<span>{{greeting}}</span>',
container: container
});
modal.activate();
$rootScope.$digest();
modal.deactivate();
$rootScope.$digest();
expect(container.text()).toBe('');
});
it('should destroy the scope when deactivated', inject(function($$asyncCallback) {
var destroySpy = jasmine.createSpy('onDestroy');
var modal = btfModal({
template: '<span>{{greeting}}</span>',
container: container,
controller: function ($scope) {
$scope.$on('$destroy', destroySpy);
}
});
modal.activate();
$rootScope.$digest();
expect(destroySpy).not.toHaveBeenCalled();
modal.deactivate();
$rootScope.$digest();
$$asyncCallback.flush();
expect(destroySpy).toHaveBeenCalled();
}));
it('should resolve a promise after deactivating', inject(function($$asyncCallback) {
var spy = jasmine.createSpy('deactivated');
var modal = btfModal({
template: '<span>x</span>',
container: container
});
modal.activate();
$rootScope.$digest();
modal.deactivate().then(spy);
expect(spy).not.toHaveBeenCalled();
$$asyncCallback.flush();
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
}));
});
describe('#active', function () {
it('should return the state of the modal', inject(function($$asyncCallback) {
var modal = btfModal({
template: '<span>{{greeting}}</span>',
container: container
});
$rootScope.$digest();
expect(modal.active()).toBe(false);
modal.activate();
$rootScope.$digest();
expect(modal.active()).toBe(true);
modal.deactivate();
$rootScope.$digest();
$$asyncCallback.flush();
expect(modal.active()).toBe(false);
}));
});
});
describe('with animations', function () {
var $animate,
modal;
beforeEach(module('ngAnimateMock'));
beforeEach(inject(function(btfModal, _$rootScope_, _$animate_) {
$rootScope = _$rootScope_;
$animate = _$animate_;
modal = btfModal({
template: '<span>animations!</span>',
container: container
});
}));
it('should trigger an enter animation when activated', function () {
modal.activate();
$rootScope.$digest();
var item = $animate.queue.shift();
expect(item.event).toBe('enter');
});
it('should trigger a leave animation when deactivated', function () {
modal.activate();
$rootScope.$digest();
$animate.queue.shift();
modal.deactivate();
$rootScope.$digest();
var item = $animate.queue.shift();
expect(item.event).toBe('leave');
});
});
});