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 () {
var elt,
$compile,
$rootScope,
$rootRouter,
$compileProvider;
var elt, testMod;
beforeEach(function () {
module('ng');
module('ngComponentRouter');
module(function($provide) {
$provide.value('$routerRootComponent', 'app');
});
module(function (_$compileProvider_) {
$compileProvider = _$compileProvider_;
});
inject(function (_$compile_, _$rootScope_, _$rootRouter_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$rootRouter = _$rootRouter_;
});
testMod = angular.module('testMod', ['ngComponentRouter'])
.value('$routerRootComponent', 'app');
});
it('should work with a provided root component', inject(function($location) {
it('should work with a provided root component', function() {
registerComponent('homeCmp', {
template: 'Home'
});
@ -37,14 +21,17 @@ describe('router', function () {
]
});
compile('<app></app>');
module('testMod');
compileApp();
$location.path('/');
$rootScope.$digest();
expect(elt.text()).toBe('Home');
}));
inject(function($location, $rootScope) {
$location.path('/');
$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;
registerComponent('homeCmp', {
bindings: { $router: '=' },
@ -63,41 +50,48 @@ describe('router', function () {
]
});
compile('<app></app>');
module('testMod');
compileApp();
$location.path('/');
$rootScope.$digest();
var homeElement = elt.find('home-cmp');
expect(homeElement.text()).toBe('Home');
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
expect(router).toBeDefined();
}));
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();
})
});
it('should work when an async route is provided route data', inject(function($location, $q) {
registerDirective('homeCmp', {
template: 'Home ({{homeCmp.isAdmin}})',
it('should work when an async route is provided route data', function() {
registerComponent('homeCmp', {
template: 'Home ({{$ctrl.isAdmin}})',
$routerOnActivate: function(next, prev) {
this.isAdmin = next.routeData.data.isAdmin;
}
});
registerDirective('app', {
registerComponent('app', {
template: '<div ng-outlet></div>',
$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('/');
$rootScope.$digest();
expect(elt.text()).toBe('Home (true)');
}));
inject(function($location, $rootScope) {
$location.path('/');
$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');
$httpBackend.expectGET('homeCmp.html').respond('Home');
registerComponent('homeCmp', {
templateUrl: 'homeCmp.html',
$routerOnActivate: $routerOnActivate
@ -110,17 +104,24 @@ describe('router', function () {
]
});
compile('<app></app>');
module('testMod');
$location.path('/');
$rootScope.$digest();
$httpBackend.flush();
var homeElement = elt.find('home-cmp');
expect(homeElement.text()).toBe('Home');
expect($routerOnActivate).toHaveBeenCalled();
}));
inject(function($location, $rootScope, $httpBackend) {
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', {
template: 'Home ({{homeCmp.isAdmin}})'
});
@ -131,15 +132,20 @@ describe('router', function () {
{ path: '/', component: 'homeCmp', name: 'Home' }
]
});
compile('<app></app>');
$location.path('/');
$rootScope.$digest();
var instruction = $rootRouter.generate(['/Home']);
expect($rootRouter.currentInstruction).toEqual(instruction);
}));
module('testMod');
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', {
template: 'Home ({{homeCmp.isAdmin}})',
bindings: {
@ -153,25 +159,18 @@ describe('router', function () {
{ 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);
}));
module('testMod');
function registerDirective(name, options) {
function factory() {
return {
template: options.template || '',
controllerAs: name,
controller: getController(options)
};
}
applyStaticProperties(factory, options);
$compileProvider.directive(name, factory);
}
inject(function($rootScope, $rootRouter, $location) {
compileApp();
$location.path('/');
$rootScope.$digest();
var homeElement = elt.find('home-cmp');
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
});
});
function registerComponent(name, options) {
@ -183,12 +182,14 @@ describe('router', function () {
if (options.templateUrl) definition.templateUrl = options.templateUrl;
applyStaticProperties(definition, options);
$compileProvider.component(name, definition);
angular.module('testMod').component(name, definition);
}
function compile(template) {
elt = $compile('<div>' + template + '</div>')($rootScope);
$rootScope.$digest();
function compileApp() {
inject(function($compile, $rootScope) {
elt = $compile('<div><app></app</div>')($rootScope);
$rootScope.$digest();
});
return elt;
}