forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng_link_spec.js
212 lines (175 loc) · 7.31 KB
/
ng_link_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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
'use strict';
describe('ngLink', function () {
describe('html5Mode enabled', function () {
runHrefTestsAndExpectPrefix('/', true);
});
describe('html5Mode disabled', function () {
runHrefTestsAndExpectPrefix('', false, '');
});
describe('html5Mode disabled, with hash prefix', function () {
runHrefTestsAndExpectPrefix('', false, '!');
});
describe('html5Mode enabled', function () {
runHrefTestsAndExpectPrefix('/moo', true);
});
describe('html5Mode disabled', function () {
runHrefTestsAndExpectPrefix('/moo', false, '');
});
describe('html5Mode disabled, with hash prefix', function () {
runHrefTestsAndExpectPrefix('/moo', false, '!');
});
function runHrefTestsAndExpectPrefix(baseHref, html5Mode, hashPrefix) {
var prefix = html5Mode ? '.' : '#' + hashPrefix;
it('should allow linking from the parent to the child', function () {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
configureRouter([
{ path: '/a', component: 'oneCmp' },
{ path: '/b', component: 'twoCmp', name: 'Two' }
]);
var elt = compile('<a ng-link="[\'/Two\']">link</a> | outer { <div ng-outlet></div> }');
navigateTo('/a');
expect(elt.find('a').attr('href')).toBe(prefix + '/b');
});
it('should allow linking from the child and the parent', function () {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
configureRouter([
{ path: '/a', component: 'oneCmp' },
{ path: '/b', component: 'twoCmp', name: 'Two' }
]);
var elt = compile('outer { <div ng-outlet></div> }');
navigateTo('/b');
expect(elt.find('a').attr('href')).toBe(prefix + '/b');
});
it('should allow params in routerLink directive', function () {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
registerComponent('twoLinkCmp', '<div><a ng-link="[\'/Two\', {param: \'lol\'}]">{{twoLinkCmp.number}}</a></div>', function () {this.number = 'two';});
configureRouter([
{ path: '/a', component: 'twoLinkCmp' },
{ path: '/b/:param', component: 'twoCmp', name: 'Two' }
]);
var elt = compile('<div ng-outlet></div>');
navigateTo('/a');
expect(elt.find('a').attr('href')).toBe(prefix + '/b/lol');
});
it('should update the href of links with bound params', function () {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
registerComponent('twoLinkCmp', '<div><a ng-link="[\'/Two\', {param: $ctrl.number}]">{{$ctrl.number}}</a></div>', function () {this.number = 43;});
configureRouter([
{ path: '/a', component: 'twoLinkCmp' },
{ path: '/b/:param', component: 'twoCmp', name: 'Two' }
]);
var elt = compile('<div ng-outlet></div>');
navigateTo('/a');
expect(elt.find('a').text()).toBe('43');
expect(elt.find('a').attr('href')).toBe(prefix + '/b/43');
});
it('should navigate on left-mouse click when a link url matches a route', function () {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
configureRouter([
{ path: '/', component: 'oneCmp' },
{ path: '/two', component: 'twoCmp', name: 'Two'}
]);
var elt = compile('<a ng-link="[\'/Two\']">link</a> | <div ng-outlet></div>');
expect(elt.text()).toBe('link | one');
expect(elt.find('a').attr('href')).toBe(prefix + '/two');
elt.find('a')[0].click();
inject(function($rootScope) { $rootScope.$digest(); });
expect(elt.text()).toBe('link | two');
});
it('should not navigate on non-left mouse click when a link url matches a route', function() {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
configureRouter([
{ path: '/', component: 'oneCmp' },
{ path: '/two', component: 'twoCmp', name: 'Two'}
]);
var elt = compile('<a ng-link="[\'/Two\']">link</a> | <div ng-outlet></div>');
expect(elt.text()).toBe('link | one');
elt.find('a').triggerHandler({ type: 'click', which: 3 });
inject(function($rootScope) { $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 () {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
configureRouter([
{ path: '/', component: 'oneCmp' },
{ path: '/two', component: 'twoCmp', name: 'Two'}
]);
expect(function () {
var elt = compile('<a>link</a>');
expect(elt.text()).toBe('link');
elt.find('a')[0].click();
inject(function($rootScope) { $rootScope.$digest(); });
}).not.toThrow();
});
it('should add an ng-link-active class on the current link', function() {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
configureRouter([
{ path: '/', component: 'oneCmp', name: 'One' }
]);
var elt = compile('<a ng-link="[\'/One\']">one</a> | <div ng-outlet></div>');
navigateTo('/');
expect(elt.find('a').attr('class')).toBe('ng-link-active');
});
it('should not add a href if link attributes are undefined', function () {
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
configureRouter([
{ path: '/a', component: 'oneCmp' },
{ path: '/b', component: 'twoCmp', name: 'Two' }
]);
var elt = compile('<a ng-link="something.undefined">link</a> | outer { <div ng-outlet></div> }');
navigateTo('/a');
expect(elt.find('a').hasAttr('href')).toBeFalsy();
});
}
function registerComponent(name, template, controller) {
module(function($compileProvider) {
$compileProvider.component(name, {
template: template,
controller: controller
});
});
}
function setup(config) {
module(function($provide) {
$provide.decorator('$browser', function($delegate) {
$delegate.baseHref = function() { return config.baseHref; };
return $delegate;
});
});
module('ngComponentRouter');
module(function($locationProvider) {
$locationProvider.html5Mode(config.html5Mode);
$locationProvider.hashPrefix(config.hashPrefix);
});
registerComponent('userCmp', '<div>hello {{$ctrl.$routeParams.name}}</div>', function () {});
registerComponent('oneCmp', '<div>{{$ctrl.number}}</div>', function () {this.number = 'one';});
registerComponent('twoCmp', '<div><a ng-link="[\'/Two\']">{{$ctrl.number}}</a></div>', function () {this.number = 'two';});
}
function configureRouter(routeConfig) {
inject(function($rootRouter) {
$rootRouter.config(routeConfig);
});
}
function compile(template) {
var elt;
inject(function($compile, $rootScope) {
elt = $compile('<div>' + template + '</div>')($rootScope);
$rootScope.$digest();
});
return elt;
}
function navigateTo(url) {
inject(function($rootRouter, $rootScope) {
$rootRouter.navigateByUrl(url);
$rootScope.$digest();
});
}
});