test(angular_1_router): fix router_spec tests
These tests were registering new components after the application had been bootstrapped, which is not a valid use case for synchronous routes in Angular 1. In particular it was registering the "root" component, which caused the `$rootRouter` to blow up, when it was instantiated, pointing to a root component that did not yet exist.
This commit is contained in:
parent
adef68b4d6
commit
83f0e7c975
|
@ -2,30 +2,14 @@
|
||||||
|
|
||||||
describe('router', function () {
|
describe('router', function () {
|
||||||
|
|
||||||
var elt,
|
var elt, testMod;
|
||||||
$compile,
|
|
||||||
$rootScope,
|
|
||||||
$rootRouter,
|
|
||||||
$compileProvider;
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
module('ng');
|
testMod = angular.module('testMod', ['ngComponentRouter'])
|
||||||
module('ngComponentRouter');
|
.value('$routerRootComponent', 'app');
|
||||||
module(function($provide) {
|
|
||||||
$provide.value('$routerRootComponent', 'app');
|
|
||||||
});
|
|
||||||
module(function (_$compileProvider_) {
|
|
||||||
$compileProvider = _$compileProvider_;
|
|
||||||
});
|
|
||||||
|
|
||||||
inject(function (_$compile_, _$rootScope_, _$rootRouter_) {
|
|
||||||
$compile = _$compile_;
|
|
||||||
$rootScope = _$rootScope_;
|
|
||||||
$rootRouter = _$rootRouter_;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with a provided root component', inject(function($location) {
|
it('should work with a provided root component', function() {
|
||||||
|
|
||||||
registerComponent('homeCmp', {
|
registerComponent('homeCmp', {
|
||||||
template: 'Home'
|
template: 'Home'
|
||||||
});
|
});
|
||||||
|
@ -37,14 +21,17 @@ describe('router', function () {
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
compile('<app></app>');
|
module('testMod');
|
||||||
|
compileApp();
|
||||||
|
|
||||||
$location.path('/');
|
inject(function($location, $rootScope) {
|
||||||
$rootScope.$digest();
|
$location.path('/');
|
||||||
expect(elt.text()).toBe('Home');
|
$rootScope.$digest();
|
||||||
}));
|
expect(elt.text()).toBe('Home');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should bind the component to the current router', inject(function($location) {
|
it('should bind the component to the current router', function() {
|
||||||
var router;
|
var router;
|
||||||
registerComponent('homeCmp', {
|
registerComponent('homeCmp', {
|
||||||
bindings: { $router: '=' },
|
bindings: { $router: '=' },
|
||||||
|
@ -63,41 +50,48 @@ describe('router', function () {
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
compile('<app></app>');
|
module('testMod');
|
||||||
|
compileApp();
|
||||||
|
|
||||||
$location.path('/');
|
inject(function($location, $rootScope) {
|
||||||
$rootScope.$digest();
|
$location.path('/');
|
||||||
var homeElement = elt.find('home-cmp');
|
$rootScope.$digest();
|
||||||
expect(homeElement.text()).toBe('Home');
|
var homeElement = elt.find('home-cmp');
|
||||||
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
|
expect(homeElement.text()).toBe('Home');
|
||||||
expect(router).toBeDefined();
|
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
|
||||||
}));
|
expect(router).toBeDefined();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
it('should work when an async route is provided route data', inject(function($location, $q) {
|
it('should work when an async route is provided route data', function() {
|
||||||
registerDirective('homeCmp', {
|
registerComponent('homeCmp', {
|
||||||
template: 'Home ({{homeCmp.isAdmin}})',
|
template: 'Home ({{$ctrl.isAdmin}})',
|
||||||
$routerOnActivate: function(next, prev) {
|
$routerOnActivate: function(next, prev) {
|
||||||
this.isAdmin = next.routeData.data.isAdmin;
|
this.isAdmin = next.routeData.data.isAdmin;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
registerDirective('app', {
|
registerComponent('app', {
|
||||||
template: '<div ng-outlet></div>',
|
template: '<div ng-outlet></div>',
|
||||||
$routeConfig: [
|
$routeConfig: [
|
||||||
{ path: '/', loader: function() { return $q.when('homeCmp'); }, data: { isAdmin: true } }
|
{ path: '/', loader: function($q) { return $q.when('homeCmp'); }, data: { isAdmin: true } }
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
compile('<app></app>');
|
module('testMod');
|
||||||
|
compileApp();
|
||||||
|
|
||||||
$location.path('/');
|
inject(function($location, $rootScope) {
|
||||||
$rootScope.$digest();
|
$location.path('/');
|
||||||
expect(elt.text()).toBe('Home (true)');
|
$rootScope.$digest();
|
||||||
}));
|
expect(elt.text()).toBe('Home (true)');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work with a templateUrl component', function() {
|
||||||
|
|
||||||
it('should work with a templateUrl component', inject(function($location, $httpBackend) {
|
|
||||||
var $routerOnActivate = jasmine.createSpy('$routerOnActivate');
|
var $routerOnActivate = jasmine.createSpy('$routerOnActivate');
|
||||||
$httpBackend.expectGET('homeCmp.html').respond('Home');
|
|
||||||
registerComponent('homeCmp', {
|
registerComponent('homeCmp', {
|
||||||
templateUrl: 'homeCmp.html',
|
templateUrl: 'homeCmp.html',
|
||||||
$routerOnActivate: $routerOnActivate
|
$routerOnActivate: $routerOnActivate
|
||||||
|
@ -110,17 +104,24 @@ describe('router', function () {
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
compile('<app></app>');
|
module('testMod');
|
||||||
|
|
||||||
$location.path('/');
|
inject(function($location, $rootScope, $httpBackend) {
|
||||||
$rootScope.$digest();
|
|
||||||
$httpBackend.flush();
|
|
||||||
var homeElement = elt.find('home-cmp');
|
|
||||||
expect(homeElement.text()).toBe('Home');
|
|
||||||
expect($routerOnActivate).toHaveBeenCalled();
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should provide the current instruction', inject(function($location, $q) {
|
$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() {
|
||||||
registerComponent('homeCmp', {
|
registerComponent('homeCmp', {
|
||||||
template: 'Home ({{homeCmp.isAdmin}})'
|
template: 'Home ({{homeCmp.isAdmin}})'
|
||||||
});
|
});
|
||||||
|
@ -131,15 +132,20 @@ describe('router', function () {
|
||||||
{ path: '/', component: 'homeCmp', name: 'Home' }
|
{ path: '/', component: 'homeCmp', name: 'Home' }
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
compile('<app></app>');
|
|
||||||
|
|
||||||
$location.path('/');
|
module('testMod');
|
||||||
$rootScope.$digest();
|
|
||||||
var instruction = $rootRouter.generate(['/Home']);
|
|
||||||
expect($rootRouter.currentInstruction).toEqual(instruction);
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should provide the root level router', inject(function($location, $q) {
|
inject(function($rootScope, $rootRouter, $location) {
|
||||||
|
compileApp();
|
||||||
|
|
||||||
|
$location.path('/');
|
||||||
|
$rootScope.$digest();
|
||||||
|
var instruction = $rootRouter.generate(['/Home']);
|
||||||
|
expect($rootRouter.currentInstruction).toEqual(instruction);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should provide the root level router', function() {
|
||||||
registerComponent('homeCmp', {
|
registerComponent('homeCmp', {
|
||||||
template: 'Home ({{homeCmp.isAdmin}})',
|
template: 'Home ({{homeCmp.isAdmin}})',
|
||||||
bindings: {
|
bindings: {
|
||||||
|
@ -153,25 +159,18 @@ describe('router', function () {
|
||||||
{ path: '/', component: 'homeCmp', name: 'Home' }
|
{ path: '/', component: 'homeCmp', name: 'Home' }
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
compile('<app></app>');
|
|
||||||
|
|
||||||
$location.path('/');
|
module('testMod');
|
||||||
$rootScope.$digest();
|
|
||||||
var homeElement = elt.find('home-cmp');
|
|
||||||
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
|
|
||||||
}));
|
|
||||||
|
|
||||||
function registerDirective(name, options) {
|
inject(function($rootScope, $rootRouter, $location) {
|
||||||
function factory() {
|
compileApp();
|
||||||
return {
|
|
||||||
template: options.template || '',
|
$location.path('/');
|
||||||
controllerAs: name,
|
$rootScope.$digest();
|
||||||
controller: getController(options)
|
var homeElement = elt.find('home-cmp');
|
||||||
};
|
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
|
||||||
}
|
});
|
||||||
applyStaticProperties(factory, options);
|
});
|
||||||
$compileProvider.directive(name, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
function registerComponent(name, options) {
|
function registerComponent(name, options) {
|
||||||
|
|
||||||
|
@ -183,12 +182,14 @@ describe('router', function () {
|
||||||
if (options.templateUrl) definition.templateUrl = options.templateUrl;
|
if (options.templateUrl) definition.templateUrl = options.templateUrl;
|
||||||
|
|
||||||
applyStaticProperties(definition, options);
|
applyStaticProperties(definition, options);
|
||||||
$compileProvider.component(name, definition);
|
angular.module('testMod').component(name, definition);
|
||||||
}
|
}
|
||||||
|
|
||||||
function compile(template) {
|
function compileApp() {
|
||||||
elt = $compile('<div>' + template + '</div>')($rootScope);
|
inject(function($compile, $rootScope) {
|
||||||
$rootScope.$digest();
|
elt = $compile('<div><app></app</div>')($rootScope);
|
||||||
|
$rootScope.$digest();
|
||||||
|
});
|
||||||
return elt;
|
return elt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue