forked from neurosnap/starfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose.test.ts
59 lines (53 loc) · 1.33 KB
/
compose.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
import { describe, expect, it } from "./test.ts";
import { run, sleep } from "./deps.ts";
import { compose } from "./compose.ts";
const tests = describe("compose()");
it(tests, "should compose middleware", async () => {
const mdw = compose<{ one: string; three: string }>([
function* (ctx, next) {
ctx.one = "two";
yield* next();
},
function* (ctx, next) {
ctx.three = "four";
yield* next();
},
]);
const actual = await run(function* () {
return yield* mdw({ one: "", three: "" });
});
const expected = {
// we should see the mutation
one: "two",
three: "four",
};
expect(actual).toEqual(expected);
});
it(tests, "order of execution", async () => {
const mdw = compose<{ actual: string }>([
function* (ctx, next) {
ctx.actual += "a";
yield* next();
ctx.actual += "g";
},
function* (ctx, next) {
yield* sleep(10);
ctx.actual += "b";
yield* next();
yield* sleep(10);
ctx.actual += "f";
},
function* (ctx, next) {
ctx.actual += "c";
yield* next();
ctx.actual += "d";
yield* sleep(30);
ctx.actual += "e";
},
]);
const actual = await run(function* () {
return yield* mdw({ actual: "" });
});
const expected = { actual: "abcdefg" };
expect(actual).toEqual(expected);
});