Skip to content

Commit

Permalink
test: api
Browse files Browse the repository at this point in the history
  • Loading branch information
nyaomaru committed Sep 29, 2024
1 parent 110fe2d commit 085f49d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/utils/api_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as modAssertEqual from "https://deno.land/std@0.224.0/assert/assert_equals.ts";
import * as modAssertThrows from "https://deno.land/std@0.224.0/assert/assert_throws.ts";
import { getUserKey, handleUserNotFound, statusCheck } from "#src/utils/api.ts";

Deno.test("statusCheck", async (t) => {
await t.step("#1 throws error when status is falsy", () => {
modAssertThrows.assertThrows(
() => statusCheck(null as unknown as Deno.KvCommitResult),
Error,
"Something went wrong.",
);
});

await t.step("#2 does not throw when status is truthy", () => {
const mockStatus = {} as Deno.KvCommitResult;
statusCheck(mockStatus);
});
});

Deno.test("getUserKey returns correct user key", () => {
const id = "123";
const expectedKey = ["user", "123"];
modAssertEqual.assertEquals(getUserKey(id), expectedKey);
});

Deno.test("handleUserNotFound returns correct response", async () => {
const id = "123";
const response = handleUserNotFound(id);
modAssertEqual.assertEquals(response.status, 200);

const bodyText = await response.text();
modAssertEqual.assertEquals(bodyText, `No user with id ${id} found`);
});

0 comments on commit 085f49d

Please sign in to comment.