feat: update tests
This commit is contained in:
@@ -5,19 +5,18 @@ import { Relay } from "../../libraries/relay.ts";
|
||||
import { route } from "../../libraries/route.ts";
|
||||
import { UserSchema } from "./user.ts";
|
||||
|
||||
export const relay = new Relay({ adapter: http }, [
|
||||
export const relay = new Relay({ url: "http://localhost:36573", adapter: http }, [
|
||||
route
|
||||
.post("/users")
|
||||
.body(UserSchema.omit({ id: true }))
|
||||
.body(UserSchema.omit({ id: true, createdAt: 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())),
|
||||
.response(UserSchema),
|
||||
route
|
||||
.put("/users/:userId")
|
||||
.params({ userId: z.string().check(z.uuid()) })
|
||||
.body(UserSchema.omit({ id: true })),
|
||||
.body(UserSchema.omit({ id: true, createdAt: true })),
|
||||
route.delete("/users/:userId").params({ userId: z.string().check(z.uuid()) }),
|
||||
]);
|
||||
|
||||
@@ -1,32 +1,33 @@
|
||||
import { Api } from "../../libraries/api.ts";
|
||||
import { NotFoundError } from "../../mod.ts";
|
||||
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;
|
||||
export const api = new Api([
|
||||
relay.route("POST", "/users").handle(async ({ name, email }) => {
|
||||
const id = crypto.randomUUID();
|
||||
users.push({ id, name, email, createdAt: new Date() });
|
||||
return id;
|
||||
}),
|
||||
relay.route("GET", "/users/:userId").handle(async ({ userId }) => {
|
||||
const user = users.find((user) => user.id === userId);
|
||||
if (user === undefined) {
|
||||
return new NotFoundError();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => {
|
||||
users = users.filter((user) => user.id === userId);
|
||||
});
|
||||
return user;
|
||||
}),
|
||||
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);
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -4,6 +4,7 @@ export const UserSchema = z.object({
|
||||
id: z.string().check(z.uuid()),
|
||||
name: z.string(),
|
||||
email: z.string().check(z.email()),
|
||||
createdAt: z.coerce.date(),
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof UserSchema>;
|
||||
|
||||
@@ -1,14 +1,51 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { describe, it } from "@std/testing/bdd";
|
||||
import "./mocks/server.ts";
|
||||
|
||||
import { assertEquals, assertObjectMatch } from "@std/assert";
|
||||
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
|
||||
|
||||
import { relay } from "./mocks/relay.ts";
|
||||
import { api, users } from "./mocks/server.ts";
|
||||
|
||||
describe("Relay", () => {
|
||||
it("should create a new user", async () => {
|
||||
let server: Deno.HttpServer<Deno.NetAddr>;
|
||||
|
||||
beforeAll(() => {
|
||||
server = Deno.serve(
|
||||
{
|
||||
port: 36573,
|
||||
hostname: "localhost",
|
||||
onListen({ port, hostname }) {
|
||||
console.log(`Listening at http://${hostname}:${port}`);
|
||||
},
|
||||
},
|
||||
async (request) => api.handle(request),
|
||||
);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await server.shutdown();
|
||||
});
|
||||
|
||||
it("should successfully relay users", async () => {
|
||||
const userId = await relay.post("/users", { name: "John Doe", email: "john.doe@fixture.none" });
|
||||
|
||||
console.log({ userId });
|
||||
|
||||
assertEquals(typeof userId, "string");
|
||||
assertEquals(users.length, 1);
|
||||
|
||||
const user = await relay.get("/users/:userId", { userId });
|
||||
|
||||
assertEquals(user.createdAt instanceof Date, true);
|
||||
|
||||
await relay.put("/users/:userId", { userId }, { name: "Jane Doe", email: "jane.doe@fixture.none" });
|
||||
|
||||
assertEquals(users.length, 1);
|
||||
assertObjectMatch(users[0], {
|
||||
name: "Jane Doe",
|
||||
email: "jane.doe@fixture.none",
|
||||
});
|
||||
|
||||
await relay.delete("/users/:userId", { userId });
|
||||
|
||||
assertEquals(users.length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user