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:
Peter Bacon Darwin 2016-03-07 22:09:37 +00:00 committed by Pete Bacon Darwin
parent adef68b4d6
commit 83f0e7c975
1 changed files with 85 additions and 84 deletions

View File

@ -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_) { it('should work with a provided root component', function() {
$compile = _$compile_;
$rootScope = _$rootScope_;
$rootRouter = _$rootRouter_;
});
});
it('should work with a provided root component', inject(function($location) {
registerComponent('homeCmp', { registerComponent('homeCmp', {
template: 'Home' template: 'Home'
}); });
@ -37,14 +21,17 @@ describe('router', function () {
] ]
}); });
compile('<app></app>'); module('testMod');
compileApp();
inject(function($location, $rootScope) {
$location.path('/'); $location.path('/');
$rootScope.$digest(); $rootScope.$digest();
expect(elt.text()).toBe('Home'); 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();
inject(function($location, $rootScope) {
$location.path('/'); $location.path('/');
$rootScope.$digest(); $rootScope.$digest();
var homeElement = elt.find('home-cmp'); var homeElement = elt.find('home-cmp');
expect(homeElement.text()).toBe('Home'); expect(homeElement.text()).toBe('Home');
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined(); expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
expect(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();
inject(function($location, $rootScope) {
$location.path('/'); $location.path('/');
$rootScope.$digest(); $rootScope.$digest();
expect(elt.text()).toBe('Home (true)'); 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,7 +104,13 @@ describe('router', function () {
] ]
}); });
compile('<app></app>'); module('testMod');
inject(function($location, $rootScope, $httpBackend) {
$httpBackend.expectGET('homeCmp.html').respond('Home');
compileApp();
$location.path('/'); $location.path('/');
$rootScope.$digest(); $rootScope.$digest();
@ -118,9 +118,10 @@ describe('router', function () {
var homeElement = elt.find('home-cmp'); var homeElement = elt.find('home-cmp');
expect(homeElement.text()).toBe('Home'); expect(homeElement.text()).toBe('Home');
expect($routerOnActivate).toHaveBeenCalled(); expect($routerOnActivate).toHaveBeenCalled();
})); })
});
it('should provide the current instruction', inject(function($location, $q) { 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>');
module('testMod');
inject(function($rootScope, $rootRouter, $location) {
compileApp();
$location.path('/'); $location.path('/');
$rootScope.$digest(); $rootScope.$digest();
var instruction = $rootRouter.generate(['/Home']); var instruction = $rootRouter.generate(['/Home']);
expect($rootRouter.currentInstruction).toEqual(instruction); expect($rootRouter.currentInstruction).toEqual(instruction);
})); });
});
it('should provide the root level router', inject(function($location, $q) { 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>');
module('testMod');
inject(function($rootScope, $rootRouter, $location) {
compileApp();
$location.path('/'); $location.path('/');
$rootScope.$digest(); $rootScope.$digest();
var homeElement = elt.find('home-cmp'); var homeElement = elt.find('home-cmp');
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter); expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
})); });
});
function registerDirective(name, options) {
function factory() {
return {
template: options.template || '',
controllerAs: name,
controller: getController(options)
};
}
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) {
elt = $compile('<div><app></app</div>')($rootScope);
$rootScope.$digest(); $rootScope.$digest();
});
return elt; return elt;
} }