-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.ts
84 lines (72 loc) · 2.57 KB
/
index.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
import 'https://gist.githubusercontent.com/qwtel/b14f0f81e3a96189f7771f83ee113f64/raw/TestRequest.ts'
import {
assert,
assertExists,
assertEquals,
assertStrictEquals,
assertStringIncludes,
assertThrows,
assertRejects,
} from 'https://deno.land/std@0.133.0/testing/asserts.ts'
const { test } = Deno;
import { ExtendablePromise } from '../index.ts';
test('exists', () => {
assertExists(ExtendablePromise)
})
test('should be instantiable as a class', () => {
assertExists(new ExtendablePromise())
});
test('initialize it with a value', async () => {
assertExists(await new ExtendablePromise(true))
})
test('promise settled result', async () => {
assertEquals(await new ExtendablePromise(true), [{ status: 'fulfilled', value: true }])
})
test('promise settled result failure', async () => {
assertEquals(await new ExtendablePromise(Promise.reject(Error())), [{ status: 'rejected', reason: Error() }])
})
const timeout = (n: number) => new Promise(r => setTimeout(r, n))
test('extending the promise', async () => {
const start = Date.now()
const t1 = timeout(10)
const p = new ExtendablePromise(t1)
p.waitUntil(timeout(20))
await t1;
assertEquals(p.settled, false)
await p
assert(Date.now() - start >= 9)
assertEquals(p.settled, true)
})
test('not extending the promise after settlement', async () => {
const p = new ExtendablePromise(timeout(10))
p.waitUntil(timeout(50))
await p
assertEquals(p.settled, true)
const t1 = timeout(10);
assertEquals(p.waitUntil(t1), undefined)
await t1
assertEquals(p.settled, true)
})
test('multiple with ordering', async () => {
const p = new ExtendablePromise(timeout(30).then(() => 1))
p.waitUntil(timeout(40).then(() => 2))
p.waitUntil(timeout(20).then(() => 3))
assertEquals(await p, [{ status: 'fulfilled', value: 1 }, { status: 'fulfilled', value: 2 }, { status: 'fulfilled', value: 3 }])
})
test('multiple with rejections', async () => {
const p = new ExtendablePromise(timeout(30).then(() => 1))
p.waitUntil(timeout(40).then(() => Promise.reject(2)))
p.waitUntil(timeout(20).then(() => 3))
assertEquals(await p, [{ status: 'fulfilled', value: 1 }, { status: 'rejected', reason: 2 }, { status: 'fulfilled', value: 3 }])
})
test('total time of extended promise', async () => {
const start = Date.now()
const p = new ExtendablePromise(timeout(20).then(() => 1));
await timeout(10)
p.waitUntil(timeout(30).then(() => Promise.reject(Error('2'))));
assertEquals(await p, [
{ status: "fulfilled", value: 1 },
{ status: "rejected", reason: Error('2') },
]);
assert(Date.now() - start >= 40)
})