-
Notifications
You must be signed in to change notification settings - Fork 0
/
appStateSpec.js
73 lines (57 loc) · 2.11 KB
/
appStateSpec.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
// Sample StateProvider test
// http://stackoverflow.com/questions/20433485/angular-ui-router-unit-testing-states-to-urls#answer-21078955
// Load the app
// Definition included in test for completeness
var app = angular.module('StateApp', ['ui.router']);
app.config(function($stateProvider) {
$stateProvider.state('myState', {
url: '/state/:id',
templateUrl: 'template.html',
controller: 'MyCtrl',
resolve: {
data: ['myService', function(service) {
return service.findAll();
}]
}
});
$stateProvider.state('myState.dolphins', {
url: '/dolphins',
template: '<h1>Dolphins!</h1>'
});
});
app.controller('MyCtrl', function() {});
describe('StateApp/myState', function() {
var $rootScope, $state, $injector, myServiceMock, state = 'myState';
beforeEach(function() {
module('StateApp', function($provide) {
$provide.value('myService', myServiceMock = {});
});
inject(function(_$rootScope_, _$state_, _$injector_, $templateCache) {
$rootScope = _$rootScope_;
$state = _$state_;
$injector = _$injector_;
// We need add the template entry into the templateCache if we ever
// specify a templateUrl
$templateCache.put('template.html', '');
});
});
it('should respond to URL', function() {
expect($state.href(state, { id: 1 })).toEqual('#/state/1');
});
it('should resolve data', function() {
myServiceMock.findAll = jasmine.createSpy('findAll').andReturn('findAll');
$state.go(state, { id: 1 });
$rootScope.$digest();
expect($state.current.name).toBe(state);
// Call invoke to inject dependencies and run function
expect($injector.invoke($state.current.resolve.data)).toBe('findAll');
});
it('should resolve nested data', function() {
myServiceMock.findAll = jasmine.createSpy('findAll').andReturn('findAll');
$state.go(state+'.dolphins', { id: 1 });
$rootScope.$digest();
expect($state.current.name).toBe(state+'.dolphins');
// $state.$current.locals.globals is the resolved values with inherited values
expect($state.$current.locals.globals.data).toBe('findAll');
});
});