-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest.js
More file actions
149 lines (130 loc) · 3.77 KB
/
Copy pathtest.js
File metadata and controls
149 lines (130 loc) · 3.77 KB
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
import test from 'ava';
import {asyncCounter} from 'async-counter';
import {Nancy, states} from '.';
test('empty executor results in a pending promise', t => {
const p = new Nancy(() => {});
t.is(p.state, states.pending);
});
const throwSomethingWrong = () => {
throw new Error('Something went wrong...');
};
test('error thrown during resolve execution results in a rejected promise', t => {
const p = new Nancy(throwSomethingWrong);
t.is(p.state, states.rejected);
});
test('simple resolve and reject works', t => {
let p = Nancy.resolve(42);
t.is(p.state, states.resolved);
t.is(p.value, 42);
p = Nancy.reject(42);
t.is(p.state, states.rejected);
t.is(p.value, 42);
});
test('chain then sync', t => {
let p = Nancy.reject(42)
.then(() => 0)
.then(() => 1)
.then(() => 2);
t.is(p.state, states.rejected);
t.is(p.value, 42);
p = Nancy.resolve(0)
.then(value => {
t.is(value, 0);
return 1;
})
.then(value => {
t.is(value, 1);
return 2;
})
.then(throwSomethingWrong);
t.is(p.state, states.rejected);
});
const anything = t => () => t.fail('anything should never be called.');
test.cb('chain catch sync', t => {
Nancy.reject(42)
.catch(error => error)
.catch(anything(t))
.then(throwSomethingWrong)
.catch(throwSomethingWrong)
.catch(() => t.end());
});
test.cb('unpack promise value on resolve, not reject', t => {
Nancy.reject(Nancy.resolve(Nancy.reject(42)))
.catch(value => {
t.is(value instanceof Nancy, true);
return value;
})
.catch(value => {
t.is(value, 42);
return value;
})
.then(value => t.is(value, 42))
.then(() => Nancy.reject(24))
.catch(value => t.is(value, 24))
.then(() => t.end());
});
const delay = () => new Nancy(resolve => setTimeout(resolve, 500));
test.cb('chain then async', t => {
delay()
.then(delay)
.then(delay)
.then(() => t.end());
});
test.cb('chain catch async', t => {
delay()
.then(() => Nancy.reject())
.catch(throwSomethingWrong)
.catch(() => 42)
.catch(anything(t))
.then(value => t.is(value, 42))
.then(delay)
.then(throwSomethingWrong)
.catch(() => t.end());
});
test.cb('multiple then on single promise', t => {
const counter = asyncCounter(3, {onFinished: () => t.end()});
const p = delay();
p.then(counter.count);
p.then(counter.count);
p.then(delay).then(counter.count);
});
test.cb('multiple catch on single promise', t => {
const counter = asyncCounter(3, {onFinished: () => t.end()});
const p = delay()
.then(() => Nancy.reject());
p.then(anything(t));
p.catch(counter.count);
p.catch(counter.count);
p.catch(delay).then(counter.count);
});
const carryAfterDelays = (value, numberOfDelays = 0) => {
let wait = Nancy.resolve();
for (let i = 0; i < numberOfDelays; i++) {
wait = wait
.then(delay);
}
return wait
.then(() => value);
};
test.cb('subsequent resolves and rejects are ignored', t => {
let p = new Nancy((resolve, reject) => {
reject(42);
reject();
resolve(24);
});
p
.then(value => t.fail(value))
.catch(value => t.is(value, 42));
const shorter = carryAfterDelays(42, 1)
.then(value => Nancy.reject(value));
const longer = carryAfterDelays(24, 2);
p = new Nancy(resolve => {
resolve(longer);
resolve(shorter);
});
longer
.then(() => p)
.then(value => t.fail(value))
.catch(value => t.is(value, 42))
.then(() => t.end());
});