-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.ts
67 lines (59 loc) · 1.68 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
import { expect, test } from "vitest";
import { Console, Context, Effect } from "effect";
import { effect } from "./index.js";
function* getTodo(id: string) {
const todoService = yield* TodoService;
const todo = yield* todoService.getTodoById(id);
if (todo.description.length < 2) {
return yield* Effect.fail(new ValidationError("Too small description"));
}
return todo;
}
const wrappedInEffect = effect(function* (first: string, second: number) {
yield* Console.log(first);
yield* Console.log(second);
return first + second.toString();
});
test("effect", async () => {
const program = effect(function* () {
const id = yield* wrappedInEffect("id-", 1);
return yield* getTodo(id);
});
const todo = await program.pipe(
Effect.provideService(TodoService, {
getTodoById: effect(function* (id: string) {
return { description: "Learn effect", id };
}),
}),
Effect.scoped,
Effect.runPromise,
);
expect(todo).toEqual({
description: "Learn effect",
id: "id-1",
});
});
test("can pass this to generator", async () => {
class MyService {
readonly local = 1;
compute = effect(this, function* (this: MyService, add: number) {
return yield* Effect.succeed(this.local + add);
});
}
const instance = new MyService();
expect(Effect.runSync(instance.compute(2))).toBe(3);
});
export class NotFoundError extends Error {
readonly name = "NotFoundError";
}
export class ValidationError extends Error {
readonly name = "ValidationError";
}
class TodoService extends Context.Tag("TodoService")<
TodoService,
{
getTodoById(
id: string,
): Effect.Effect<{ description: string }, NotFoundError>;
}
>() {}