2015-08-20 17:26:57 -04:00
|
|
|
'use strict';
|
|
|
|
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 18:53:50 -04:00
|
|
|
describe('ngLink', function () {
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
describe('html5Mode enabled', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
runHrefTestsAndExpectPrefix('/', true);
|
2015-08-20 17:26:57 -04:00
|
|
|
});
|
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
describe('html5Mode disabled', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
runHrefTestsAndExpectPrefix('', false, '');
|
2016-03-21 07:49:37 -04:00
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
describe('html5Mode disabled, with hash prefix', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
runHrefTestsAndExpectPrefix('', false, '!');
|
2015-08-20 17:26:57 -04:00
|
|
|
});
|
|
|
|
|
2016-03-21 08:04:15 -04:00
|
|
|
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) {
|
2016-03-21 07:49:37 -04:00
|
|
|
var prefix = html5Mode ? '.' : '#' + hashPrefix;
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
it('should allow linking from the parent to the child', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-21 07:49:37 -04:00
|
|
|
configureRouter([
|
|
|
|
{ path: '/a', component: 'oneCmp' },
|
|
|
|
{ path: '/b', component: 'twoCmp', name: 'Two' }
|
|
|
|
]);
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
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');
|
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
it('should allow linking from the child and the parent', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-21 07:49:37 -04:00
|
|
|
configureRouter([
|
|
|
|
{ path: '/a', component: 'oneCmp' },
|
|
|
|
{ path: '/b', component: 'twoCmp', name: 'Two' }
|
|
|
|
]);
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
var elt = compile('outer { <div ng-outlet></div> }');
|
|
|
|
navigateTo('/b');
|
|
|
|
expect(elt.find('a').attr('href')).toBe(prefix + '/b');
|
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
|
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
it('should allow params in routerLink directive', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-21 07:49:37 -04:00
|
|
|
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' }
|
|
|
|
]);
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
var elt = compile('<div ng-outlet></div>');
|
|
|
|
navigateTo('/a');
|
|
|
|
expect(elt.find('a').attr('href')).toBe(prefix + '/b/lol');
|
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 18:53:50 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
it('should update the href of links with bound params', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-21 07:49:37 -04:00
|
|
|
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' }
|
|
|
|
]);
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
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');
|
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
|
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
it('should navigate on left-mouse click when a link url matches a route', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-21 07:49:37 -04:00
|
|
|
configureRouter([
|
|
|
|
{ path: '/', component: 'oneCmp' },
|
|
|
|
{ path: '/two', component: 'twoCmp', name: 'Two'}
|
|
|
|
]);
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
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');
|
2015-08-20 17:26:57 -04:00
|
|
|
|
|
|
|
elt.find('a')[0].click();
|
2016-03-04 03:14:27 -05:00
|
|
|
inject(function($rootScope) { $rootScope.$digest(); });
|
2016-03-21 07:49:37 -04:00
|
|
|
expect(elt.text()).toBe('link | two');
|
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-02-03 22:50:51 -05:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
it('should not navigate on non-left mouse click when a link url matches a route', function() {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-21 07:49:37 -04:00
|
|
|
configureRouter([
|
|
|
|
{ path: '/', component: 'oneCmp' },
|
|
|
|
{ path: '/two', component: 'twoCmp', name: 'Two'}
|
|
|
|
]);
|
2016-03-04 03:14:27 -05:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
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');
|
|
|
|
});
|
2016-02-03 22:50:51 -05:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
|
|
|
|
// See https://github.com/angular/router/issues/206
|
|
|
|
it('should not navigate a link without an href', function () {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-04 03:14:27 -05:00
|
|
|
configureRouter([
|
2016-03-21 07:49:37 -04:00
|
|
|
{ path: '/', component: 'oneCmp' },
|
|
|
|
{ path: '/two', component: 'twoCmp', name: 'Two'}
|
2016-03-04 03:14:27 -05:00
|
|
|
]);
|
2016-03-21 07:49:37 -04:00
|
|
|
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();
|
2016-03-04 03:14:27 -05:00
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
|
2016-03-21 07:49:37 -04:00
|
|
|
it('should add an ng-link-active class on the current link', function() {
|
2016-03-21 08:04:15 -04:00
|
|
|
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
|
2016-03-21 07:49:37 -04:00
|
|
|
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');
|
|
|
|
});
|
2016-05-20 12:48:03 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
2016-03-21 07:49:37 -04:00
|
|
|
}
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 18:53:50 -04:00
|
|
|
|
2016-03-04 03:14:27 -05:00
|
|
|
function registerComponent(name, template, controller) {
|
|
|
|
module(function($compileProvider) {
|
|
|
|
$compileProvider.component(name, {
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 18:53:50 -04:00
|
|
|
template: template,
|
|
|
|
controller: controller
|
2016-03-04 03:14:27 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup(config) {
|
2016-03-21 08:04:15 -04:00
|
|
|
module(function($provide) {
|
|
|
|
$provide.decorator('$browser', function($delegate) {
|
|
|
|
$delegate.baseHref = function() { return config.baseHref; };
|
|
|
|
return $delegate;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
module('ngComponentRouter');
|
2016-03-04 03:14:27 -05:00
|
|
|
module(function($locationProvider) {
|
2016-03-21 07:49:37 -04:00
|
|
|
$locationProvider.html5Mode(config.html5Mode);
|
|
|
|
$locationProvider.hashPrefix(config.hashPrefix);
|
2016-03-04 03:14:27 -05:00
|
|
|
});
|
|
|
|
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);
|
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function compile(template) {
|
2016-03-04 03:14:27 -05:00
|
|
|
var elt;
|
|
|
|
inject(function($compile, $rootScope) {
|
|
|
|
elt = $compile('<div>' + template + '</div>')($rootScope);
|
|
|
|
$rootScope.$digest();
|
|
|
|
});
|
2015-08-20 17:26:57 -04:00
|
|
|
return elt;
|
|
|
|
}
|
2016-03-04 03:14:27 -05:00
|
|
|
|
|
|
|
function navigateTo(url) {
|
|
|
|
inject(function($rootRouter, $rootScope) {
|
|
|
|
$rootRouter.navigateByUrl(url);
|
|
|
|
$rootScope.$digest();
|
|
|
|
});
|
|
|
|
}
|
2015-08-20 17:26:57 -04:00
|
|
|
});
|