-
Notifications
You must be signed in to change notification settings - Fork 30.2k
/
Copy pathwinjs.polyfill.promise.test.ts
185 lines (158 loc) · 5.51 KB
/
winjs.polyfill.promise.test.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import { Promise as WinJSPromise } from 'vs/base/common/winjs.base';
import { PolyfillPromise } from 'vs/base/common/winjs.polyfill.promise';
suite('Polyfill Promise', function () {
test('sync-resolve, NativePromise', function () {
// native promise behaviour
const actual: string[] = [];
const promise = new Promise(resolve => {
actual.push('inCtor');
resolve(null);
}).then(() => actual.push('inThen'));
actual.push('afterCtor');
return promise.then(() => {
assert.deepEqual(actual, ['inCtor', 'afterCtor', 'inThen']);
});
});
test('sync-resolve, WinJSPromise', function () {
// winjs promise behaviour
const actual: string[] = [];
const promise = new WinJSPromise(resolve => {
actual.push('inCtor');
resolve(null);
}).then(() => actual.push('inThen'));
actual.push('afterCtor');
return promise.then(() => {
assert.deepEqual(actual, ['inCtor', 'inThen', 'afterCtor']);
});
});
test('sync-resolve, PolyfillPromise', function () {
// winjs promise behaviour
const actual: string[] = [];
const promise = new PolyfillPromise(resolve => {
actual.push('inCtor');
resolve(null);
}).then(() => actual.push('inThen'));
actual.push('afterCtor');
return promise.then(() => {
assert.deepEqual(actual, ['inCtor', 'afterCtor', 'inThen']);
});
});
test('sync-then, NativePromise', function () {
const actual: string[] = [];
const promise = Promise.resolve(123).then(() => actual.push('inThen'));
actual.push('afterThen');
return promise.then(() => {
assert.deepEqual(actual, ['afterThen', 'inThen']);
});
});
test('sync-then, WinJSPromise', function () {
const actual: string[] = [];
const promise = WinJSPromise.as(123).then(() => actual.push('inThen'));
actual.push('afterThen');
return promise.then(() => {
assert.deepEqual(actual, ['inThen', 'afterThen']);
});
});
test('sync-then, PolyfillPromise', function () {
const actual: string[] = [];
const promise = PolyfillPromise.resolve(123).then(() => actual.push('inThen'));
actual.push('afterThen');
return promise.then(() => {
assert.deepEqual(actual, ['afterThen', 'inThen']);
});
});
test('PolyfillPromise, executor has two params', function () {
return new PolyfillPromise(function () {
assert.equal(arguments.length, 2);
assert.equal(typeof arguments[0], 'function');
assert.equal(typeof arguments[1], 'function');
arguments[0]();
});
});
test('Promises polyfill does not support chaining then and catch #57722', function () {
return PolyfillPromise.resolve(1).then(function (x) { return x + 1; }).then(function (x) {
assert.equal(x, 2);
});
});
// run the same tests for the native and polyfill promise
(<any[]>[Promise, PolyfillPromise]).forEach(PromiseCtor => {
test(PromiseCtor.name + ', resolved value', function () {
return new PromiseCtor((resolve: Function) => resolve(1)).then((value: number) => assert.equal(value, 1));
});
test(PromiseCtor.name + ', rejected value', function () {
return new PromiseCtor((_: Function, reject: Function) => reject(1)).then(null, (value: number) => assert.equal(value, 1));
});
test(PromiseCtor.name + ', catch', function () {
return new PromiseCtor((_: Function, reject: Function) => reject(1)).catch((value: number) => assert.equal(value, 1));
});
test(PromiseCtor.name + ', static-resolve', function () {
return PromiseCtor.resolve(42).then((value: number) => assert.equal(value, 42));
});
test(PromiseCtor.name + ', static-reject', function () {
return PromiseCtor.reject(42).then(null, (value: number) => assert.equal(value, 42));
});
test(PromiseCtor.name + ', static-all, 1', function () {
return PromiseCtor.all([
PromiseCtor.resolve(1),
PromiseCtor.resolve(2)
]).then((values: number[]) => {
assert.deepEqual(values, [1, 2]);
});
});
test(PromiseCtor.name + ', static-all, 2', function () {
return PromiseCtor.all([
PromiseCtor.resolve(1),
3,
PromiseCtor.resolve(2)
]).then((values: number[]) => {
assert.deepEqual(values, [1, 3, 2]);
});
});
test(PromiseCtor.name + ', static-all, 3', function () {
return PromiseCtor.all([
PromiseCtor.resolve(1),
PromiseCtor.reject(13),
PromiseCtor.reject(12),
]).catch((values: number) => {
assert.deepEqual(values, 13);
});
});
test(PromiseCtor.name + ', static-race, 1', function () {
return PromiseCtor.race([
PromiseCtor.resolve(1),
PromiseCtor.resolve(2),
]).then((value: number) => {
assert.deepEqual(value, 1);
});
});
test(PromiseCtor.name + ', static-race, 2', function () {
return PromiseCtor.race([
PromiseCtor.reject(-1),
PromiseCtor.resolve(2),
]).catch((value: number) => {
assert.deepEqual(value, -1);
});
});
test(PromiseCtor.name + ', static-race, 3', function () {
return PromiseCtor.race([
PromiseCtor.resolve(1),
PromiseCtor.reject(2),
]).then((value: number) => {
assert.deepEqual(value, 1);
});
});
test(PromiseCtor.name + ', throw in ctor', function () {
return new PromiseCtor(() => {
throw new Error('sooo bad');
}).catch((err: Error) => {
assert.equal(err.message, 'sooo bad');
});
});
});
});