add PGlite CRUD for user_votes with tests

This commit is contained in:
2026-03-10 16:52:11 +01:00
parent 8422e49e78
commit a4998190c3
2 changed files with 84 additions and 0 deletions
@@ -0,0 +1,42 @@
import { createTestDb } from "@/shared/db/client"
import type { PGlite } from "@electric-sql/pglite"
import { beforeEach, describe, expect, it } from "vitest"
let db: PGlite
beforeEach(async () => {
db = await createTestDb()
})
const { getUserVote, saveUserVote, deleteUserVote } = await import(
"./user-votes-db"
)
describe("user-votes-db", () => {
it("returns null when no vote exists", async () => {
const vote = await getUserVote(db, 1)
expect(vote).toBeNull()
})
it("saves and retrieves a vote", async () => {
await saveUserVote(db, 1, "ja")
const vote = await getUserVote(db, 1)
expect(vote).not.toBeNull()
expect(vote?.vote).toBe("ja")
expect(vote?.legislation_id).toBe(1)
})
it("overwrites an existing vote", async () => {
await saveUserVote(db, 1, "ja")
await saveUserVote(db, 1, "nein")
const vote = await getUserVote(db, 1)
expect(vote?.vote).toBe("nein")
})
it("deletes a vote", async () => {
await saveUserVote(db, 1, "ja")
await deleteUserVote(db, 1)
const vote = await getUserVote(db, 1)
expect(vote).toBeNull()
})
})