This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathrouter-viewport.es5.spec.js
More file actions
837 lines (634 loc) · 22 KB
/
router-viewport.es5.spec.js
File metadata and controls
837 lines (634 loc) · 22 KB
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
'use strict';
describe('ngOutlet', function () {
var elt,
$compile,
$rootScope,
$router,
$templateCache,
$controllerProvider;
beforeEach(function() {
module('ng');
module('ngNewRouter');
module(function(_$controllerProvider_) {
$controllerProvider = _$controllerProvider_;
});
inject(function(_$compile_, _$rootScope_, _$router_, _$templateCache_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$router = _$router_;
$templateCache = _$templateCache_;
});
registerComponent('user', '<div>hello {{user.name}}</div>', function($routeParams) {
this.name = $routeParams.name;
});
registerComponent('one', '<div>{{one.number}}</div>', boringController('number', 'one'));
registerComponent('two', '<div>{{two.number}}</div>', boringController('number', 'two'));
});
it('should work in a simple case', function () {
compile('<ng-outlet></ng-outlet>');
$router.config([
{ path: '/', component: 'one' }
]);
$router.navigate('/');
$rootScope.$digest();
expect(elt.text()).toBe('one');
});
// See https://github.com/angular/router/issues/105
it('should warn when instantiating a component with no controller', function () {
put('noController', '<div>{{ 2 + 2 }}</div>');
$router.config([
{ path: '/', component: 'noController' }
]);
spyOn(console, 'warn');
compile('<ng-outlet></ng-outlet>');
$router.navigate('/');
expect(console.warn).toHaveBeenCalledWith('Could not find controller for', 'NoControllerController');
expect(elt.text()).toBe('4');
});
it('should navigate between components with different parameters', function () {
$router.config([
{ path: '/user/:name', component: 'user' }
]);
compile('<ng-outlet></ng-outlet>');
$router.navigate('/user/brian');
$rootScope.$digest();
expect(elt.text()).toBe('hello brian');
$router.navigate('/user/igor');
$rootScope.$digest();
expect(elt.text()).toBe('hello igor');
});
it('should not reactivate a parent when navigating between child components with different parameters', function () {
ParentController.$routeConfig = [
{ path: '/user/:name', component: 'user' }
];
function ParentController () {}
var spy = ParentController.prototype.activate = jasmine.createSpy('activate');
registerComponent('parent', 'parent { <ng-outlet></ng-outlet> }', ParentController);
$router.config([
{ path: '/parent', component: 'parent' }
]);
compile('<ng-outlet></ng-outlet>');
$router.navigate('/parent/user/brian');
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
expect(elt.text()).toBe('parent { hello brian }');
spy.calls.reset();
$router.navigate('/parent/user/igor');
$rootScope.$digest();
expect(spy).not.toHaveBeenCalled();
expect(elt.text()).toBe('parent { hello igor }');
});
it('should work with multiple named outlets', function () {
$router.config([
{ path: '/', component: {left: 'one', right: 'two'} },
{ path: '/switched', components: {left: 'two', right: 'one'} }
]);
compile('outlet 1: <div ng-outlet="left"></div> | ' +
'outlet 2: <div ng-outlet="right"></div>');
$router.navigate('/');
$rootScope.$digest();
expect(elt.text()).toBe('outlet 1: one | outlet 2: two');
$router.navigate('/switched');
$rootScope.$digest();
expect(elt.text()).toBe('outlet 1: two | outlet 2: one');
});
it('should work with nested outlets', function () {
registerComponent('childRouter', '<div>inner { <div ng-outlet></div> }</div>', undefined, [
{ path: '/b', component: 'one' }
]);
$router.config([
{ path: '/a', component: 'childRouter' }
]);
compile('<div>outer { <div ng-outlet></div> }</div>');
$router.navigate('/a/b');
$rootScope.$digest();
expect(elt.text()).toBe('outer { inner { one } }');
});
it('should work with recursive nested outlets', function () {
put('router', '<div>recur { <div ng-outlet></div> }</div>');
$router.config([
{ path: '/recur', component: 'router' },
{ path: '/', component: 'one' }
]);
compile('<div>root { <div ng-outlet></div> }</div>');
$router.navigate('/');
$rootScope.$digest();
expect(elt.text()).toBe('root { one }');
});
it('should update anchor hrefs with the routerLink directive', function () {
put('one', '<div><a ng-link="two">{{number}}</a></div>');
$router.config([
{ path: '/a', component: 'one' },
{ path: '/b', component: 'two' }
]);
compile('<div>outer { <div ng-outlet></div> }</div>');
$router.navigate('/a');
$rootScope.$digest();
expect(elt.find('a').attr('href')).toBe('./b');
});
it('should allow params in routerLink directive', function () {
put('router', '<div>outer { <div ng-outlet></div> }</div>');
put('one', '<div><a ng-link="two({param: \'lol\'})">{{number}}</a></div>');
$router.config([
{ path: '/a', component: 'one' },
{ path: '/b/:param', component: 'two' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/a');
$rootScope.$digest();
expect(elt.find('a').attr('href')).toBe('./b/lol');
});
// TODO: test dynamic links
it('should update the href of links', function () {
put('router', '<div>outer { <div ng-outlet></div> }</div>');
put('one', '<div><a ng-link="two({param: one.number})">{{one.number}}</a></div>');
$router.config([
{ path: '/a', component: 'one' },
{ path: '/b/:param', component: 'two' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/a');
$rootScope.$digest();
expect(elt.find('a').attr('href')).toBe('./b/one');
});
it('should run the activate hook of controllers', function () {
var spy = jasmine.createSpy('activate');
registerComponent('activate', '', {
activate: spy
});
$router.config([
{ path: '/a', component: 'activate' }
]);
compile('<div>outer { <div ng-outlet></div> }</div>');
$router.navigate('/a');
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
});
it('should inject into the activate hook of a controller', inject(function ($http) {
var spy = jasmine.createSpy('activate');
spy.$inject = ['$routeParams', '$http'];
registerComponent('user', '', {
activate: spy
});
$router.config([
{ path: '/user/:name', component: 'user' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user/brian');
$rootScope.$digest();
expect(spy).toHaveBeenCalledWith({name: 'brian'}, $http);
}));
it('should inject $scope into the activate hook of a controller', function () {
var spy = jasmine.createSpy('activate');
spy.$inject = ['$scope'];
registerComponent('user', '', {
activate: spy
});
$router.config([
{ path: '/user/:name', component: 'user' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user/brian');
$rootScope.$digest();
expect(spy.calls.first().args[0].$root).toEqual($rootScope);
});
it('should inject $scope into the controller constructor', function () {
var injectedScope;
registerComponent('user', '', function ($scope) {
injectedScope = $scope;
});
$router.config([
{ path: '/user', component: 'user' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user');
$rootScope.$digest();
expect(injectedScope).toBeDefined();
});
it('should inject controller constructors that use the array notation for dependency injection', inject(function ($http) {
var injectedHttp;
registerComponent('user', '', ['$http', function (differentName) {
injectedHttp = differentName;
}]);
$router.config([
{ path: '/user', component: 'user' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user');
$rootScope.$digest();
expect(injectedHttp).toBe($http);
}));
it('should run the deactivate hook of controllers', function () {
var spy = jasmine.createSpy('deactivate');
registerComponent('deactivate', '', {
deactivate: spy
});
$router.config([
{ path: '/a', component: 'deactivate' },
{ path: '/b', component: 'one' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/a');
$rootScope.$digest();
$router.navigate('/b');
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
});
it('should inject into the deactivate hook of controllers', inject(function ($http) {
var spy = jasmine.createSpy('deactivate');
spy.$inject = ['$routeParams', '$http'];
registerComponent('deactivate', '', {
deactivate: spy
});
$router.config([
{ path: '/user/:name', component: 'deactivate' },
{ path: '/post/:id', component: 'one' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user/brian');
$rootScope.$digest();
$router.navigate('/post/123');
$rootScope.$digest();
expect(spy).toHaveBeenCalledWith({id: '123'}, $http);
}));
it('should run the deactivate hook before the activate hook', function () {
var log = [];
registerComponent('activate', '', {
activate: function () { log.push('activate'); }
});
registerComponent('deactivate', '', {
deactivate: function () { log.push('deactivate'); }
});
$router.config([
{ path: '/a', component: 'deactivate' },
{ path: '/b', component: 'activate' }
]);
compile('outer { <div ng-outlet></div> }');
$router.navigate('/a');
$rootScope.$digest();
$router.navigate('/b');
$rootScope.$digest();
expect(log).toEqual(['deactivate', 'activate']);
});
it('should not activate a component when canActivate returns false', function () {
var spy = jasmine.createSpy('activate');
registerComponent('activate', '', {
canActivate: function () { return false; },
activate: spy
});
$router.config([
{ path: '/a', component: 'activate' }
]);
compile('outer { <div ng-outlet></div> }');
$router.navigate('/a');
$rootScope.$digest();
expect(spy).not.toHaveBeenCalled();
expect(elt.text()).toBe('outer { }');
});
it('should not activate a component when canActivate returns a rejected promise', inject(function ($q) {
var spy = jasmine.createSpy('activate');
registerComponent('activate', '', {
canActivate: function () { return $q.reject(); },
activate: spy
});
$router.config([
{ path: '/a', component: 'activate' }
]);
compile('outer { <div ng-outlet></div> }');
$router.navigate('/a');
$rootScope.$digest();
expect(spy).not.toHaveBeenCalled();
expect(elt.text()).toBe('outer { }');
}));
it('should activate a component when canActivate returns true', function () {
var spy = jasmine.createSpy('activate');
registerComponent('activate', 'hi', {
canActivate: function () { return true; },
activate: spy
});
$router.config([
{ path: '/a', component: 'activate' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/a');
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
expect(elt.text()).toBe('hi');
});
it('should activate a component when canActivate returns a resolved promise', inject(function ($q) {
var spy = jasmine.createSpy('activate');
registerComponent('activate', 'hi', {
canActivate: function () { return $q.when(); },
activate: spy
});
$router.config([
{ path: '/a', component: 'activate' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/a');
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
expect(elt.text()).toBe('hi');
}));
it('should inject into the canActivate hook of controllers', inject(function ($http) {
var spy = jasmine.createSpy('canActivate').and.returnValue(true);
spy.$inject = ['$routeParams', '$http'];
registerComponent('activate', '', {
canActivate: spy
});
$router.config([
{ path: '/user/:name', component: 'activate' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user/brian');
$rootScope.$digest();
expect(spy).toHaveBeenCalledWith({name: 'brian'}, $http);
}));
it('should not navigate when canDeactivate returns false', function () {
registerComponent('activate', 'hi', {
canDeactivate: function () { return false; }
});
$router.config([
{ path: '/a', component: 'activate' },
{ path: '/b', component: 'one' }
]);
compile('outer { <div ng-outlet></div> }');
$router.navigate('/a');
$rootScope.$digest();
expect(elt.text()).toBe('outer { hi }');
$router.navigate('/b');
$rootScope.$digest();
expect(elt.text()).toBe('outer { hi }');
});
it('should not navigate when canDeactivate returns a rejected promise', inject(function ($q) {
registerComponent('activate', 'hi', {
canDeactivate: function () { return $q.reject(); }
});
$router.config([
{ path: '/a', component: 'activate' },
{ path: '/b', component: 'one' }
]);
compile('outer { <div ng-outlet></div> }');
$router.navigate('/a');
$rootScope.$digest();
expect(elt.text()).toBe('outer { hi }');
$router.navigate('/b');
$rootScope.$digest();
expect(elt.text()).toBe('outer { hi }');
}));
it('should navigate when canDeactivate returns true', function () {
registerComponent('activate', 'hi', {
canDeactivate: function () { return true; }
});
$router.config([
{ path: '/a', component: 'activate' },
{ path: '/b', component: 'one' }
]);
compile('outer { <div ng-outlet></div> }');
$router.navigate('/a');
$rootScope.$digest();
expect(elt.text()).toBe('outer { hi }');
$router.navigate('/b');
$rootScope.$digest();
expect(elt.text()).toBe('outer { one }');
});
it('should activate a component when canActivate returns true', function () {
var spy = jasmine.createSpy('activate');
registerComponent('activate', 'hi', {
canActivate: function () { return true; },
activate: spy
});
$router.config([
{ path: '/a', component: 'activate' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/a');
$rootScope.$digest();
expect(spy).toHaveBeenCalled();
expect(elt.text()).toBe('hi');
});
it('should inject into the canDeactivate hook of controllers', inject(function ($http) {
var spy = jasmine.createSpy('canDeactivate').and.returnValue(true);
spy.$inject = ['$routeParams', '$http'];
registerComponent('deactivate', '', {
canDeactivate: spy
});
$router.config([
{ path: '/user/:name', component: 'deactivate' },
{ path: '/post/:id', component: 'one' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user/brian');
$rootScope.$digest();
$router.navigate('/post/123');
$rootScope.$digest();
expect(spy).toHaveBeenCalledWith({id: '123'}, $http);
}));
it('should change location path', inject(function ($location) {
$router.config([
{ path: '/user', component: 'user' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/user');
$rootScope.$digest();
expect($location.path()).toBe('/user');
}));
// TODO: test injecting $scope
it('should navigate on left-mouse click when a link url matches a route', function () {
$router.config([
{ path: '/', component: 'one' },
{ path: '/two', component: 'two' },
]);
compile('<a href="./two">link</a> | <div ng-outlet></div>');
$rootScope.$digest();
expect(elt.text()).toBe('link | one');
elt.find('a')[0].click();
$rootScope.$digest();
expect(elt.text()).toBe('link | two');
});
it('should not navigate on non-left mouse click when a link url matches a route', inject(function ($router) {
$router.config([
{ path: '/', component: 'one' },
{ path: '/two', component: 'two' },
]);
compile('<a href="./two">link</a> | <div ng-outlet></div>');
$rootScope.$digest();
expect(elt.text()).toBe('link | one');
elt.find('a').triggerHandler({ type: 'click', which: 3 });
$rootScope.$digest();
expect(elt.text()).toBe('link | one');
}));
// See https://github.com/angular/router/issues/206
it('should not navigate a link without an href', function () {
$router.config([
{ path: '/', component: 'one' },
{ path: '/two', component: 'two' },
]);
expect(function() {
compile('<a>link</a>');
$rootScope.$digest();
expect(elt.text()).toBe('link');
elt.find('a')[0].click();
$rootScope.$digest();
}).not.toThrow();
});
it('should change location to the canonical route', inject(function ($location) {
compile('<div ng-outlet></div>');
$router.config([
{ path: '/', redirectTo: '/user' },
{ path: '/user', component: 'user' }
]);
$router.navigate('/');
$rootScope.$digest();
expect($location.path()).toBe('/user');
}));
it('should change location to the canonical route with nested components', inject(function ($location) {
$router.config([
{ path: '/old-parent', redirectTo: '/new-parent' },
{ path: '/new-parent', component: 'childRouter' }
]);
registerComponent('childRouter', '<div>inner { <div ng-outlet></div> }</div>', undefined, [
{ path: '/old-child', redirectTo: '/new-child' },
{ path: '/new-child', component: 'one'},
{ path: '/old-child-two', redirectTo: '/new-child-two' },
{ path: '/new-child-two', component: 'two'}
]);
compile('<div ng-outlet></div>');
$router.navigate('/old-parent/old-child');
$rootScope.$digest();
expect($location.path()).toBe('/new-parent/new-child');
expect(elt.text()).toBe('inner { one }');
$router.navigate('/old-parent/old-child-two');
$rootScope.$digest();
expect($location.path()).toBe('/new-parent/new-child-two');
expect(elt.text()).toBe('inner { two }');
}));
it('should navigate when the location path changes', inject(function ($location) {
$router.config([
{ path: '/one', component: 'one' }
]);
compile('<div ng-outlet></div>');
$location.path('/one');
$rootScope.$digest();
expect(elt.text()).toBe('one');
}));
it('should expose a "navigating" property on $router', function () {
$router.config([
{ path: '/one', component: 'one' }
]);
compile('<div ng-outlet></div>');
$router.navigate('/one');
expect($router.navigating).toBe(true);
$rootScope.$digest();
expect($router.navigating).toBe(false);
});
it('Should allow navigating by component name', function () {
compile('<ng-viewport></ng-viewport>');
$router.config([
{ path: '/', component: 'one' }
]);
$router.navigate('one');
$rootScope.$digest();
expect(elt.text()).toBe('one');
});
function registerComponent(name, template, componentConstructor, routeConfig) {
if (!template) {
template = '';
}
var Ctrl;
componentConstructor = componentConstructor || function() {};
if (angular.isArray(componentConstructor)) {
Ctrl = componentConstructor[componentConstructor.length - 1];
} else if (angular.isFunction(componentConstructor)) {
Ctrl = componentConstructor;
} else if (angular.isObject(componentConstructor)) {
Ctrl = function () {};
if (componentConstructor.canActivate) {
Ctrl.canActivate = componentConstructor.canActivate;
delete componentConstructor.canActivate;
}
Ctrl.prototype = componentConstructor;
componentConstructor = Ctrl;
}
Ctrl.$routeConfig = routeConfig;
$controllerProvider.register(componentControllerName(name), componentConstructor);
put(name, template);
}
function boringController (model, value) {
return function () {
this[model] = value;
};
}
function put (name, template) {
$templateCache.put(componentTemplatePath(name), [200, template, {}]);
}
function compile(template) {
elt = $compile('<div>' + template + '</div>')($rootScope);
$rootScope.$digest();
return elt;
}
});
describe('ngOutlet animations', function () {
var elt,
$animate,
$compile,
$rootScope,
$router,
$templateCache,
$controllerProvider;
beforeEach(function() {
module('ngAnimate');
module('ngAnimateMock');
module('ngNewRouter');
module(function(_$controllerProvider_) {
$controllerProvider = _$controllerProvider_;
});
inject(function(_$animate_, _$compile_, _$rootScope_, _$router_, _$templateCache_) {
$animate = _$animate_;
$compile = _$compile_;
$rootScope = _$rootScope_;
$router = _$router_;
$templateCache = _$templateCache_;
});
put('user', '<div>hello {{user.name}}</div>');
$controllerProvider.register('UserController', function($routeParams) {
this.name = $routeParams.name;
});
});
afterEach(function() {
expect($animate.queue).toEqual([]);
});
it('should work in a simple case', function () {
var item;
compile('<div ng-outlet></div>');
$router.config([
{ path: '/user/:name', component: 'user' }
]);
$router.navigate('/user/brian');
$rootScope.$digest();
expect(elt.text()).toBe('hello brian');
// "user" component enters
item = $animate.queue.shift();
expect(item.event).toBe('enter');
// navigate to pete
$router.navigate('/user/pete');
$rootScope.$digest();
expect(elt.text()).toBe('hello pete');
// "user pete" component enters
item = $animate.queue.shift();
expect(item.event).toBe('enter');
expect(item.element.text()).toBe('hello pete');
// "user brian" component leaves
item = $animate.queue.shift();
expect(item.event).toBe('leave');
expect(item.element.text()).toBe('hello brian');
});
function put (name, template) {
$templateCache.put(componentTemplatePath(name), [200, template, {}]);
}
function compile(template) {
elt = $compile('<div>' + template + '</div>')($rootScope);
$rootScope.$digest();
return elt;
}
});