diff --git a/tests/routes/things/create.test.ts b/tests/routes/things/create.test.ts index 65e9935..f30bc31 100644 --- a/tests/routes/things/create.test.ts +++ b/tests/routes/things/create.test.ts @@ -3,17 +3,24 @@ import { test, expect } from "bun:test" test("create a thing", async () => { const { ky } = await getTestServer() + const thing = { + name: "Thing1", + description: "Thing1 Description", + } - ky.post("things/create", { - json: { - name: "Thing1", - description: "Thing1 Description", - }, - }) + const createData = await ky + .post("things/create", { + json: thing, + }) + .json<{ ok: boolean }>() + + expect(createData).toEqual({ ok: true }) const data = await ky .get("things/list") .json<{ things: { name: string; description: string }[] }>() expect(data.things).toHaveLength(1) + expect(data.things[0]?.name).toBe(thing.name) + expect(data.things[0]?.description).toBe(thing.description) }) diff --git a/tests/routes/things/delete.test.ts b/tests/routes/things/delete.test.ts new file mode 100644 index 0000000..d7056e2 --- /dev/null +++ b/tests/routes/things/delete.test.ts @@ -0,0 +1,47 @@ +import { expect, test } from "bun:test" +import { getTestServer } from "tests/fixtures/get-test-server" + +test("delete a thing by id and ignore missing ids", async () => { + const { ky } = await getTestServer() + + await ky + .post("things/create", { + json: { + name: "Thing1", + description: "Thing1 Description", + }, + }) + .json<{ ok: boolean }>() + + const beforeDelete = await ky.get("things/list").json<{ + things: { thing_id: string; name: string; description: string }[] + }>() + + expect(beforeDelete.things).toHaveLength(1) + + const deleteData = await ky + .post("things/delete", { + body: new URLSearchParams({ + thing_id: beforeDelete.things[0]!.thing_id, + }), + }) + .json<{ ok: boolean }>() + + expect(deleteData).toEqual({ ok: true }) + + const afterDelete = await ky.get("things/list").json<{ things: unknown[] }>() + expect(afterDelete.things).toHaveLength(0) + + const missingDeleteData = await ky + .post("things/delete", { + body: new URLSearchParams({ + thing_id: "missing", + }), + }) + .json<{ ok: boolean }>() + + expect(missingDeleteData).toEqual({ ok: true }) + + const data = await ky.get("things/list").json<{ things: unknown[] }>() + expect(data.things).toHaveLength(0) +}) diff --git a/tests/routes/things/list.test.ts b/tests/routes/things/list.test.ts new file mode 100644 index 0000000..66f1366 --- /dev/null +++ b/tests/routes/things/list.test.ts @@ -0,0 +1,46 @@ +import { expect, test } from "bun:test" +import { getTestServer } from "tests/fixtures/get-test-server" + +test("list starts empty and returns created things in insertion order", async () => { + const { ky } = await getTestServer() + + const emptyData = await ky.get("things/list").json<{ things: unknown[] }>() + + expect(emptyData.things).toEqual([]) + + const things = [ + { + name: "Thing1", + description: "Thing1 Description", + }, + { + name: "Thing2", + description: "Thing2 Description", + }, + ] + + for (const thing of things) { + const createData = await ky + .post("things/create", { + json: thing, + }) + .json<{ ok: boolean }>() + + expect(createData).toEqual({ ok: true }) + } + + const data = await ky.get("things/list").json<{ + things: { thing_id: string; name: string; description: string }[] + }>() + + expect(data.things).toEqual([ + { + thing_id: "0", + ...things[0], + }, + { + thing_id: "1", + ...things[1], + }, + ]) +})