-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
177 lines (156 loc) · 5.03 KB
/
index.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
import { flowPipe } from "../src/index";
import { types, onPatch, IJsonPatch } from "mobx-state-tree";
it("returns the last resolved value from the pipe", async () => {
const store = types
.model({})
.actions((self) => ({
action1: flowPipe(() => 123).end(),
action2: flowPipe(() => Promise.resolve("456")).end(),
action3: flowPipe(() => Promise.resolve("456"))
.then((x) => Promise.resolve(x + "789"))
.end(),
}))
.create({});
await expect(store.action1()).resolves.toBe(123);
await expect(store.action2()).resolves.toBe("456");
await expect(store.action3()).resolves.toBe("456789");
});
it("can take an initial typed input", async () => {
const store = types
.model({})
.actions((self) => ({
action1: flowPipe((input: string) => "hello " + input).end(),
action2: flowPipe((input: number) => Promise.resolve(input))
.then((next) => next + 100)
.then((next) => Promise.resolve(next + 100))
.then((next) => Promise.resolve(next + ""))
.end(),
}))
.create({});
await expect(store.action1("foo")).resolves.toBe("hello foo");
await expect(store.action2(1)).resolves.toBe("201");
});
it("allows modification the model mid-flow", async () => {
const store = types
.model({
value: types.optional(types.string, ""),
})
.actions((self) => ({
setValue(val: string) {
self.value = val;
},
}))
.actions((self) => ({
action1: flowPipe((input: number) => {
self.value = input + "";
return input + 100;
})
.then((next) => {
self.setValue(next + "");
return Promise.resolve(next + 100);
})
.end(),
}))
.create({});
let patches: IJsonPatch[] = [];
onPatch(store, (p) => patches.push(p));
await expect(store.action1(1)).resolves.toBe(201);
expect(store.value).toBe("101");
expect(patches).toEqual([
{
op: "replace",
path: "/value",
value: "1",
},
{
op: "replace",
path: "/value",
value: "101",
},
]);
});
it("returns errors as promise rejections", async () => {
const store = types
.model({})
.actions((self) => ({
action1: flowPipe((input: number) => Promise.resolve(input))
.then((num) => (num > 100 ? Promise.reject("naa") : num + 100))
.then((num) => Promise.resolve(num + ""))
.end(),
action2: flowPipe((input: number) => Promise.resolve(input))
.then((num) => {
if (num > 100) throw new Error("nope");
return num + 100;
})
.then((num) => Promise.resolve(num + ""))
.end(),
}))
.create({});
await expect(store.action1(99)).resolves.toBe("199");
await expect(store.action1(101)).rejects.toEqual("naa");
await expect(store.action2(99)).resolves.toBe("199");
await expect(store.action2(101)).rejects.toThrow("nope");
});
it("can optionally handle errors", async () => {
const store = types
.model({})
.actions((self) => ({
action1: flowPipe((input: number) => Promise.resolve(input))
.then((num) => (num > 100 ? Promise.reject("naa") : num + 100))
.catch((err) => {
return "something bad happened";
})
.end(),
}))
.create({});
await expect(store.action1(99)).resolves.toBe(199);
await expect(store.action1(101)).resolves.toBe("something bad happened");
});
it("can optionally handle errors with no input", async () => {
const store = types
.model({})
.actions((self) => ({
action1: flowPipe(() => Promise.resolve({ a: "anon", b: 123 }))
.then((o) => {
if (o.b < 9999) throw new Error("oops");
return o.b;
})
.catch((err) => {
return "oopsie";
})
.end(),
}))
.create({});
await expect(store.action1()).resolves.toBe("oopsie");
});
it("can continue on after and error has been caught", async () => {
const reportErrorToServer = (err: Error) => Promise.resolve(err);
const store = types
.model({})
.actions((self) => ({
action1: flowPipe((input: number) => Promise.resolve(input))
.then((num) => (num > 100 ? Promise.reject("naa") : num + 100))
.catch((err) => reportErrorToServer(new Error("something bad happened")))
.then((result) => (result instanceof Error ? "error reported to server" : result))
.end(),
}))
.create({});
await expect(store.action1(99)).resolves.toBe(199);
await expect(store.action1(101)).resolves.toBe("error reported to server");
});
it("can nest flow functions", async () => {
const store = types
.model({})
.actions((self) => ({
action1: flowPipe((input: number) => Promise.resolve(input + 100)).end(),
}))
.actions((self) => ({
action2: flowPipe((input: number) => Promise.resolve(input + 10))
.then((next) => self.action1(next + 10))
.then((next) => next + "")
.end(),
}))
.create({});
await expect(store.action1(1)).resolves.toBe(101);
await expect(store.action2(1)).resolves.toBe("121");
});