feat: initial commit
This commit is contained in:
13
tests/mocks/actions.ts
Normal file
13
tests/mocks/actions.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import z from "zod";
|
||||
|
||||
import { action } from "../../libraries/action.ts";
|
||||
|
||||
export const addTwoNumbers = action
|
||||
.make("addTwoNumbers")
|
||||
.input({ a: z.number(), b: z.number() })
|
||||
.output({ added: z.number() })
|
||||
.handle(async ({ a, b }) => {
|
||||
return {
|
||||
added: a + b,
|
||||
};
|
||||
});
|
||||
23
tests/mocks/relay.ts
Normal file
23
tests/mocks/relay.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import z from "zod";
|
||||
|
||||
import { http } from "../../adapters/http.ts";
|
||||
import { Relay } from "../../libraries/relay.ts";
|
||||
import { route } from "../../libraries/route.ts";
|
||||
import { UserSchema } from "./user.ts";
|
||||
|
||||
export const relay = new Relay({ adapter: http }, [
|
||||
route
|
||||
.post("/users")
|
||||
.body(UserSchema.omit({ id: true }))
|
||||
.response(z.string()),
|
||||
route.get("/users").response(z.array(UserSchema)),
|
||||
route
|
||||
.get("/users/:userId")
|
||||
.params({ userId: z.string().check(z.uuid()) })
|
||||
.response(UserSchema.or(z.undefined())),
|
||||
route
|
||||
.put("/users/:userId")
|
||||
.params({ userId: z.string().check(z.uuid()) })
|
||||
.body(UserSchema.omit({ id: true })),
|
||||
route.delete("/users/:userId").params({ userId: z.string().check(z.uuid()) }),
|
||||
]);
|
||||
32
tests/mocks/server.ts
Normal file
32
tests/mocks/server.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { relay } from "./relay.ts";
|
||||
import { User } from "./user.ts";
|
||||
|
||||
export let users: User[] = [];
|
||||
|
||||
relay.route("POST", "/users").handle(async ({ name, email }) => {
|
||||
const id = crypto.randomUUID();
|
||||
users.push({ id, name, email });
|
||||
return id;
|
||||
});
|
||||
|
||||
relay.route("GET", "/users").handle(async () => {
|
||||
return users;
|
||||
});
|
||||
|
||||
relay.route("GET", "/users/:userId").handle(async ({ userId }) => {
|
||||
return users.find((user) => user.id === userId);
|
||||
});
|
||||
|
||||
relay.route("PUT", "/users/:userId").handle(async ({ userId, name, email }) => {
|
||||
for (const user of users) {
|
||||
if (user.id === userId) {
|
||||
user.name = name;
|
||||
user.email = email;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => {
|
||||
users = users.filter((user) => user.id === userId);
|
||||
});
|
||||
9
tests/mocks/user.ts
Normal file
9
tests/mocks/user.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import z from "zod";
|
||||
|
||||
export const UserSchema = z.object({
|
||||
id: z.string().check(z.uuid()),
|
||||
name: z.string(),
|
||||
email: z.string().check(z.email()),
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof UserSchema>;
|
||||
14
tests/route.test.ts
Normal file
14
tests/route.test.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { describe, it } from "@std/testing/bdd";
|
||||
|
||||
import { relay } from "./mocks/relay.ts";
|
||||
|
||||
describe("Relay", () => {
|
||||
it("should create a new user", async () => {
|
||||
const userId = await relay.post("/users", { name: "John Doe", email: "john.doe@fixture.none" });
|
||||
|
||||
console.log({ userId });
|
||||
|
||||
assertEquals(typeof userId, "string");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user