Template
1
0

feat: split client to separate class

This commit is contained in:
2025-04-20 11:56:40 +00:00
parent 2e8d6b76d7
commit 221452893e
11 changed files with 220 additions and 195 deletions

View File

@@ -3,11 +3,14 @@ import "./mocks/server.ts";
import { assertEquals, assertObjectMatch, assertRejects } from "@std/assert";
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { relay } from "./mocks/relay.ts";
import { adapter } from "../adapters/http.ts";
import { RelayClient } from "../libraries/client.ts";
import { relay, RelayRoutes } from "./mocks/relay.ts";
import { api, users } from "./mocks/server.ts";
describe("Relay", () => {
let server: Deno.HttpServer<Deno.NetAddr>;
let client: RelayClient<RelayRoutes>;
beforeAll(() => {
server = Deno.serve(
@@ -20,6 +23,7 @@ describe("Relay", () => {
},
async (request) => api.handle(request),
);
client = new RelayClient({ url: "http://localhost:36573", adapter, routes: relay.routes });
});
afterAll(async () => {
@@ -27,16 +31,16 @@ describe("Relay", () => {
});
it("should successfully relay users", async () => {
const userId = await relay.post("/users", { name: "John Doe", email: "john.doe@fixture.none" });
const userId = await client.post("/users", { name: "John Doe", email: "john.doe@fixture.none" });
assertEquals(typeof userId, "string");
assertEquals(users.length, 1);
const user = await relay.get("/users/:userId", { userId });
const user = await client.get("/users/:userId", { userId });
assertEquals(user.createdAt instanceof Date, true);
await relay.put("/users/:userId", { userId }, { name: "Jane Doe", email: "jane.doe@fixture.none" });
await client.put("/users/:userId", { userId }, { name: "Jane Doe", email: "jane.doe@fixture.none" });
assertEquals(users.length, 1);
assertObjectMatch(users[0], {
@@ -44,16 +48,16 @@ describe("Relay", () => {
email: "jane.doe@fixture.none",
});
await relay.delete("/users/:userId", { userId });
await client.delete("/users/:userId", { userId });
assertEquals(users.length, 0);
});
it("should successfully run .actions", async () => {
assertEquals(await relay.get("/add-two", { a: 1, b: 1 }), 2);
assertEquals(await client.get("/add-two", { a: 1, b: 1 }), 2);
});
it("should reject .actions with error", async () => {
await assertRejects(() => relay.get("/add-two", { a: -1, b: 1 }), "Invalid input numbers added");
await assertRejects(() => client.get("/add-two", { a: -1, b: 1 }), "Invalid input numbers added");
});
});