-
Notifications
You must be signed in to change notification settings - Fork 9
/
async.spec.js
133 lines (94 loc) · 3.58 KB
/
async.spec.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
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
import {ProvidePromise, InjectPromise, Inject, TransientScope} from '../src/annotations';
import {Injector} from '../src/injector';
class UserList {}
// An async provider.
@ProvidePromise(UserList)
function fetchUsers() {
return Promise.resolve(new UserList);
}
class SynchronousUserList {}
class UserController {
constructor(@Inject(UserList) list) {
this.list = list;
}
}
class SmartUserController {
constructor(@InjectPromise(UserList) promise) {
this.promise = promise;
}
}
describe('async', function() {
it('should return a promise', function() {
var injector = new Injector([fetchUsers]);
var p = injector.getPromise(UserList)
expect(p).toBePromiseLike();
});
it('should throw when instantiating promise provider synchronously', function() {
var injector = new Injector([fetchUsers]);
expect(() => injector.get(UserList))
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise!');
});
it('should return promise even if the provider is sync', function() {
var injector = new Injector();
var p = injector.getPromise(SynchronousUserList);
expect(p).toBePromiseLike();
});
// regression
it('should return promise even if the provider is sync, from cache', function() {
var injector = new Injector();
var p1 = injector.getPromise(SynchronousUserList);
var p2 = injector.getPromise(SynchronousUserList);
expect(p2).toBePromiseLike();
});
it('should return promise when a dependency is async', function(done) {
var injector = new Injector([fetchUsers]);
injector.getPromise(UserController).then(function(userController) {
expect(userController).toBeInstanceOf(UserController);
expect(userController.list).toBeInstanceOf(UserList);
done();
});
});
// regression
it('should return a promise even from parent injector', function() {
var injector = new Injector([SynchronousUserList]);
var childInjector = injector.createChild([])
expect(childInjector.getPromise(SynchronousUserList)).toBePromiseLike();
});
it('should throw when a dependency is async', function() {
var injector = new Injector([fetchUsers]);
expect(() => injector.get(UserController))
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise! (UserController -> UserList)');
});
it('should resolve synchronously when async dependency requested as a promise', function() {
var injector = new Injector([fetchUsers]);
var controller = injector.get(SmartUserController);
expect(controller).toBeInstanceOf(SmartUserController);
expect(controller.promise).toBePromiseLike();
});
// regression
it('should not cache TransientScope', function(done) {
@TransientScope
@Inject(UserList)
class NeverCachedUserController {
constructor(list) {
this.list = list;
}
}
var injector = new Injector([fetchUsers]);
injector.getPromise(NeverCachedUserController).then(function(controller1) {
injector.getPromise(NeverCachedUserController).then(function(controller2) {
expect(controller1).not.toBe(controller2);
done();
});
});
});
it('should allow async dependency in a parent constructor', function(done) {
class ChildUserController extends UserController {}
var injector = new Injector([fetchUsers]);
injector.getPromise(ChildUserController).then(function(childUserController) {
expect(childUserController).toBeInstanceOf(ChildUserController);
expect(childUserController.list).toBeInstanceOf(UserList);
done();
});
});
});