-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathstateDirectivesSpec.js
640 lines (512 loc) · 22.2 KB
/
stateDirectivesSpec.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
describe('uiStateRef', function() {
var timeoutFlush, el, el2, template, scope, document, _locationProvider;
beforeEach(module('ui.router'));
beforeEach(module(function($stateProvider, $locationProvider) {
_locationProvider = $locationProvider;
$stateProvider.state('top', {
url: ''
}).state('contacts', {
url: '/contacts',
template: '<a ui-sref=".item({ id: 5 })" class="item">Person</a> <ui-view></ui-view>'
}).state('contacts.item', {
url: '/:id',
template: '<a ui-sref=".detail" class="item-detail">Detail</a> | <a ui-sref="^" class="item-parent">Parent</a> | <ui-view></ui-view>'
}).state('contacts.item.detail', {
template: '<div class="title">Detail</div> | <a ui-sref="^" class="item-parent2">Item</a>'
});
}));
beforeEach(inject(function($document) {
document = $document[0];
}));
function triggerClick(el, options) {
options = angular.extend({
metaKey: false,
ctrlKey: false,
shiftKey: false,
altKey: false,
button: 0
}, options || {});
var e = document.createEvent("MouseEvents");
e.initMouseEvent(
"click", // typeArg of type DOMString, Specifies the event type.
true, // canBubbleArg of type boolean, Specifies whether or not the event can bubble.
true, // cancelableArg of type boolean, Specifies whether or not the event's default action can be prevented.
undefined, // viewArg of type views::AbstractView, Specifies the Event's AbstractView.
0, // detailArg of type long, Specifies the Event's mouse click count.
0, // screenXArg of type long, Specifies the Event's screen x coordinate
0, // screenYArg of type long, Specifies the Event's screen y coordinate
0, // clientXArg of type long, Specifies the Event's client x coordinate
0, // clientYArg of type long, Specifies the Event's client y coordinate
options.ctrlKey, // ctrlKeyArg of type boolean, Specifies whether or not control key was depressed during the Event.
options.altKey, // altKeyArg of type boolean, Specifies whether or not alt key was depressed during the Event.
options.shiftKey, // shiftKeyArg of type boolean, Specifies whether or not shift key was depressed during the Event.
options.metaKey, // metaKeyArg of type boolean, Specifies whether or not meta key was depressed during the Event.
options.button, // buttonArg of type unsigned short, Specifies the Event's mouse button.
null // relatedTargetArg of type EventTarget
);
el[0].dispatchEvent(e);
}
describe('links with promises', function() {
it('should update the href when promises on parameters change before scope is applied', inject(function($rootScope, $compile, $q) {
var defer = $q.defer();
el = angular.element('<a ui-sref="contacts.item.detail({ id: contact.id })">Details</a>');
$rootScope.contact = defer.promise;
defer.resolve({ id: 6 });
$compile(el)($rootScope);
$rootScope.$digest();
// HACK: Promises no longer auto-unwrap in 1.2.x+
if ($rootScope.contact.$$resolved && $rootScope.contact.$$resolved.value) {
$rootScope.contact = $rootScope.contact.$$resolved.value;
$rootScope.$digest();
}
expect(el.attr('href')).toBe('#/contacts/6');
}));
});
function buildDOM($rootScope, $compile, $timeout) {
el = angular.element('<a ui-sref="contacts.item.detail({ id: contact.id })">Details</a>');
el2 = angular.element('<a ui-sref="top">Top</a>');
scope = $rootScope;
scope.contact = { id: 5 };
scope.$apply();
$compile(el)(scope);
$compile(el2)(scope);
scope.$digest();
timeoutFlush = function () {
try {
$timeout.flush();
} catch (e) {
// Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
// Behave as Angular >=1.1.5 and do nothing in such case.
}
}
};
describe('links', function() {
beforeEach(inject(buildDOM));
it('should generate the correct href', function() {
expect(el.attr('href')).toBe('#/contacts/5');
expect(el2.attr('href')).toBe('#');
});
it('should update the href when parameters change', function() {
expect(el.attr('href')).toBe('#/contacts/5');
scope.contact.id = 6;
scope.$apply();
expect(el.attr('href')).toBe('#/contacts/6');
});
it('should allow multi-line attribute values', inject(function($compile, $rootScope) {
el = angular.element("<a ui-sref=\"contacts.item.detail({\n\tid: $index\n})\">Details</a>");
$rootScope.$index = 3;
$rootScope.$apply();
$compile(el)($rootScope);
$rootScope.$digest();
expect(el.attr('href')).toBe('#/contacts/3');
}));
it('should transition states when left-clicked', inject(function($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
triggerClick(el);
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('contacts.item.detail');
expect($stateParams).toEqual({ id: "5" });
}));
it('should transition when given a click that contains no data (fake-click)', inject(function($state, $stateParams, $q) {
expect($state.current.name).toEqual('top');
triggerClick(el, {
metaKey: undefined,
ctrlKey: undefined,
shiftKey: undefined,
altKey: undefined,
button: undefined
});
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('contacts.item.detail');
expect($stateParams).toEqual({ id: "5" });
}));
it('should not transition states when ctrl-clicked', inject(function($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
triggerClick(el, { ctrlKey: true });
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({ });
}));
it('should not transition states when meta-clicked', inject(function($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
triggerClick(el, { metaKey: true });
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({});
}));
it('should not transition states when shift-clicked', inject(function($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
triggerClick(el, { shiftKey: true });
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({});
}));
it('should not transition states when middle-clicked', inject(function($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
triggerClick(el, { button: 1 });
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({});
}));
it('should not transition states when element has target specified', inject(function($state, $stateParams, $q) {
el.attr('target', '_blank');
expect($state.$current.name).toEqual('top');
triggerClick(el);
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({});
}));
it('should not transition states if preventDefault() is called in click handler', inject(function($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
el.bind('click', function(e) {
e.preventDefault();
});
triggerClick(el);
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({});
}));
it('should allow passing params to current state', inject(function($compile, $rootScope, $state) {
$state.current.name = 'contacts.item.detail';
el = angular.element("<a ui-sref=\"{id: $index}\">Details</a>");
$rootScope.$index = 3;
$rootScope.$apply();
$compile(el)($rootScope);
$rootScope.$digest();
expect(el.attr('href')).toBe('#/contacts/3');
}));
it('should allow multi-line attribute values when passing params to current state', inject(function($compile, $rootScope, $state) {
$state.current.name = 'contacts.item.detail';
el = angular.element("<a ui-sref=\"{\n\tid: $index\n}\">Details</a>");
$rootScope.$index = 3;
$rootScope.$apply();
$compile(el)($rootScope);
$rootScope.$digest();
expect(el.attr('href')).toBe('#/contacts/3');
}));
it('should take an object as a parameter and update properly on digest churns', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.item.detail(urlParams)">Contacts</a></div>');
template = $compile(el)($rootScope);
$rootScope.urlParams = { id:1 };
$rootScope.$digest();
expect(angular.element(template[0].querySelector('a')).attr('href')).toBe('#/contacts/1');
$rootScope.urlParams.id = 2;
$rootScope.$digest();
expect(angular.element(template[0].querySelector('a')).attr('href')).toBe('#/contacts/2');
}));
});
describe('links in html5 mode', function() {
beforeEach(function() {
_locationProvider.html5Mode(true);
});
beforeEach(inject(buildDOM));
it('should generate the correct href', function() {
expect(el.attr('href')).toBe('/contacts/5');
expect(el2.attr('href')).toBe('');
});
it('should update the href when parameters change', function() {
expect(el.attr('href')).toBe('/contacts/5');
scope.contact.id = 6;
scope.$apply();
expect(el.attr('href')).toBe('/contacts/6');
});
it('should transition states when the url is empty', inject(function($state, $stateParams, $q) {
// Odd, in html5Mode, the initial state isn't matching on empty url, but does match if top.url is "/".
// expect($state.$current.name).toEqual('top');
triggerClick(el2);
timeoutFlush();
$q.flush();
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({});
}));
});
describe('forms', function() {
var el, scope;
beforeEach(inject(function($rootScope, $compile) {
el = angular.element('<form ui-sref="contacts.item.detail({ id: contact.id })"></form>');
scope = $rootScope;
scope.contact = { id: 5 };
scope.$apply();
$compile(el)(scope);
scope.$digest();
}));
it('should generate the correct action', function() {
expect(el.attr('action')).toBe('#/contacts/5');
});
});
describe('relative transitions', function() {
beforeEach(inject(function($rootScope, $compile, $state) {
$state.transitionTo("contacts.item", { id: 5 });
el = angular.element('<a ui-sref=".detail">Details</a>');
scope = $rootScope;
scope.$apply();
$compile(el)(scope);
template = $compile(angular.element('<div><ui-view></ui-view><div>'))(scope);
scope.$digest();
}));
it('should work', inject(function ($state, $stateParams, $q, $timeout) {
triggerClick(el);
$timeout.flush();
$q.flush();
expect($state.$current.name).toBe("contacts.item.detail");
expect($state.params).toEqual({ id: "5" });
}));
it('should resolve states from parent uiView', inject(function ($state, $stateParams, $q, $timeout) {
$state.transitionTo('contacts');
$q.flush();
var parentToChild = angular.element(template[0].querySelector('a.item'));
triggerClick(parentToChild);
$timeout.flush();
$q.flush();
var childToGrandchild = angular.element(template[0].querySelector('a.item-detail'));
var childToParent = angular.element(template[0].querySelector('a.item-parent'));
triggerClick(childToGrandchild);
$timeout.flush();
$q.flush();
var grandchildToParent = angular.element(template[0].querySelector('a.item-parent2'));
expect($state.$current.name).toBe("contacts.item.detail")
triggerClick(grandchildToParent);
$timeout.flush();
$q.flush();
expect($state.$current.name).toBe("contacts.item");
$state.transitionTo("contacts.item.detail", { id: 3 });
triggerClick(childToParent);
$timeout.flush();
$q.flush();
expect($state.$current.name).toBe("contacts");
}));
});
describe('transition options', function() {
beforeEach(inject(function($rootScope, $compile, $state) {
el = angular.element('<a ui-sref="contacts.item.detail({ id: contact.id })" ui-sref-opts="{ reload: true, absolute: true, notify: true }">Details</a>');
scope = $rootScope;
scope.contact = { id: 5 };
$compile(el)(scope);
scope.$digest();
}));
it('uses allowed transition options', inject(function($q, $timeout, $state) {
var transitionOptions;
spyOn($state, 'go').andCallFake(function(state, params, options) {
transitionOptions = options;
});
triggerClick(el);
$timeout.flush();
expect(transitionOptions.reload).toEqual(true);
expect(transitionOptions.absolute).toEqual(true);
expect(transitionOptions.notify).toBeUndefined();
}));
});
});
describe('uiSrefActive', function() {
var el, template, scope, document, _stateProvider;
beforeEach(module('ui.router'));
beforeEach(module(function($stateProvider) {
_stateProvider = $stateProvider;
$stateProvider.state('top', {
url: ''
}).state('contacts', {
url: '/contacts',
views: {
'@': {
template: '<a ui-sref=".item({ id: 6 })" ui-sref-active="active">Contacts</a>'
}
}
}).state('contacts.item', {
url: '/:id',
}).state('contacts.item.detail', {
url: '/detail/:foo'
}).state('contacts.item.edit', {
url: '/edit'
});
}));
beforeEach(inject(function($document, $timeout) {
document = $document[0];
timeoutFlush = function () {
try {
$timeout.flush();
} catch (e) {
// Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
// Behave as Angular >=1.1.5 and do nothing in such case.
}
}
}));
it('should update class for sibling uiSref', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.item({ id: 1 })" ui-sref-active="active">Contacts</a><a ui-sref="contacts.item({ id: 2 })" ui-sref-active="active">Contacts</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
$state.transitionTo('contacts.item', { id: 2 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
}));
it('should match state\'s parameters', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.item.detail({ foo: \'bar\' })" ui-sref-active="active">Contacts</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
$state.transitionTo('contacts.item.detail', { id: 5, foo: 'bar' });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
$state.transitionTo('contacts.item.detail', { id: 5, foo: 'baz' });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
}));
it('should match on child states', inject(function($rootScope, $q, $compile, $state) {
template = $compile('<div><a ui-sref="contacts.item({ id: 1 })" ui-sref-active="active">Contacts</a></div>')($rootScope);
$rootScope.$digest();
var a = angular.element(template[0].getElementsByTagName('a')[0]);
$state.transitionTo('contacts.item.edit', { id: 1 });
$q.flush();
timeoutFlush();
expect(a.attr('class')).toMatch(/active/);
$state.transitionTo('contacts.item.edit', { id: 4 });
$q.flush();
timeoutFlush();
expect(a.attr('class')).not.toMatch(/active/);
}));
it('should NOT match on child states when active-equals is used', inject(function($rootScope, $q, $compile, $state) {
template = $compile('<div><a ui-sref="contacts.item({ id: 1 })" ui-sref-active-eq="active">Contacts</a></div>')($rootScope);
$rootScope.$digest();
var a = angular.element(template[0].getElementsByTagName('a')[0]);
$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
timeoutFlush();
expect(a.attr('class')).toMatch(/active/);
$state.transitionTo('contacts.item.edit', { id: 1 });
$q.flush();
timeoutFlush();
expect(a.attr('class')).not.toMatch(/active/);
}));
it('should match on child states when active-equals and active-equals-eq is used', inject(function($rootScope, $q, $compile, $state, $timeout) {
template = $compile('<div><a ui-sref="contacts.item({ id: 1 })" ui-sref-active="active" ui-sref-active-eq="active-eq">Contacts</a></div>')($rootScope);
$rootScope.$digest();
var a = angular.element(template[0].getElementsByTagName('a')[0]);
$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
timeoutFlush();
expect(a.attr('class')).toMatch(/active/);
expect(a.attr('class')).toMatch(/active-eq/);
$state.transitionTo('contacts.item.edit', { id: 1 });
$q.flush();
timeoutFlush();
expect(a.attr('class')).toMatch(/active/);
expect(a.attr('class')).not.toMatch(/active-eq/);
}));
it('should resolve relative state refs', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<section><div ui-view></div></section>');
template = $compile(el)($rootScope);
$rootScope.$digest();
$state.transitionTo('contacts');
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('ng-scope');
$state.transitionTo('contacts.item', { id: 6 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('ng-scope active');
$state.transitionTo('contacts.item', { id: 5 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('ng-scope');
}));
it('should match on any child state refs', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div ui-sref-active="active"><a ui-sref="contacts.item({ id: 1 })">Contacts</a><a ui-sref="contacts.item({ id: 2 })">Contacts</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();
expect(angular.element(template[0]).attr('class')).toBe('ng-scope');
$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0]).attr('class')).toBe('ng-scope active');
$state.transitionTo('contacts.item', { id: 2 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0]).attr('class')).toBe('ng-scope active');
}));
it('should match fuzzy on lazy loaded states', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.lazy" ui-sref-active="active">Lazy Contact</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();
$rootScope.$on('$stateNotFound', function () {
_stateProvider.state('contacts.lazy', {});
});
$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
$state.transitionTo('contacts.lazy');
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
}));
it('should match exactly on lazy loaded states', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.lazy" ui-sref-active-eq="active">Lazy Contact</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();
$rootScope.$on('$stateNotFound', function () {
_stateProvider.state('contacts.lazy', {});
});
$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
$state.transitionTo('contacts.lazy');
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
}));
});
describe('uiView controllers or onEnter handlers', function() {
var el, template, scope, document, count;
beforeEach(module('ui.router'));
beforeEach(module(function($stateProvider) {
count = 0;
$stateProvider
.state('aside', { url: '/aside', template: '<div class="aside"></div>' })
.state('A', { url: '/A', template: '<div class="A" ui-view="fwd"></div>' })
.state('A.fwd', {
url: '/fwd', views: { 'fwd@A': {
template: '<div class="fwd" ui-view>',
controller: function($state) { if (count++ < 20 && $state.current.name == 'A.fwd') $state.go(".nest"); }
}}
})
.state('A.fwd.nest', { url: '/nest', template: '<div class="nest"></div>' });
}));
beforeEach(inject(function($document) {
document = $document[0];
}));
it('should not go into an infinite loop when controller uses $state.go', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><ui-view></ui-view></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();
$state.transitionTo('aside');
$q.flush();
expect(template[0].querySelector('.aside')).toBeDefined();
expect(template[0].querySelector('.fwd')).toBeNull();
$state.transitionTo('A');
$q.flush();
expect(template[0].querySelector('.A')).not.toBeNull();
expect(template[0].querySelector('.fwd')).toBeNull();
$state.transitionTo('A.fwd');
$q.flush();
expect(template[0].querySelector('.A')).not.toBeNull();
expect(template[0].querySelector('.fwd')).not.toBeNull();
expect(template[0].querySelector('.nest')).not.toBeNull();
expect(count).toBe(1);
}));
});