aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Effect/requestBody.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Effect/requestBody.test.ts')
-rw-r--r--src/lib/Effect/requestBody.test.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lib/Effect/requestBody.test.ts b/src/lib/Effect/requestBody.test.ts
new file mode 100644
index 00000000..8ca31036
--- /dev/null
+++ b/src/lib/Effect/requestBody.test.ts
@@ -0,0 +1,56 @@
+import { describe, expect, it } from "vitest";
+import { Schema } from "effect";
+import {
+ decodeRequestJsonOrThrow,
+ decodeUnknownOrThrow,
+} from "$lib/Effect/requestBody";
+
+describe("request body effect decoders", () => {
+ it("decodes unknown payload with a schema", () => {
+ const decoded = decodeUnknownOrThrow(Schema.Array(Schema.String), [
+ "a",
+ "b",
+ "c",
+ ]);
+
+ expect(decoded).toEqual(["a", "b", "c"]);
+ });
+
+ it("throws when payload does not match schema", () => {
+ expect(() =>
+ decodeUnknownOrThrow(Schema.Array(Schema.String), ["a", 2]),
+ ).toThrowError();
+ });
+
+ it("decodes request.json body with schema", async () => {
+ const request = new Request("https://due.moe/api/preferences", {
+ method: "PUT",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify(["seasonal", "ongoing"]),
+ });
+ const decoded = await decodeRequestJsonOrThrow(
+ request,
+ Schema.Array(Schema.String),
+ );
+
+ expect(decoded).toEqual(["seasonal", "ongoing"]);
+ });
+
+ it("decodes request.json object body with record schema", async () => {
+ const request = new Request("https://due.moe/api/configuration", {
+ method: "PUT",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({ theme: "dark", notifications: true }),
+ });
+ const decoded = await decodeRequestJsonOrThrow(
+ request,
+ Schema.Record(Schema.String, Schema.Unknown),
+ );
+
+ expect(decoded).toEqual({ theme: "dark", notifications: true });
+ });
+});