2015-11-01 15:31:36 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('router', function () {
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
var elt, testMod;
|
2015-11-01 15:31:36 -05:00
|
|
|
beforeEach(function () {
|
2016-03-07 17:09:37 -05:00
|
|
|
testMod = angular.module('testMod', ['ngComponentRouter'])
|
|
|
|
.value('$routerRootComponent', 'app');
|
2015-11-01 15:31:36 -05:00
|
|
|
});
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
it('should work with a provided root component', function() {
|
|
|
|
|
2015-11-01 15:31:36 -05:00
|
|
|
registerComponent('homeCmp', {
|
|
|
|
template: 'Home'
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
module('testMod');
|
|
|
|
compileApp();
|
2015-11-01 15:31:36 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
inject(function($location, $rootScope) {
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
expect(elt.text()).toBe('Home');
|
|
|
|
});
|
|
|
|
});
|
2015-11-01 15:31:36 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
it('should bind the component to the current router', function() {
|
2016-02-07 15:40:00 -05:00
|
|
|
var router;
|
|
|
|
registerComponent('homeCmp', {
|
2016-02-22 09:01:02 -05:00
|
|
|
bindings: { $router: '=' },
|
2016-02-07 15:40:00 -05:00
|
|
|
controller: function($scope, $element) {
|
|
|
|
this.$routerOnActivate = function() {
|
2016-02-22 09:01:02 -05:00
|
|
|
router = this.$router;
|
2016-02-07 15:40:00 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
template: 'Home'
|
2015-11-01 15:31:36 -05:00
|
|
|
});
|
|
|
|
|
2016-02-07 15:40:00 -05:00
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
module('testMod');
|
|
|
|
compileApp();
|
2016-02-07 15:40:00 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
inject(function($location, $rootScope) {
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
var homeElement = elt.find('home-cmp');
|
|
|
|
expect(homeElement.text()).toBe('Home');
|
|
|
|
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
|
|
|
|
expect(router).toBeDefined();
|
|
|
|
})
|
|
|
|
});
|
2016-02-07 15:40:00 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
it('should work when an async route is provided route data', function() {
|
|
|
|
registerComponent('homeCmp', {
|
|
|
|
template: 'Home ({{$ctrl.isAdmin}})',
|
2016-01-31 15:52:19 -05:00
|
|
|
$routerOnActivate: function(next, prev) {
|
|
|
|
this.isAdmin = next.routeData.data.isAdmin;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
registerComponent('app', {
|
2016-01-31 15:52:19 -05:00
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
2016-03-07 17:09:37 -05:00
|
|
|
{ path: '/', loader: function($q) { return $q.when('homeCmp'); }, data: { isAdmin: true } }
|
2016-01-31 15:52:19 -05:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
module('testMod');
|
|
|
|
compileApp();
|
2016-01-31 15:52:19 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
inject(function($location, $rootScope) {
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
expect(elt.text()).toBe('Home (true)');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with a templateUrl component', function() {
|
2016-01-31 15:52:19 -05:00
|
|
|
|
2016-02-10 19:59:26 -05:00
|
|
|
var $routerOnActivate = jasmine.createSpy('$routerOnActivate');
|
2016-03-07 17:09:37 -05:00
|
|
|
|
2016-02-10 19:59:26 -05:00
|
|
|
registerComponent('homeCmp', {
|
|
|
|
templateUrl: 'homeCmp.html',
|
|
|
|
$routerOnActivate: $routerOnActivate
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
module('testMod');
|
2016-02-10 19:59:26 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
inject(function($location, $rootScope, $httpBackend) {
|
2016-02-10 19:59:26 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
$httpBackend.expectGET('homeCmp.html').respond('Home');
|
|
|
|
|
|
|
|
compileApp();
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
$httpBackend.flush();
|
|
|
|
var homeElement = elt.find('home-cmp');
|
|
|
|
expect(homeElement.text()).toBe('Home');
|
|
|
|
expect($routerOnActivate).toHaveBeenCalled();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should provide the current instruction', function() {
|
2016-02-18 10:25:03 -05:00
|
|
|
registerComponent('homeCmp', {
|
|
|
|
template: 'Home ({{homeCmp.isAdmin}})'
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp', name: 'Home' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
module('testMod');
|
|
|
|
|
|
|
|
inject(function($rootScope, $rootRouter, $location) {
|
|
|
|
compileApp();
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
var instruction = $rootRouter.generate(['/Home']);
|
|
|
|
expect($rootRouter.currentInstruction).toEqual(instruction);
|
|
|
|
});
|
|
|
|
});
|
2016-02-18 10:25:03 -05:00
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
it('should provide the root level router', function() {
|
2016-02-18 10:25:03 -05:00
|
|
|
registerComponent('homeCmp', {
|
|
|
|
template: 'Home ({{homeCmp.isAdmin}})',
|
|
|
|
bindings: {
|
|
|
|
$router: '<'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp', name: 'Home' }
|
|
|
|
]
|
|
|
|
});
|
2016-03-07 17:09:37 -05:00
|
|
|
|
|
|
|
module('testMod');
|
|
|
|
|
|
|
|
inject(function($rootScope, $rootRouter, $location) {
|
|
|
|
compileApp();
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
var homeElement = elt.find('home-cmp');
|
|
|
|
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
|
|
|
|
});
|
|
|
|
});
|
2015-11-01 15:31:36 -05:00
|
|
|
|
2016-02-07 15:40:00 -05:00
|
|
|
function registerComponent(name, options) {
|
2015-11-01 15:31:36 -05:00
|
|
|
|
2016-02-07 15:40:00 -05:00
|
|
|
var definition = {
|
|
|
|
bindings: options.bindings,
|
|
|
|
controller: getController(options),
|
2016-02-10 19:59:26 -05:00
|
|
|
};
|
2016-02-10 20:00:34 -05:00
|
|
|
if (options.template) definition.template = options.template;
|
|
|
|
if (options.templateUrl) definition.templateUrl = options.templateUrl;
|
2016-02-10 19:59:26 -05:00
|
|
|
|
2016-02-07 15:40:00 -05:00
|
|
|
applyStaticProperties(definition, options);
|
2016-03-07 17:09:37 -05:00
|
|
|
angular.module('testMod').component(name, definition);
|
2015-11-01 15:31:36 -05:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:09:37 -05:00
|
|
|
function compileApp() {
|
|
|
|
inject(function($compile, $rootScope) {
|
|
|
|
elt = $compile('<div><app></app</div>')($rootScope);
|
|
|
|
$rootScope.$digest();
|
|
|
|
});
|
2015-11-01 15:31:36 -05:00
|
|
|
return elt;
|
|
|
|
}
|
2016-02-07 15:40:00 -05:00
|
|
|
|
|
|
|
function getController(options) {
|
|
|
|
var controller = options.controller || function () {};
|
|
|
|
[
|
|
|
|
'$routerOnActivate', '$routerOnDeactivate',
|
|
|
|
'$routerOnReuse', '$routerCanReuse',
|
|
|
|
'$routerCanDeactivate'
|
|
|
|
].forEach(function (hookName) {
|
|
|
|
if (options[hookName]) {
|
|
|
|
controller.prototype[hookName] = options[hookName];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyStaticProperties(target, options) {
|
|
|
|
['$canActivate', '$routeConfig'].forEach(function(property) {
|
|
|
|
if (options[property]) {
|
|
|
|
target[property] = options[property];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-01-31 15:52:19 -05:00
|
|
|
});
|