/
Pipeline.test.ts
382 lines (305 loc) · 10.4 KB
/
Pipeline.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import Pipeline from ".";
import {
Context,
context,
Global,
global,
TestFn,
TestMatchFN,
value,
} from "../../utils/tests";
describe("Pipeline class", () => {
const fn1: TestFn = jest.fn(x => x + 1);
const fn2: TestFn = jest.fn(x => x * 2);
const fn3: TestFn<number, string> = jest.fn(x => x.toString());
const pipeline = new Pipeline<number, Context, Global>()
.pipe(fn1)
.pipe(fn2)
.pipe(fn3);
const expected = "22";
function expectFns() {
expect(fn1).toHaveBeenCalledWith(10, context, global);
expect(fn2).toHaveBeenCalledWith(11, context, global);
expect(fn3).toHaveBeenCalledWith(22, context, global);
}
beforeEach(jest.clearAllMocks);
test("executes functions in order and passes the contexts", () => {
const actual = pipeline.run(value, context, global);
expect(actual).toBe(expected);
expectFns();
});
test("can be composed to a function", () => {
const composition = pipeline.compose();
const actual = composition(value, context, global);
expect(actual).toBe(expected);
expectFns();
});
test("can be nested", () => {
const pipeline = new Pipeline<number, Context, Global>()
.pipe(fn1)
.pipe(new Pipeline<number, Context, Global>().pipe(fn2).compose())
.pipe(fn3);
const actual = pipeline.run(value, context, global);
expect(actual).toBe(expected);
expectFns();
});
test("works with promises", () => {
const fn = jest.fn(async x => x * 2);
const pipeline = new Pipeline<number, Context, Global>()
.pipe(fn1)
.pipe(fn)
.pipe(fn3);
const actual = pipeline.run(value, context, global);
expect(actual).resolves.toBe(expected);
});
describe("context", () => {
test("can change the context", () => {
const pipeline = new Pipeline<number, Context, Global>()
.context(fn1)
.pipe(fn2 as unknown as TestFn<number, number, number>)
.pipe(fn3 as unknown as TestFn<number, string, number>);
const actual = pipeline.run(value, context, global);
const expected = "20";
const expectedContext = 11;
expect(actual).toBe(expected);
expect(fn1).toHaveBeenCalledWith(10, context, global);
expect(fn2).toHaveBeenCalledWith(10, expectedContext, global);
expect(fn3).toHaveBeenCalledWith(20, expectedContext, global);
});
test("can change the context (promise)", async () => {
const ctxFn: TestFn<number, Promise<number>> = jest.fn(async x => x + 1);
const pipeline = new Pipeline<number, Context, Global>()
.context(ctxFn)
.pipe(fn2 as unknown as TestFn<number, number, number>)
.pipe(fn3 as unknown as TestFn<number, string, number>);
const actual = await pipeline.run(value, context, global);
const expected = "20";
const expectedContext = 11;
expect(actual).toBe(expected);
expect(ctxFn).toHaveBeenCalledWith(10, context, global);
expect(fn2).toHaveBeenCalledWith(10, expectedContext, global);
expect(fn3).toHaveBeenCalledWith(20, expectedContext, global);
});
});
describe("error handler", () => {
const error = new Error();
const badFn: TestFn<unknown, void> = jest.fn(() => {
throw error;
});
const catchError: TestFn<unknown, unknown> = jest.fn(e => e);
test("can pass an error handler", () => {
const pipeline = new Pipeline<number, Context, Global>()
.catch(catchError)
.pipe(fn1)
.pipe(fn2)
.pipe(fn3)
.pipe(badFn);
const actual = pipeline.run(1, context, global);
expect(actual).toBe(error);
expect(catchError).toHaveBeenCalledWith(error, context, global);
});
describe("can pass multiple error handlers", () => {
const expected1 = new Error("1");
const catchError1: TestFn<unknown, Error> = jest.fn(() => expected1);
const expected2 = new Error("2");
const catchError2: TestFn<unknown, Error> = jest.fn(() => expected2);
const eh1: TestFn<number, number> = jest.fn(value => {
if (value < 5) {
throw error;
}
return value * 2;
});
const eh2: TestFn<number, number> = jest.fn(value => {
if (value > 15) {
throw error;
}
return value / 2;
});
const eh3: TestFn<number, string> = jest.fn(value => value.toString());
const pipeline = new Pipeline<number, Context, Global>()
.catch(catchError1)
.pipe(eh1)
.catch(catchError2)
.pipe(eh2)
.pipe(eh3);
test("calls the first error handler", () => {
const value = 4;
const actual = pipeline.run(value, context, global);
expect(actual).toBe(expected1);
expect(eh1).toHaveBeenCalledWith(value, context, global);
expect(catchError1).toHaveBeenCalledWith(error, context, global);
expect(eh2).not.toHaveBeenCalled();
});
test("calls the second error handler", () => {
const value = 20;
const actual = pipeline.run(value, context, global);
expect(actual).toBe(expected2);
expect(eh1).toHaveBeenCalledWith(value, context, global);
expect(catchError1).not.toHaveBeenCalled();
expect(eh2).toHaveBeenCalledWith(40, context, global);
expect(catchError2).toHaveBeenCalledWith(error, context, global);
});
test("returns value if no error occurs", () => {
const value = 7;
const actual = pipeline.run(value, context, global);
const expected = "7";
expect(actual).toBe(expected);
expect(eh1).toHaveBeenCalledWith(value, context, global);
expect(catchError1).not.toHaveBeenCalled();
expect(eh2).toHaveBeenCalledWith(14, context, global);
expect(catchError2).not.toHaveBeenCalled();
expect(eh3).toHaveBeenCalledWith(value, context, global);
});
});
describe("clears error handler before starting", () => {
const fn: TestFn<number, number> = jest.fn(value => {
if (value < 5) {
throw error;
}
return value * 2;
});
const catchError: TestFn<unknown, unknown> = jest.fn(e => e);
const pipeline = new Pipeline<number, Context, Global>()
.pipe(value => {
if (value > 5) {
throw error;
}
return value;
})
.catch(catchError)
.pipe(fn)
.compose();
test("calls the error handler the first time", () => {
const actual = pipeline(1, context, global);
expect(actual).toBe(error);
expect(catchError).toHaveBeenCalledWith(error, context, global);
});
test("does not call the error handler the second time", () => {
const actual = () => pipeline(value, context, global);
expect(actual).toThrow(error);
expect(catchError).not.toHaveBeenCalled();
});
});
test("throws original error if no error handler is provided", () => {
const pipeline = new Pipeline<number, Context, Global>().pipe(badFn);
const actual = () => pipeline.run(value, context, global);
expect(actual).toThrowError(error);
});
describe("async cases", () => {
const catchErrorAsync: TestFn<unknown, Promise<unknown>> = jest.fn(
async e => e
);
test("can pass an error handler (async)", async () => {
const pipeline = new Pipeline<number, Context, Global>()
.catch(catchErrorAsync)
.pipe(badFn);
const result = await pipeline.run(value, context, global);
expect(result).toBe(error);
expect(catchErrorAsync).toHaveBeenCalledWith(error, context, global);
});
describe("can pass multiple error handlers (async)", () => {
const expected1 = new Error("1");
const catchError1: TestFn<unknown, Promise<Error>> = jest.fn(
async () => expected1
);
const expected2 = new Error("2");
const catchError2: TestFn<unknown, Promise<Error>> = jest.fn(
async () => expected2
);
const eh1: TestFn<number, number> = jest.fn(value => {
if (value < 5) {
throw error;
}
return value * 2;
});
const eh2: TestFn<number, number> = jest.fn(value => {
if (value > 15) {
throw error;
}
return value / 2;
});
const eh3: TestFn<number, string> = jest.fn(value => value.toString());
const pipeline = new Pipeline<number, Context, Global>()
.catch(catchError1)
.pipe(eh1)
.catch(catchError2)
.pipe(eh2)
.pipe(eh3);
test("calls the first error handler (async)", () => {
const value = 4;
const actual = pipeline.run(value, context, global);
expect(actual).resolves.toBe(expected1);
expect(eh1).toHaveBeenCalledWith(value, context, global);
expect(catchError1).toHaveBeenCalledWith(error, context, global);
expect(eh2).not.toHaveBeenCalled();
});
test("calls the second error handler (async)", () => {
const value = 20;
const actual = pipeline.run(value, context, global);
expect(actual).resolves.toBe(expected2);
expect(eh1).toHaveBeenCalledWith(value, context, global);
expect(catchError1).not.toHaveBeenCalled();
expect(eh2).toHaveBeenCalledWith(40, context, global);
expect(catchError2).toHaveBeenCalledWith(error, context, global);
});
test("returns value if no error occurs (async)", () => {
const value = 7;
const actual = pipeline.run(value, context, global);
const expected = "7";
expect(actual).toBe(expected);
expect(eh1).toHaveBeenCalledWith(value, context, global);
expect(catchError1).not.toHaveBeenCalled();
expect(eh2).toHaveBeenCalledWith(14, context, global);
expect(catchError2).not.toHaveBeenCalled();
expect(eh3).toHaveBeenCalledWith(value, context, global);
});
});
test("throws original error if no error handler is provided (async)", () => {
const pipeline = new Pipeline<number, Context, Global>().pipe(
async () => {
throw error;
}
);
const result = pipeline.run(1, context, global);
expect(result).rejects.toThrowError(error);
});
});
});
test("works with steps", () => {
const tap: TestFn<number, void> = jest.fn(() => {});
const assert: TestFn<number, Error> = jest.fn(() => new Error());
const ifelse: [
TestFn<number, boolean>,
TestFn<number, number>,
TestFn<number, number>
] = [
jest.fn(value => value > 2),
jest.fn(value => value / 2),
jest.fn(value => value * 2),
];
const match: TestMatchFN<number, string> = jest.fn(m =>
m
.on(
value => value === 2,
() => "two"
)
.on(
value => value === 3,
() => "three"
)
.otherwise(() => "other")
);
const pipeline = new Pipeline<number, Context, Global>()
.tap(tap)
.assert(assert)
.ifelse(...ifelse)
.match(match)
.pairwise(() => "result");
expect(pipeline.run(value, context, global)).toStrictEqual([
"other",
"result",
]);
expect(pipeline.run(1, context, global)).toStrictEqual(["two", "result"]);
expect(pipeline.run(6, context, global)).toStrictEqual(["three", "result"]);
});
});