Skip to content

Commit 83f0e7c

Browse files
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.
1 parent adef68b commit 83f0e7c

File tree

1 file changed

+87
-86
lines changed

1 file changed

+87
-86
lines changed

modules/angular1_router/test/integration/router_spec.js

Lines changed: 87 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,14 @@
22

33
describe('router', function () {
44

5-
var elt,
6-
$compile,
7-
$rootScope,
8-
$rootRouter,
9-
$compileProvider;
10-
5+
var elt, testMod;
116
beforeEach(function () {
12-
module('ng');
13-
module('ngComponentRouter');
14-
module(function($provide) {
15-
$provide.value('$routerRootComponent', 'app');
16-
});
17-
module(function (_$compileProvider_) {
18-
$compileProvider = _$compileProvider_;
19-
});
20-
21-
inject(function (_$compile_, _$rootScope_, _$rootRouter_) {
22-
$compile = _$compile_;
23-
$rootScope = _$rootScope_;
24-
$rootRouter = _$rootRouter_;
25-
});
7+
testMod = angular.module('testMod', ['ngComponentRouter'])
8+
.value('$routerRootComponent', 'app');
269
});
2710

28-
it('should work with a provided root component', inject(function($location) {
11+
it('should work with a provided root component', function() {
12+
2913
registerComponent('homeCmp', {
3014
template: 'Home'
3115
});
@@ -37,14 +21,17 @@ describe('router', function () {
3721
]
3822
});
3923

40-
compile('<app></app>');
24+
module('testMod');
25+
compileApp();
4126

42-
$location.path('/');
43-
$rootScope.$digest();
44-
expect(elt.text()).toBe('Home');
45-
}));
27+
inject(function($location, $rootScope) {
28+
$location.path('/');
29+
$rootScope.$digest();
30+
expect(elt.text()).toBe('Home');
31+
});
32+
});
4633

47-
it('should bind the component to the current router', inject(function($location) {
34+
it('should bind the component to the current router', function() {
4835
var router;
4936
registerComponent('homeCmp', {
5037
bindings: { $router: '=' },
@@ -63,41 +50,48 @@ describe('router', function () {
6350
]
6451
});
6552

66-
compile('<app></app>');
53+
module('testMod');
54+
compileApp();
6755

68-
$location.path('/');
69-
$rootScope.$digest();
70-
var homeElement = elt.find('home-cmp');
71-
expect(homeElement.text()).toBe('Home');
72-
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
73-
expect(router).toBeDefined();
74-
}));
56+
inject(function($location, $rootScope) {
57+
$location.path('/');
58+
$rootScope.$digest();
59+
var homeElement = elt.find('home-cmp');
60+
expect(homeElement.text()).toBe('Home');
61+
expect(homeElement.isolateScope().$ctrl.$router).toBeDefined();
62+
expect(router).toBeDefined();
63+
})
64+
});
7565

76-
it('should work when an async route is provided route data', inject(function($location, $q) {
77-
registerDirective('homeCmp', {
78-
template: 'Home ({{homeCmp.isAdmin}})',
66+
it('should work when an async route is provided route data', function() {
67+
registerComponent('homeCmp', {
68+
template: 'Home ({{$ctrl.isAdmin}})',
7969
$routerOnActivate: function(next, prev) {
8070
this.isAdmin = next.routeData.data.isAdmin;
8171
}
8272
});
8373

84-
registerDirective('app', {
74+
registerComponent('app', {
8575
template: '<div ng-outlet></div>',
8676
$routeConfig: [
87-
{ path: '/', loader: function() { return $q.when('homeCmp'); }, data: { isAdmin: true } }
77+
{ path: '/', loader: function($q) { return $q.when('homeCmp'); }, data: { isAdmin: true } }
8878
]
8979
});
9080

91-
compile('<app></app>');
81+
module('testMod');
82+
compileApp();
9283

93-
$location.path('/');
94-
$rootScope.$digest();
95-
expect(elt.text()).toBe('Home (true)');
96-
}));
84+
inject(function($location, $rootScope) {
85+
$location.path('/');
86+
$rootScope.$digest();
87+
expect(elt.text()).toBe('Home (true)');
88+
});
89+
});
90+
91+
it('should work with a templateUrl component', function() {
9792

98-
it('should work with a templateUrl component', inject(function($location, $httpBackend) {
9993
var $routerOnActivate = jasmine.createSpy('$routerOnActivate');
100-
$httpBackend.expectGET('homeCmp.html').respond('Home');
94+
10195
registerComponent('homeCmp', {
10296
templateUrl: 'homeCmp.html',
10397
$routerOnActivate: $routerOnActivate
@@ -110,17 +104,24 @@ describe('router', function () {
110104
]
111105
});
112106

113-
compile('<app></app>');
107+
module('testMod');
114108

115-
$location.path('/');
116-
$rootScope.$digest();
117-
$httpBackend.flush();
118-
var homeElement = elt.find('home-cmp');
119-
expect(homeElement.text()).toBe('Home');
120-
expect($routerOnActivate).toHaveBeenCalled();
121-
}));
109+
inject(function($location, $rootScope, $httpBackend) {
122110

123-
it('should provide the current instruction', inject(function($location, $q) {
111+
$httpBackend.expectGET('homeCmp.html').respond('Home');
112+
113+
compileApp();
114+
115+
$location.path('/');
116+
$rootScope.$digest();
117+
$httpBackend.flush();
118+
var homeElement = elt.find('home-cmp');
119+
expect(homeElement.text()).toBe('Home');
120+
expect($routerOnActivate).toHaveBeenCalled();
121+
})
122+
});
123+
124+
it('should provide the current instruction', function() {
124125
registerComponent('homeCmp', {
125126
template: 'Home ({{homeCmp.isAdmin}})'
126127
});
@@ -131,15 +132,20 @@ describe('router', function () {
131132
{ path: '/', component: 'homeCmp', name: 'Home' }
132133
]
133134
});
134-
compile('<app></app>');
135135

136-
$location.path('/');
137-
$rootScope.$digest();
138-
var instruction = $rootRouter.generate(['/Home']);
139-
expect($rootRouter.currentInstruction).toEqual(instruction);
140-
}));
136+
module('testMod');
137+
138+
inject(function($rootScope, $rootRouter, $location) {
139+
compileApp();
140+
141+
$location.path('/');
142+
$rootScope.$digest();
143+
var instruction = $rootRouter.generate(['/Home']);
144+
expect($rootRouter.currentInstruction).toEqual(instruction);
145+
});
146+
});
141147

142-
it('should provide the root level router', inject(function($location, $q) {
148+
it('should provide the root level router', function() {
143149
registerComponent('homeCmp', {
144150
template: 'Home ({{homeCmp.isAdmin}})',
145151
bindings: {
@@ -153,25 +159,18 @@ describe('router', function () {
153159
{ path: '/', component: 'homeCmp', name: 'Home' }
154160
]
155161
});
156-
compile('<app></app>');
157-
158-
$location.path('/');
159-
$rootScope.$digest();
160-
var homeElement = elt.find('home-cmp');
161-
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
162-
}));
163-
164-
function registerDirective(name, options) {
165-
function factory() {
166-
return {
167-
template: options.template || '',
168-
controllerAs: name,
169-
controller: getController(options)
170-
};
171-
}
172-
applyStaticProperties(factory, options);
173-
$compileProvider.directive(name, factory);
174-
}
162+
163+
module('testMod');
164+
165+
inject(function($rootScope, $rootRouter, $location) {
166+
compileApp();
167+
168+
$location.path('/');
169+
$rootScope.$digest();
170+
var homeElement = elt.find('home-cmp');
171+
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
172+
});
173+
});
175174

176175
function registerComponent(name, options) {
177176

@@ -183,12 +182,14 @@ describe('router', function () {
183182
if (options.templateUrl) definition.templateUrl = options.templateUrl;
184183

185184
applyStaticProperties(definition, options);
186-
$compileProvider.component(name, definition);
185+
angular.module('testMod').component(name, definition);
187186
}
188187

189-
function compile(template) {
190-
elt = $compile('<div>' + template + '</div>')($rootScope);
191-
$rootScope.$digest();
188+
function compileApp() {
189+
inject(function($compile, $rootScope) {
190+
elt = $compile('<div><app></app</div>')($rootScope);
191+
$rootScope.$digest();
192+
});
192193
return elt;
193194
}
194195

0 commit comments

Comments
 (0)