|
2 | 2 |
|
3 | 3 | describe('ngLink', function () { |
4 | 4 |
|
5 | | - var elt, |
6 | | - $compile, |
7 | | - $rootScope, |
8 | | - $rootRouter, |
9 | | - $compileProvider; |
10 | | - |
11 | | - beforeEach(function () { |
12 | | - module('ng'); |
13 | | - module('ngComponentRouter'); |
14 | | - module(function (_$compileProvider_) { |
15 | | - $compileProvider = _$compileProvider_; |
16 | | - }); |
17 | | - |
18 | | - inject(function (_$compile_, _$rootScope_, _$rootRouter_) { |
19 | | - $compile = _$compile_; |
20 | | - $rootScope = _$rootScope_; |
21 | | - $rootRouter = _$rootRouter_; |
22 | | - }); |
23 | | - |
24 | | - registerComponent('userCmp', '<div>hello {{userCmp.$routeParams.name}}</div>', function () {}); |
25 | | - registerComponent('oneCmp', '<div>{{oneCmp.number}}</div>', function () {this.number = 'one'}); |
26 | | - registerComponent('twoCmp', '<div><a ng-link="[\'/Two\']">{{twoCmp.number}}</a></div>', function () {this.number = 'two'}); |
27 | | - }); |
28 | | - |
29 | | - |
30 | 5 | it('should allow linking from the parent to the child', function () { |
31 | | - $rootRouter.config([ |
| 6 | + setup(); |
| 7 | + configureRouter([ |
32 | 8 | { path: '/a', component: 'oneCmp' }, |
33 | 9 | { path: '/b', component: 'twoCmp', name: 'Two' } |
34 | 10 | ]); |
35 | | - compile('<a ng-link="[\'/Two\']">link</a> | outer { <div ng-outlet></div> }'); |
36 | | - |
37 | | - $rootRouter.navigateByUrl('/a'); |
38 | | - $rootScope.$digest(); |
39 | 11 |
|
| 12 | + var elt = compile('<a ng-link="[\'/Two\']">link</a> | outer { <div ng-outlet></div> }'); |
| 13 | + navigateTo('/a'); |
40 | 14 | expect(elt.find('a').attr('href')).toBe('./b'); |
41 | 15 | }); |
42 | 16 |
|
43 | 17 | it('should allow linking from the child and the parent', function () { |
44 | | - $rootRouter.config([ |
| 18 | + setup(); |
| 19 | + configureRouter([ |
45 | 20 | { path: '/a', component: 'oneCmp' }, |
46 | 21 | { path: '/b', component: 'twoCmp', name: 'Two' } |
47 | 22 | ]); |
48 | | - compile('outer { <div ng-outlet></div> }'); |
49 | | - |
50 | | - $rootRouter.navigateByUrl('/b'); |
51 | | - $rootScope.$digest(); |
52 | 23 |
|
| 24 | + var elt = compile('outer { <div ng-outlet></div> }'); |
| 25 | + navigateTo('/b'); |
53 | 26 | expect(elt.find('a').attr('href')).toBe('./b'); |
54 | 27 | }); |
55 | 28 |
|
56 | 29 |
|
57 | 30 | it('should allow params in routerLink directive', function () { |
| 31 | + setup(); |
58 | 32 | registerComponent('twoLinkCmp', '<div><a ng-link="[\'/Two\', {param: \'lol\'}]">{{twoLinkCmp.number}}</a></div>', function () {this.number = 'two'}); |
59 | | - |
60 | | - $rootRouter.config([ |
| 33 | + configureRouter([ |
61 | 34 | { path: '/a', component: 'twoLinkCmp' }, |
62 | 35 | { path: '/b/:param', component: 'twoCmp', name: 'Two' } |
63 | 36 | ]); |
64 | | - compile('<div ng-outlet></div>'); |
65 | | - |
66 | | - $rootRouter.navigateByUrl('/a'); |
67 | | - $rootScope.$digest(); |
68 | 37 |
|
| 38 | + var elt = compile('<div ng-outlet></div>'); |
| 39 | + navigateTo('/a'); |
69 | 40 | expect(elt.find('a').attr('href')).toBe('./b/lol'); |
70 | 41 | }); |
71 | 42 |
|
72 | 43 |
|
73 | 44 | it('should update the href of links with bound params', function () { |
74 | | - registerComponent('twoLinkCmp', '<div><a ng-link="[\'/Two\', {param: twoLinkCmp.number}]">{{twoLinkCmp.number}}</a></div>', function () {this.number = 'param'}); |
75 | | - $rootRouter.config([ |
| 45 | + setup(); |
| 46 | + registerComponent('twoLinkCmp', '<div><a ng-link="[\'/Two\', {param: $ctrl.number}]">{{$ctrl.number}}</a></div>', function () {this.number = 43}); |
| 47 | + configureRouter([ |
76 | 48 | { path: '/a', component: 'twoLinkCmp' }, |
77 | 49 | { path: '/b/:param', component: 'twoCmp', name: 'Two' } |
78 | 50 | ]); |
79 | | - compile('<div ng-outlet></div>'); |
80 | | - |
81 | | - $rootRouter.navigateByUrl('/a'); |
82 | | - $rootScope.$digest(); |
83 | 51 |
|
84 | | - expect(elt.find('a').attr('href')).toBe('./b/param'); |
| 52 | + var elt = compile('<div ng-outlet></div>'); |
| 53 | + navigateTo('/a'); |
| 54 | + expect(elt.find('a').text()).toBe('43'); |
| 55 | + expect(elt.find('a').attr('href')).toBe('./b/43'); |
85 | 56 | }); |
86 | 57 |
|
87 | 58 |
|
88 | 59 | it('should navigate on left-mouse click when a link url matches a route', function () { |
89 | | - $rootRouter.config([ |
| 60 | + setup(); |
| 61 | + configureRouter([ |
90 | 62 | { path: '/', component: 'oneCmp' }, |
91 | 63 | { path: '/two', component: 'twoCmp', name: 'Two'} |
92 | 64 | ]); |
93 | 65 |
|
94 | | - compile('<a ng-link="[\'/Two\']">link</a> | <div ng-outlet></div>'); |
95 | | - $rootScope.$digest(); |
| 66 | + var elt = compile('<a ng-link="[\'/Two\']">link</a> | <div ng-outlet></div>'); |
96 | 67 | expect(elt.text()).toBe('link | one'); |
97 | | - |
98 | 68 | expect(elt.find('a').attr('href')).toBe('./two'); |
99 | 69 |
|
100 | 70 | elt.find('a')[0].click(); |
101 | | - |
102 | | - $rootScope.$digest(); |
| 71 | + inject(function($rootScope) { $rootScope.$digest(); }); |
103 | 72 | expect(elt.text()).toBe('link | two'); |
104 | 73 | }); |
105 | 74 |
|
106 | 75 |
|
107 | | - it('should not navigate on non-left mouse click when a link url matches a route', inject(function ($rootRouter) { |
108 | | - $rootRouter.config([ |
| 76 | + it('should not navigate on non-left mouse click when a link url matches a route', function() { |
| 77 | + setup(); |
| 78 | + configureRouter([ |
109 | 79 | { path: '/', component: 'oneCmp' }, |
110 | 80 | { path: '/two', component: 'twoCmp', name: 'Two'} |
111 | 81 | ]); |
112 | 82 |
|
113 | | - compile('<a ng-link="[\'/Two\']">link</a> | <div ng-outlet></div>'); |
114 | | - $rootScope.$digest(); |
| 83 | + var elt = compile('<a ng-link="[\'/Two\']">link</a> | <div ng-outlet></div>'); |
115 | 84 | expect(elt.text()).toBe('link | one'); |
116 | 85 | elt.find('a').triggerHandler({ type: 'click', which: 3 }); |
117 | | - |
118 | | - $rootScope.$digest(); |
| 86 | + inject(function($rootScope) { $rootScope.$digest(); }); |
119 | 87 | expect(elt.text()).toBe('link | one'); |
120 | | - })); |
| 88 | + }); |
121 | 89 |
|
122 | 90 |
|
123 | 91 | // See https://github.com/angular/router/issues/206 |
124 | 92 | it('should not navigate a link without an href', function () { |
125 | | - $rootRouter.config([ |
| 93 | + setup(); |
| 94 | + configureRouter([ |
126 | 95 | { path: '/', component: 'oneCmp' }, |
127 | 96 | { path: '/two', component: 'twoCmp', name: 'Two'} |
128 | 97 | ]); |
129 | 98 | expect(function () { |
130 | | - compile('<a>link</a>'); |
131 | | - $rootScope.$digest(); |
| 99 | + var elt = compile('<a>link</a>'); |
132 | 100 | expect(elt.text()).toBe('link'); |
133 | 101 | elt.find('a')[0].click(); |
134 | | - $rootScope.$digest(); |
| 102 | + inject(function($rootScope) { $rootScope.$digest(); }); |
135 | 103 | }).not.toThrow(); |
136 | 104 | }); |
137 | 105 |
|
138 | | - it('should add an ng-link-active class on the current link', inject(function ($rootRouter) { |
139 | | - $rootRouter.config([ |
| 106 | + it('should add an ng-link-active class on the current link', function() { |
| 107 | + setup(); |
| 108 | + configureRouter([ |
140 | 109 | { path: '/', component: 'oneCmp', name: 'One' } |
141 | 110 | ]); |
142 | 111 |
|
143 | | - compile('<a ng-link="[\'/One\']">one</a> | <div ng-outlet></div>'); |
144 | | - $rootScope.$digest(); |
145 | | - |
146 | | - $rootRouter.navigateByUrl('/'); |
147 | | - $rootScope.$digest(); |
148 | | - |
| 112 | + var elt = compile('<a ng-link="[\'/One\']">one</a> | <div ng-outlet></div>'); |
| 113 | + navigateTo('/'); |
149 | 114 | expect(elt.find('a').attr('class')).toBe('ng-link-active'); |
150 | | - })); |
| 115 | + }); |
| 116 | + |
151 | 117 |
|
| 118 | + describe('html5Mode disabled', function () { |
| 119 | + it('should prepend href with a hash', function () { |
| 120 | + setup({ html5Mode: false }); |
| 121 | + module(function($locationProvider) { |
| 122 | + $locationProvider.html5Mode(false); |
| 123 | + }); |
| 124 | + configureRouter([ |
| 125 | + { path: '/b', component: 'twoCmp', name: 'Two' } |
| 126 | + ]); |
| 127 | + var elt = compile('<a ng-link="[\'/Two\']">link</a>'); |
| 128 | + expect(elt.find('a').attr('href')).toBe('#/b'); |
| 129 | + }); |
| 130 | + }); |
152 | 131 |
|
153 | | - function registerComponent(name, template, config) { |
154 | | - var controller = function () {}; |
155 | 132 |
|
156 | | - function factory() { |
157 | | - return { |
| 133 | + function registerComponent(name, template, controller) { |
| 134 | + module(function($compileProvider) { |
| 135 | + $compileProvider.component(name, { |
158 | 136 | template: template, |
159 | | - controllerAs: name, |
160 | 137 | controller: controller |
161 | | - }; |
162 | | - } |
163 | | - |
164 | | - if (!template) { |
165 | | - template = ''; |
166 | | - } |
167 | | - if (angular.isArray(config)) { |
168 | | - factory.annotations = [new angular.annotations.RouteConfig(config)]; |
169 | | - } else if (typeof config === 'function') { |
170 | | - controller = config; |
171 | | - } else if (typeof config === 'object') { |
172 | | - if (config.canActivate) { |
173 | | - controller.$canActivate = config.canActivate; |
174 | | - } |
175 | | - } |
176 | | - $compileProvider.directive(name, factory); |
| 138 | + }); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + function setup(config) { |
| 143 | + var html5Mode = !(config && config.html5Mode === false); |
| 144 | + module('ngComponentRouter') |
| 145 | + module(function($locationProvider) { |
| 146 | + $locationProvider.html5Mode(html5Mode); |
| 147 | + }); |
| 148 | + registerComponent('userCmp', '<div>hello {{$ctrl.$routeParams.name}}</div>', function () {}); |
| 149 | + registerComponent('oneCmp', '<div>{{$ctrl.number}}</div>', function () {this.number = 'one'}); |
| 150 | + registerComponent('twoCmp', '<div><a ng-link="[\'/Two\']">{{$ctrl.number}}</a></div>', function () {this.number = 'two'}); |
| 151 | + } |
| 152 | + |
| 153 | + function configureRouter(routeConfig) { |
| 154 | + inject(function($rootRouter) { |
| 155 | + $rootRouter.config(routeConfig); |
| 156 | + }); |
177 | 157 | } |
178 | 158 |
|
179 | 159 | function compile(template) { |
180 | | - elt = $compile('<div>' + template + '</div>')($rootScope); |
181 | | - $rootScope.$digest(); |
| 160 | + var elt; |
| 161 | + inject(function($compile, $rootScope) { |
| 162 | + elt = $compile('<div>' + template + '</div>')($rootScope); |
| 163 | + $rootScope.$digest(); |
| 164 | + }); |
182 | 165 | return elt; |
183 | 166 | } |
| 167 | + |
| 168 | + function navigateTo(url) { |
| 169 | + inject(function($rootRouter, $rootScope) { |
| 170 | + $rootRouter.navigateByUrl(url); |
| 171 | + $rootScope.$digest(); |
| 172 | + }); |
| 173 | + } |
184 | 174 | }); |
0 commit comments