chore: document and stabilize vitest browser setup
This commit is contained in:
parent
42942350dc
commit
eee0f432e7
12 changed files with 1238 additions and 325 deletions
|
|
@ -4,11 +4,17 @@ const mutationMock = vi.fn()
|
|||
const deleteManyMock = vi.fn()
|
||||
const assertAuthenticatedSession = vi.fn()
|
||||
|
||||
vi.mock("convex/browser", () => ({
|
||||
ConvexHttpClient: vi.fn().mockImplementation(() => ({
|
||||
mutation: mutationMock,
|
||||
})),
|
||||
}))
|
||||
vi.mock("convex/browser", () => {
|
||||
const ConvexHttpClient = vi.fn(function ConvexHttpClientMock() {
|
||||
return {
|
||||
mutation: mutationMock,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
ConvexHttpClient,
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock("@/lib/prisma", () => ({
|
||||
prisma: {
|
||||
|
|
@ -32,6 +38,12 @@ describe("POST /api/admin/machines/delete", () => {
|
|||
mutationMock.mockReset()
|
||||
deleteManyMock.mockReset()
|
||||
assertAuthenticatedSession.mockReset()
|
||||
mutationMock.mockImplementation(async (_ctx, payload) => {
|
||||
if (payload && typeof payload === "object" && "machineId" in payload) {
|
||||
return { ok: true }
|
||||
}
|
||||
return { _id: "user_123" }
|
||||
})
|
||||
assertAuthenticatedSession.mockResolvedValue({
|
||||
user: {
|
||||
email: "admin@example.com",
|
||||
|
|
@ -41,8 +53,7 @@ describe("POST /api/admin/machines/delete", () => {
|
|||
avatarUrl: null,
|
||||
},
|
||||
})
|
||||
mutationMock.mockResolvedValueOnce({ _id: "user_123" })
|
||||
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
|
||||
const consoleSpy = vi.spyOn(console, "error").mockImplementation(function noop() {})
|
||||
restoreConsole = () => consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
|
|
@ -52,7 +63,6 @@ describe("POST /api/admin/machines/delete", () => {
|
|||
})
|
||||
|
||||
it("returns ok when the machine removal succeeds", async () => {
|
||||
mutationMock.mockResolvedValueOnce({ ok: true })
|
||||
const { POST } = await import("./route")
|
||||
const response = await POST(
|
||||
new Request("http://localhost/api/admin/machines/delete", {
|
||||
|
|
@ -69,7 +79,12 @@ describe("POST /api/admin/machines/delete", () => {
|
|||
})
|
||||
|
||||
it("still succeeds when the Convex machine is already missing", async () => {
|
||||
mutationMock.mockRejectedValueOnce(new Error("Máquina não encontrada"))
|
||||
mutationMock.mockImplementation(async (_ctx, payload) => {
|
||||
if (payload && typeof payload === "object" && "machineId" in payload) {
|
||||
throw new Error("Máquina não encontrada")
|
||||
}
|
||||
return { _id: "user_123" }
|
||||
})
|
||||
const { POST } = await import("./route")
|
||||
const response = await POST(
|
||||
new Request("http://localhost/api/admin/machines/delete", {
|
||||
|
|
@ -84,7 +99,12 @@ describe("POST /api/admin/machines/delete", () => {
|
|||
})
|
||||
|
||||
it("returns an error for other Convex failures", async () => {
|
||||
mutationMock.mockRejectedValueOnce(new Error("timeout error"))
|
||||
mutationMock.mockImplementation(async (_ctx, payload) => {
|
||||
if (payload && typeof payload === "object" && "machineId" in payload) {
|
||||
throw new Error("timeout error")
|
||||
}
|
||||
return { _id: "user_123" }
|
||||
})
|
||||
const { POST } = await import("./route")
|
||||
const response = await POST(
|
||||
new Request("http://localhost/api/admin/machines/delete", {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue