-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-test.js
178 lines (139 loc) · 4.44 KB
/
index-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
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
const test = require('ava');
const Episode7 = require('.');
test('Run a generator yielding promises from side-effects', t => {
t.plan(4);
function* fortuneTeller(question, subject) {
let fortune = [];
fortune = fortune.concat( yield Episode7.call(customerAsks, question, subject) );
fortune = fortune.concat( yield Episode7.call(readerSays, '🔮') );
fortune = fortune.concat( yield Episode7.call(crystalProjects, '💰') );
return Promise.resolve(fortune);
}
function customerAsks(...v) {
t.deepEqual(['📡', '👽'], v)
return Promise.resolve(v);
}
function readerSays(v) {
t.is('🔮', v)
return Promise.resolve(v);
}
function crystalProjects(v) {
t.is('💰', v)
return Promise.resolve(v);
}
return Episode7.run(fortuneTeller, '📡', '👽')
.then( fortune => t.deepEqual(['📡', '👽', '🔮', '💰'], fortune) );
});
test('Run a generator yielding plain return values from side-effects', t => {
t.plan(4);
function* fortuneTeller(question, subject) {
let fortune = [];
fortune = fortune.concat( yield Episode7.call(customerAsks, question, subject) );
fortune = fortune.concat( yield Episode7.call(readerSays, '🔮') );
fortune = fortune.concat( yield Episode7.call(crystalProjects, '💰') );
return fortune;
}
function customerAsks(...v) {
t.deepEqual(['📡', '👽'], v)
return v;
}
function readerSays(v) {
t.is('🔮', v)
return v;
}
function crystalProjects(v) {
t.is('💰', v)
return v;
}
return Episode7.run(fortuneTeller, '📡', '👽')
.then( fortune => t.deepEqual(['📡', '👽', '🔮', '💰'], fortune) );
});
test('Run a generator yielding to nested `Episode7.call` for side-effects', t => {
t.plan(3);
function* fortuneTeller() {
let fortune = [];
fortune = fortune.concat( yield Episode7.call(readerSays, '🔮') );
fortune = fortune.concat( yield Episode7.call(crystalProjects, '💰') );
return fortune;
}
function* readerSays(v) {
t.is('🔮', v)
return yield Episode7.call(valuePromise, v);
}
function* crystalProjects(v) {
t.is('💰', v)
return yield Episode7.call(valuePromise, v);
}
function valuePromise(v) {
return Promise.resolve(v)
}
return Episode7.run(fortuneTeller)
.then( fortune => t.deepEqual(['🔮', '💰'], fortune) );
});
test('Calls side-effect functions with `this` arg', t => {
t.plan(1);
function* revealInnerSecret() {
let contents = { secret: '💎' }
return yield Episode7.call([contents, openUp]);
}
function openUp() {
let secret = this.secret;
return secret;
}
return Episode7.run(revealInnerSecret)
.then( secret => t.is('💎', secret) );
});
test('Clearly present side-effect errors', t => {
t.plan(1);
const fortunesFoolError = new Error('🚑');
function* fortuneTeller() {
let fortune = [];
fortune.push( yield Episode7.call(readerSays, '🔮') );
fortune.push( yield Episode7.call(crystalProjects, '💰') );
}
function readerSays(v) {
return Promise.resolve(v);
}
function crystalProjects(v) {
throw fortunesFoolError;
return Promise.resolve(v);
}
return Episode7.run(fortuneTeller)
.catch( error => t.is(error, fortunesFoolError) );
});
test('Clearly describe missing `Episode7.call` ', t => {
t.plan(1);
function* naughtyGenerator() {
yield noBueno('🚷');
}
function noBueno(v) {
return Promise.resolve(v);
}
return Episode7.run(naughtyGenerator)
.catch( error => t.regex(error.message, /requires using `Episode7\.call`/) );
});
test('Easily test yielded calls, mock their return values', t => {
t.plan(4);
function* emojiPoem(v1) {
let v2 = yield Episode7.call(iPromise, v1)
let v3 = yield Episode7.call(iPromise, v2)
yield Episode7.call(iPromise, v3)
}
function iPromise(v) {
t.fail('the tapped function should not run');
resolve(v);
}
// Inital generator args may be passed in from constructor
let reading = emojiPoem('🔋');
let it;
// First `next` does not provide a value, because nothing has yet been yielded
it = reading.next();
t.deepEqual({ fn: iPromise, args: ['🔋'] }, it.value);
// Mock return value from previous yield by passing arg to `next`
it = reading.next('💡');
t.deepEqual({ fn: iPromise, args: ['💡'] }, it.value);
it = reading.next('🎛');
t.deepEqual({ fn: iPromise, args: ['🎛'] }, it.value);
it = reading.next();
t.true(it.done);
});