vsn 0.1.129 → 0.1.130

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,271 +0,0 @@
1
- import {IDeferred, SimplePromise} from "../src/SimplePromise";
2
-
3
-
4
- class Test {
5
- constructor(
6
- public value: string = 'test'
7
- ) {}
8
- }
9
- class Testing {
10
- constructor(
11
- public test: Test
12
- ) {}
13
- }
14
-
15
-
16
- describe('Promise', () => {
17
- let dummy: any = null,
18
- dummy2: any = null;
19
-
20
- beforeEach(() => {
21
- dummy = jasmine.createSpyObj('dummy', ['callback']);
22
- dummy2 = jasmine.createSpyObj('dummy2', ['callback']);
23
- });
24
-
25
- it("defer should work properly with resolve", () => {
26
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
27
- const t: Test = new Test();
28
- expect(dummy.callback).not.toHaveBeenCalled();
29
- d.promise.then(dummy.callback.bind(dummy));
30
- d.resolve(t);
31
- expect(dummy.callback).toHaveBeenCalled();
32
- expect(d.promise['result']).toBe(t);
33
- });
34
-
35
- it("defer should work properly with reject", () => {
36
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
37
- const t: Test = new Test();
38
- expect(dummy.callback).not.toHaveBeenCalled();
39
- expect(dummy2.callback).not.toHaveBeenCalled();
40
- d.promise.then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
41
- d.reject("failed");
42
- expect(dummy.callback).not.toHaveBeenCalled();
43
- expect(dummy2.callback).toHaveBeenCalled();
44
- expect(d.promise['result']).toBe("failed");
45
- });
46
-
47
- it("defer should work properly with finally", () => {
48
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
49
- expect(dummy.callback).not.toHaveBeenCalled();
50
- expect(dummy2.callback).not.toHaveBeenCalled();
51
- d.promise.then((result: Test) => {
52
- expect(false).toBe(true); // should never be called
53
- return result;
54
- }, (reason: string) => {
55
- expect(dummy2.callback).not.toHaveBeenCalled();
56
- return reason;
57
- }).finally(dummy2.callback.bind(dummy2));
58
-
59
- d.reject("failed");
60
- expect(dummy2.callback).toHaveBeenCalled();
61
- expect(d.promise['result']).toBe("failed");
62
- });
63
-
64
- it("test race with reject", () => {
65
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
66
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
67
- const t: Test = new Test();
68
- expect(dummy.callback).not.toHaveBeenCalled();
69
- expect(dummy2.callback).not.toHaveBeenCalled();
70
- SimplePromise.race([
71
- d.promise,
72
- d2.promise
73
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
74
-
75
- d.reject("failed");
76
- d2.resolve(t);
77
- expect(dummy.callback).not.toHaveBeenCalled();
78
- expect(dummy2.callback).toHaveBeenCalled();
79
- expect(d.promise['result']).toBe("failed");
80
- });
81
-
82
- it("test race with resolve", () => {
83
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
84
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
85
- const t: Test = new Test();
86
- expect(dummy.callback).not.toHaveBeenCalled();
87
- expect(dummy2.callback).not.toHaveBeenCalled();
88
- SimplePromise.race([
89
- d.promise,
90
- d2.promise
91
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
92
-
93
- d.resolve(t);
94
- d.reject("failed");
95
- expect(dummy.callback).toHaveBeenCalled();
96
- expect(dummy2.callback).not.toHaveBeenCalled();
97
- expect(d.promise['result']).toBe(t);
98
- });
99
-
100
- it("test all with resolve", () => {
101
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
102
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
103
- const t: Test = new Test();
104
- expect(dummy.callback).not.toHaveBeenCalled();
105
- expect(dummy2.callback).not.toHaveBeenCalled();
106
- SimplePromise.all([
107
- d.promise,
108
- d2.promise
109
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
110
-
111
- d.resolve(t);
112
- expect(dummy.callback).not.toHaveBeenCalled();
113
- expect(dummy2.callback).not.toHaveBeenCalled();
114
- d2.resolve(t);
115
- expect(dummy.callback).toHaveBeenCalled();
116
- expect(dummy2.callback).not.toHaveBeenCalled();
117
- expect(d.promise['result']).toBe(t);
118
- });
119
-
120
- it("test all with reject", () => {
121
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
122
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
123
- const t: Test = new Test();
124
- expect(dummy.callback).not.toHaveBeenCalled();
125
- expect(dummy2.callback).not.toHaveBeenCalled();
126
- SimplePromise.all([
127
- d.promise,
128
- d2.promise
129
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
130
-
131
- d.resolve(t);
132
- expect(dummy.callback).not.toHaveBeenCalled();
133
- expect(dummy2.callback).not.toHaveBeenCalled();
134
- d2.reject("failed");
135
- expect(dummy.callback).not.toHaveBeenCalled();
136
- expect(dummy2.callback).toHaveBeenCalled();
137
- expect(d2.promise['result']).toBe("failed");
138
- });
139
-
140
- it("resolve static function should work properly", () => {
141
- const t: Test = new Test();
142
- expect(dummy.callback).not.toHaveBeenCalled();
143
- expect(dummy2.callback).not.toHaveBeenCalled();
144
- SimplePromise.resolve(t).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
145
- expect(dummy.callback).toHaveBeenCalled();
146
- expect(dummy2.callback).not.toHaveBeenCalled();
147
- });
148
-
149
- it("reject static function should work properly", () => {
150
- const t: Test = new Test();
151
- expect(dummy.callback).not.toHaveBeenCalled();
152
- expect(dummy2.callback).not.toHaveBeenCalled();
153
- SimplePromise.reject("failed").then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
154
- expect(dummy.callback).not.toHaveBeenCalled();
155
- expect(dummy2.callback).toHaveBeenCalled();
156
- });
157
-
158
- it("should be rejected if error is thrown", () => {
159
- const t: Test = new Test();
160
- expect(dummy.callback).not.toHaveBeenCalled();
161
- expect(dummy2.callback).not.toHaveBeenCalled();
162
- new SimplePromise<Test>((resolve, reject) => {
163
- throw Error('failed');
164
- }).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
165
- expect(dummy.callback).not.toHaveBeenCalled();
166
- expect(dummy2.callback).toHaveBeenCalled();
167
- });
168
-
169
- it("should process multiple then statements in the correct order", () => {
170
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
171
- const t: Test = new Test();
172
- d.promise.then<Test>((result: Test): Test => {
173
- expect(result.value).toBe('test');
174
- result.value = 'then 1';
175
- return result;
176
- });
177
- d.promise.then<Test>((result: Test): Test => {
178
- expect(result.value).toBe('then 1');
179
- result.value = 'then 2';
180
- return result;
181
- });
182
- d.promise.then<Test>((result: Test): Test => {
183
- expect(result.value).toBe('then 2');
184
- result.value = 'then 3';
185
- return result;
186
- });
187
- d.promise.then<Test>((result: Test): Test => {
188
- expect(result.value).toBe('then 3');
189
- result.value = 'then 4';
190
- return result;
191
- });
192
-
193
- d.resolve(t);
194
- });
195
-
196
- it("should process chained then statements in the correct order", () => {
197
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
198
- const t: Test = new Test();
199
- d.promise.then<Testing>((result: Test) => {
200
- expect(d.promise['result']).toBe(result);
201
- return new Testing(result);
202
- }).then((result: Testing | undefined): Testing => {
203
- expect((result as any as Testing).test).toBe(t);
204
- return result as any as Testing;
205
- });
206
- d.resolve(t);
207
- });
208
-
209
- it("should process callback on previously resolved promise", (done) => {
210
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
211
- const t: Test = new Test();
212
- d.resolve(t);
213
-
214
- d.promise.then<Testing>((result: Test) => {
215
- expect(d.promise['result']).toBe(result);
216
- done();
217
- return new Testing(result);
218
- });
219
- });
220
-
221
- it("should process callback on previously rejected promise", (done) => {
222
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
223
- d.reject('Failure.');
224
-
225
- d.promise.then<Testing>((result: Test) => {
226
- expect(false).toBe(true);
227
- return new Testing(result);
228
- }, (reason?: string): string => {
229
- expect(reason).toBe('Failure.');
230
- done();
231
-
232
- return reason as string;
233
- });
234
- });
235
-
236
- it("should process Promise.all callbacks with previously resolved promises", (done) => {
237
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
238
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
239
- const t: Test = new Test();
240
- const t2: Test = new Test();
241
- d2.resolve(t2);
242
-
243
- SimplePromise.all<Test>([d.promise, d2.promise]).then((results?: Test[]) => {
244
- if (results === undefined) return;
245
- expect(d.promise['result']).toBe(results[0]);
246
- expect(d2.promise['result']).toBe(results[1]);
247
- done();
248
- });
249
-
250
- d.resolve(t);
251
- });
252
-
253
- it("should process Promise.all callbacks with previously rejected promises", (done) => {
254
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
255
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
256
- const t: Test = new Test();
257
- const t2: Test = new Test();
258
- d.reject('Failure.');
259
-
260
- SimplePromise.all<Test>([d.promise, d2.promise]).then((results?: Test[]) => {
261
- expect(false).toBe(true);
262
- }, (reason?: string): string => {
263
- expect(reason).toBe('Failure.');
264
- done();
265
-
266
- return reason as string;
267
- });
268
-
269
- d2.resolve(t2);
270
- });
271
- });