Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions tests/routes/things/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
47 changes: 47 additions & 0 deletions tests/routes/things/delete.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
46 changes: 46 additions & 0 deletions tests/routes/things/list.test.ts
Original file line number Diff line number Diff line change
@@ -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],
},
])
})
Loading