-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteLinkCategoryAction.test.ts
More file actions
54 lines (46 loc) · 1.92 KB
/
Copy pathdeleteLinkCategoryAction.test.ts
File metadata and controls
54 lines (46 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { beforeEach, describe, expect, it, vi } from "vitest"
import { mockUser } from "@/test/factories/user"
import { itIsGatedBy } from "@/test/gates"
import { authModule, cacheModule, dbModule, sentryModule } from "@/test/mocks"
const h = vi.hoisted(() => ({
getUser: vi.fn(),
deleteCategory: vi.fn(),
revalidatePath: vi.fn(),
captureActionError: vi.fn()
}))
vi.mock("@/helpers/db", () =>
dbModule({ linkCategory: { delete: h.deleteCategory } })
)
vi.mock("@/helpers/supabase/auth", () => authModule(h.getUser))
vi.mock("next/cache", () => cacheModule(h.revalidatePath))
vi.mock("@/lib/sentry", () => sentryModule(h.captureActionError))
import deleteLinkCategoryAction from "../deleteLinkCategoryAction"
beforeEach(() => {
h.getUser.mockResolvedValue(mockUser(["delete:lien"]))
h.deleteCategory.mockResolvedValue({ id: 1 })
})
describe("deleteLinkCategoryAction", () => {
itIsGatedBy({
action: () => deleteLinkCategoryAction(undefined, 1),
permission: "delete:lien",
getUser: h.getUser,
writes: [h.deleteCategory]
})
it("captures and fails when the delete throws", async () => {
h.deleteCategory.mockRejectedValue(new Error("db down"))
const res = await deleteLinkCategoryAction(undefined, 1)
expect(res).toEqual({
success: false,
error: "Echec de la suppression de la catégorie"
})
expect(h.captureActionError).toHaveBeenCalledOnce()
expect(h.revalidatePath).not.toHaveBeenCalled()
})
it("deletes the category and revalidates on the happy path", async () => {
const res = await deleteLinkCategoryAction(undefined, 9)
expect(res).toEqual({ success: true })
expect(h.deleteCategory).toHaveBeenCalledWith({ where: { id: 9 } })
expect(h.revalidatePath).toHaveBeenCalledWith("/dashboard/liens")
expect(h.revalidatePath).toHaveBeenCalledWith("/liens")
})
})