2015-11-01 15:31:36 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('router', function () {
|
|
|
|
|
|
|
|
var elt,
|
|
|
|
$compile,
|
|
|
|
$rootScope,
|
2016-02-17 02:47:49 -05:00
|
|
|
$rootRouter,
|
2015-11-01 15:31:36 -05:00
|
|
|
$compileProvider;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
module('ng');
|
|
|
|
module('ngComponentRouter');
|
|
|
|
module(function($provide) {
|
|
|
|
$provide.value('$routerRootComponent', 'app');
|
|
|
|
});
|
|
|
|
module(function (_$compileProvider_) {
|
|
|
|
$compileProvider = _$compileProvider_;
|
|
|
|
});
|
|
|
|
|
2016-02-17 02:47:49 -05:00
|
|
|
inject(function (_$compile_, _$rootScope_, _$rootRouter_) {
|
2015-11-01 15:31:36 -05:00
|
|
|
$compile = _$compile_;
|
|
|
|
$rootScope = _$rootScope_;
|
2016-02-17 02:47:49 -05:00
|
|
|
$rootRouter = _$rootRouter_;
|
2015-11-01 15:31:36 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with a provided root component', inject(function($location) {
|
|
|
|
registerComponent('homeCmp', {
|
|
|
|
template: 'Home'
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
compile('<app></app>');
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
expect(elt.text()).toBe('Home');
|
|
|
|
}));
|
|
|
|
|
2016-02-07 15:40:00 -05:00
|
|
|
it('should bind the component to the current router', inject(function($location) {
|
|
|
|
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' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
compile('<app></app>');
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
var homeElement = elt.find('home-cmp');
|
|
|
|
expect(homeElement.text()).toBe('Home');
|
2016-02-22 09:01:02 -05:00
|
|
|
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
|
2016-02-07 15:40:00 -05:00
|
|
|
expect(router).toBeDefined();
|
|
|
|
}));
|
|
|
|
|
2016-01-31 15:52:19 -05:00
|
|
|
it('should work when an async route is provided route data', inject(function($location, $q) {
|
|
|
|
registerDirective('homeCmp', {
|
|
|
|
template: 'Home ({{homeCmp.isAdmin}})',
|
|
|
|
$routerOnActivate: function(next, prev) {
|
|
|
|
this.isAdmin = next.routeData.data.isAdmin;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
registerDirective('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', loader: function() { return $q.when('homeCmp'); }, data: { isAdmin: true } }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
compile('<app></app>');
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
expect(elt.text()).toBe('Home (true)');
|
|
|
|
}));
|
|
|
|
|
2016-02-10 19:59:26 -05:00
|
|
|
it('should work with a templateUrl component', inject(function($location, $httpBackend) {
|
|
|
|
var $routerOnActivate = jasmine.createSpy('$routerOnActivate');
|
|
|
|
$httpBackend.expectGET('homeCmp.html').respond('Home');
|
|
|
|
registerComponent('homeCmp', {
|
|
|
|
templateUrl: 'homeCmp.html',
|
|
|
|
$routerOnActivate: $routerOnActivate
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
compile('<app></app>');
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
$httpBackend.flush();
|
|
|
|
var homeElement = elt.find('home-cmp');
|
|
|
|
expect(homeElement.text()).toBe('Home');
|
|
|
|
expect($routerOnActivate).toHaveBeenCalled();
|
|
|
|
}));
|
|
|
|
|
2016-02-18 10:25:03 -05:00
|
|
|
it('should provide the current instruction', inject(function($location, $q) {
|
|
|
|
registerComponent('homeCmp', {
|
|
|
|
template: 'Home ({{homeCmp.isAdmin}})'
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp', name: 'Home' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
compile('<app></app>');
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
var instruction = $rootRouter.generate(['/Home']);
|
|
|
|
expect($rootRouter.currentInstruction).toEqual(instruction);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should provide the root level router', inject(function($location, $q) {
|
|
|
|
registerComponent('homeCmp', {
|
|
|
|
template: 'Home ({{homeCmp.isAdmin}})',
|
|
|
|
bindings: {
|
|
|
|
$router: '<'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
registerComponent('app', {
|
|
|
|
template: '<div ng-outlet></div>',
|
|
|
|
$routeConfig: [
|
|
|
|
{ path: '/', component: 'homeCmp', name: 'Home' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
compile('<app></app>');
|
|
|
|
|
|
|
|
$location.path('/');
|
|
|
|
$rootScope.$digest();
|
|
|
|
var homeElement = elt.find('home-cmp');
|
|
|
|
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
|
|
|
|
}));
|
2016-02-10 19:59:26 -05:00
|
|
|
|
2016-02-07 15:40:00 -05:00
|
|
|
function registerDirective(name, options) {
|
2015-11-01 15:31:36 -05:00
|
|
|
function factory() {
|
|
|
|
return {
|
|
|
|
template: options.template || '',
|
|
|
|
controllerAs: name,
|
2016-02-07 15:40:00 -05:00
|
|
|
controller: getController(options)
|
2015-11-01 15:31:36 -05:00
|
|
|
};
|
|
|
|
}
|
2016-02-07 15:40:00 -05:00
|
|
|
applyStaticProperties(factory, options);
|
|
|
|
$compileProvider.directive(name, factory);
|
|
|
|
}
|
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);
|
|
|
|
$compileProvider.component(name, definition);
|
2015-11-01 15:31:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function compile(template) {
|
|
|
|
elt = $compile('<div>' + template + '</div>')($rootScope);
|
|
|
|
$rootScope.$digest();
|
|
|
|
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
|
|
|
});
|