-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.tests.js
87 lines (63 loc) · 3.21 KB
/
routes.tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
describe('Routes test', function() {
// Mock our module in our tests
beforeEach(module('pivotApp'));
// We want to store a copy of the three services we'll use in our tests
// so we can later reference these services in our tests.
var $httpBackend, $location, $route, $rootScope;
// We added _ in our dependencies to avoid conflicting with our variables.
// Angularjs will strip out the _ and inject the dependencies.
beforeEach(inject(function(_$httpBackend_,_$location_, _$route_, _$rootScope_){
$httpBackend = _$httpBackend_
$location = _$location_;
$route = _$route_;
$rootScope = _$rootScope_;
}));
// Our test code is set up. We can now start writing the tests.
// When a user navigates to the index page, they are shown the index page with the proper
// controller
it('should load the index page on successful load of /', function(){
$httpBackend.expectGET('static/partials/index.html').respond(200, 'index HTML');
expect($location.path()).toBe('');
$location.path('/');
// The router works with the digest lifecycle, wherein after the location is set,
// it takes a single digest loop cycle to process the route,
// transform the page content, and finish the routing.
// In order to trigger the location request, we’ll run a digest cycle (on the $rootScope)
// and check that the controller is as expected.
$rootScope.$digest();
expect($location.path()).toBe( '/' );
expect($route.current.controller).toBe('ToolsCtrl');
});
it('should redirect to the index path on non-existent route', function(){
$httpBackend.expectGET('static/partials/index.html').respond(200, 'index HTML');
expect($location.path()).toBe('');
$location.path('/a/non-existent/route');
$rootScope.$digest();
expect($location.path()).toBe( '/' );
expect($route.current.controller).toBe('ToolsCtrl');
});
it('should load the tools page on successful load of /tools', function(){
$httpBackend.expectGET('static/partials/toolslist.html').respond(200, 'tools HTML');
expect($location.path()).toBe('');
$location.path('/tools');
$rootScope.$digest();
expect($location.path()).toBe( '/tools' );
expect($route.current.controller).toBe('ToolsCtrl');
});
it('should load the articles page on successful load of /articles', function(){
$httpBackend.expectGET('static/partials/articleslist.html').respond(200, 'articles HTML');
expect($location.path()).toBe('');
$location.path('/articles');
$rootScope.$digest();
expect($location.path()).toBe( '/articles' );
expect($route.current.controller).toBe('ArticlesCtrl');
});
it('should load the customers page on successful load of /customers', function(){
$httpBackend.expectGET('static/partials/customerslist.html').respond(200, 'customers HTML');
expect($location.path()).toBe('');
$location.path('/customers');
$rootScope.$digest();
expect($location.path()).toBe( '/customers' );
expect($route.current.controller).toBe('CustomersCtrl');
});
});