forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_spec.js
More file actions
170 lines (141 loc) · 5.16 KB
/
async_spec.js
File metadata and controls
170 lines (141 loc) · 5.16 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'angular2/test_lib';
import {Injector, Inject, InjectPromise, bind, Key} from 'angular2/di';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
class UserList {
}
function fetchUsers() {
return PromiseWrapper.resolve(new UserList());
}
class SynchronousUserList {
}
class UserController {
list:UserList;
constructor(list:UserList) {
this.list = list;
}
}
class AsyncUserController {
userList;
constructor(@InjectPromise(UserList) userList) {
this.userList = userList;
}
}
export function main() {
describe("async injection", function () {
describe("asyncGet", function () {
it('should return a promise', function () {
var injector = new Injector([
bind(UserList).toAsyncFactory(fetchUsers)
]);
var p = injector.asyncGet(UserList);
expect(p).toBePromise();
});
it('should return a promise when the binding is sync', function () {
var injector = new Injector([
SynchronousUserList
]);
var p = injector.asyncGet(SynchronousUserList);
expect(p).toBePromise();
});
it("should return a promise when the binding is sync (from cache)", function () {
var injector = new Injector([
UserList
]);
expect(injector.get(UserList)).toBeAnInstanceOf(UserList);
expect(injector.asyncGet(UserList)).toBePromise();
});
it('should return the injector', function (done) {
var injector = new Injector([]);
var p = injector.asyncGet(Injector);
p.then(function (injector) {
expect(injector).toBe(injector);
done();
});
});
it('should return a promise when instantiating a sync binding ' +
'with an async dependency', function (done) {
var injector = new Injector([
bind(UserList).toAsyncFactory(fetchUsers),
UserController
]);
injector.asyncGet(UserController).then(function (userController) {
expect(userController).toBeAnInstanceOf(UserController);
expect(userController.list).toBeAnInstanceOf(UserList);
done();
});
});
it("should create only one instance (async + async)", function (done) {
var injector = new Injector([
bind(UserList).toAsyncFactory(fetchUsers)
]);
var ul1 = injector.asyncGet(UserList);
var ul2 = injector.asyncGet(UserList);
PromiseWrapper.all([ul1, ul2]).then(function (uls) {
expect(uls[0]).toBe(uls[1]);
done();
});
});
it("should create only one instance (sync + async)", function (done) {
var injector = new Injector([
UserList
]);
var promise = injector.asyncGet(UserList);
var ul = injector.get(UserList);
expect(promise).toBePromise();
expect(ul).toBeAnInstanceOf(UserList);
promise.then(function (ful) {
expect(ful).toBe(ul);
done();
});
});
it('should show the full path when error happens in a constructor', function (done) {
var injector = new Injector([
UserController,
bind(UserList).toAsyncFactory(function () {
throw "Broken UserList";
})
]);
var promise = injector.asyncGet(UserController);
PromiseWrapper.then(promise, null, function (e) {
expect(e.message).toContain("Error during instantiation of UserList! (UserController -> UserList)");
done();
});
});
});
describe("get", function () {
it('should throw when instantiating an async binding', function () {
var injector = new Injector([
bind(UserList).toAsyncFactory(fetchUsers)
]);
expect(() => injector.get(UserList))
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise!');
});
it('should throw when instantiating a sync binding with an dependency', function () {
var injector = new Injector([
bind(UserList).toAsyncFactory(fetchUsers),
UserController
]);
expect(() => injector.get(UserController))
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise! (UserController -> UserList)');
});
it('should resolve synchronously when an async dependency requested as a promise', function () {
var injector = new Injector([
bind(UserList).toAsyncFactory(fetchUsers),
AsyncUserController
]);
var controller = injector.get(AsyncUserController);
expect(controller).toBeAnInstanceOf(AsyncUserController);
expect(controller.userList).toBePromise();
});
it('should wrap sync dependencies into promises if required', function () {
var injector = new Injector([
bind(UserList).toFactory(() => new UserList()),
AsyncUserController
]);
var controller = injector.get(AsyncUserController);
expect(controller).toBeAnInstanceOf(AsyncUserController);
expect(controller.userList).toBePromise();
});
});
});
}