-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
226 lines (188 loc) · 4.76 KB
/
index.spec.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* eslint-env jest */
const { defer } = require("./");
// expect(promise).rejects.toThrow does not work with Jest 21
const makeSyncWrapper = promise =>
promise.then(
value => () => value,
reason => () => {
throw reason;
}
);
const waitATurn = () => new Promise(resolve => setTimeout(resolve, 0));
describe("defer()", () => {
it("works with normal termination", () => {
let i = 0;
const fn = defer($defer => {
i += 2;
$defer(() => {
i -= 2;
});
i *= 2;
$defer(() => {
i /= 2;
});
return i;
});
expect(fn()).toBe(4);
expect(i).toBe(0);
});
it("works with exception", () => {
let i = 0;
const fn = defer($defer => {
i += 2;
$defer(() => {
i -= 2;
});
i *= 2;
$defer(() => {
i /= 2;
});
throw i;
});
try {
fn();
expect(true).toBe(false);
} catch (value) {
expect(value).toBe(4);
}
expect(i).toBe(0);
});
it("works with promise resolution", () => {
let i = 0;
const fn = defer($defer => {
i += 2;
$defer(() => {
i -= 2;
});
i *= 2;
$defer(() => {
i /= 2;
});
return waitATurn().then(() => i);
});
return fn().then(result => {
expect(result).toBe(4);
expect(i).toBe(0);
});
});
it("works with promise rejection", () => {
let i = 0;
const fn = defer($defer =>
waitATurn()
.then(() => {
i += 2;
$defer(() => {
i -= 2;
});
i *= 2;
$defer(() => {
i /= 2;
});
})
.then(() => waitATurn())
.then(() => {
throw i;
})
);
return fn().catch(result => {
expect(result).toBe(4);
expect(i).toBe(0);
});
});
it("should log exceptions from deferreds", () => {
const e1 = new Error("e1");
const e2 = new Error("e2");
const onError = jest.fn();
const fn = defer.onError(onError)($defer => {
$defer(() => {
throw e1;
});
$defer(() => {
throw e2;
});
});
fn();
expect(onError.mock.calls).toEqual([[e2], [e1]]);
});
it("should log exceptions/rejections from deferreds", () => {
const e1 = new Error("e1");
const e2 = new Error("e2");
const onError = jest.fn();
const fn = defer.onError(onError)($defer => {
$defer(() => {
throw e1;
});
$defer(() => Promise.reject(e2));
return Promise.resolve();
});
return fn().then(() => {
expect(onError.mock.calls).toEqual([[e2], [e1]]);
});
});
it("accepts optional context and arguments", () => {
const deferred = jest.fn();
defer($defer => {
$defer.call("foo", deferred, "bar", "baz");
})();
expect(deferred.mock.instances).toEqual(["foo"]);
expect(deferred.mock.calls).toEqual([["bar", "baz"]]);
});
it("accepts a context and a method name", () => {
const foo = jest.fn();
const obj = { foo };
defer($defer => {
$defer.call(obj, "foo", "bar", "baz");
})();
expect(foo.mock.instances).toEqual([obj]);
expect(foo.mock.calls).toEqual([["bar", "baz"]]);
});
});
describe("lifecycle", () => {
let always, failure, fn, success;
beforeEach(() => {
always = jest.fn();
failure = jest.fn();
success = jest.fn();
fn = defer(($defer, resultFn) => {
$defer(always);
$defer.onFailure(failure);
$defer.onSuccess(success);
return resultFn();
});
});
it("sync failure", () => {
expect(() =>
fn(() => {
throw new Error();
})
).toThrow();
expect(always.mock.calls.length).toBe(1);
expect(failure.mock.calls.length).toBe(1);
expect(success.mock.calls.length).toBe(0);
});
it("sync success", () => {
expect(fn(() => 42)).toBe(42);
expect(always.mock.calls.length).toBe(1);
expect(failure.mock.calls.length).toBe(0);
expect(success.mock.calls.length).toBe(1);
});
it("async failure", () =>
makeSyncWrapper(fn(() => Promise.reject(new Error()))).then(syncWrapper => {
expect(syncWrapper).toThrow();
expect(always.mock.calls.length).toBe(1);
expect(failure.mock.calls.length).toBe(1);
expect(success.mock.calls.length).toBe(0);
}));
it("async success", () =>
fn(() => Promise.resolve(42)).then(result => {
expect(result).toBe(42);
expect(always.mock.calls.length).toBe(1);
expect(failure.mock.calls.length).toBe(0);
expect(success.mock.calls.length).toBe(1);
}));
it("sets name and length to wrapper function", () => {
const wrapper = defer(function foo($defer, a, b, c) {});
expect(wrapper.name).toBe("defer(foo)");
expect(wrapper.length).toBe(3);
});
});