-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
143 lines (105 loc) · 2.78 KB
/
test.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
134
135
136
137
138
139
140
141
142
143
import {setTimeout as delay} from 'node:timers/promises';
import test from 'ava';
import PLazy from './index.js';
const fixture = Symbol('fixture');
test('executor resolves', async t => {
const steps = [];
const lazyPromise = new PLazy(resolve => {
steps.push('executor called');
resolve(fixture);
});
steps.push('promise created');
await delay(50);
steps.push('then called');
await lazyPromise.then(value => {
t.is(value, fixture);
steps.push('then-handler called');
});
t.deepEqual(steps, [
'promise created',
'then called',
'executor called',
'then-handler called',
]);
});
test('executor rejects', async t => {
const fixtureError = new Error('fixture');
const steps = [];
const lazyPromise = new PLazy((resolve, reject) => {
steps.push('executor called');
reject(fixtureError);
});
steps.push('promise created');
await delay(50);
steps.push('catch called');
await lazyPromise.catch(error => {
t.is(error, fixtureError);
steps.push('catch-handler called');
});
t.deepEqual(steps, [
'promise created',
'catch called',
'executor called',
'catch-handler called',
]);
});
test('executor is never called if no `then`', async t => {
t.plan(1);
new PLazy(resolve => { // eslint-disable-line no-new
t.fail('executor should not be called');
resolve();
});
await delay(50);
t.pass();
});
test('executor is called with only catch handler', async t => {
const steps = [];
const lazyPromise = new PLazy(resolve => {
steps.push('executor called');
resolve();
});
steps.push('promise created');
await delay(50);
steps.push('catch called');
await lazyPromise.catch(() => {});
t.deepEqual(steps, [
'promise created',
'catch called',
'executor called',
]);
});
test('convert promise-returning function to lazy promise', async t => {
let called = false;
const lazyPromise = PLazy.from(async () => {
called = true;
return fixture;
});
t.true(lazyPromise instanceof PLazy);
t.true(lazyPromise instanceof Promise);
t.false(called);
t.is(await lazyPromise, fixture);
t.true(called);
});
test('should have static method `reject` that returns a lazy rejected promise', async t => {
const fixtureError = new Error('fixture');
const steps = [];
const lazyPromise = PLazy.reject(fixtureError);
steps.push('promise created');
await delay(50);
steps.push('catch called');
await lazyPromise.catch(error => {
t.is(error, fixtureError);
steps.push('catch-handler called');
});
t.deepEqual(steps, [
'promise created',
'catch called',
'catch-handler called',
]);
});
test('should have static method `resolve` that returns a lazy resolved promise', async t => {
const lazyPromise = PLazy.resolve(fixture);
t.true(lazyPromise instanceof PLazy);
t.true(lazyPromise instanceof Promise);
t.is(await lazyPromise, fixture);
});