-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.test.ts
More file actions
120 lines (100 loc) · 3.05 KB
/
Copy pathutils.test.ts
File metadata and controls
120 lines (100 loc) · 3.05 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { describe, it, expect } from "vitest";
import { validateName, wrapAction } from "./utils";
describe("validateName", () => {
it.each<[string, string]>([
["hello", "hello"],
[" hello ", "hello"],
[" spaced out ", "spaced out"],
])("validateName(%j) → %j", (input, expected) => {
expect(validateName(input)).toBe(expected);
});
it.each(["", " "])("throws for %j", (input) => {
expect(() => validateName(input)).toThrow("Name cannot be empty");
});
});
describe("wrapAction", () => {
it("maps Prisma unique constraint errors to user-friendly text", async () => {
const result = await wrapAction(async () => {
throw {
code: "P2002",
meta: { target: ["name"] },
};
});
expect(result).toEqual({
success: false,
error: "A record with this name already exists.",
});
});
it("maps Prisma not found errors to user-friendly text", async () => {
const result = await wrapAction(async () => {
throw { code: "P2025" };
});
expect(result).toEqual({
success: false,
error: "The requested record was not found.",
});
});
it("maps Prisma field too long errors", async () => {
const result = await wrapAction(async () => {
throw { code: "P2000" };
});
expect(result).toEqual({
success: false,
error: "A field contains a value that is too long.",
});
});
it("maps Prisma null constraint errors", async () => {
const result = await wrapAction(async () => {
throw { code: "P2011" };
});
expect(result).toEqual({
success: false,
error: "A required field is missing.",
});
});
it("maps Prisma relation violation on delete errors", async () => {
const result = await wrapAction(async () => {
throw { code: "P2014" };
});
expect(result).toEqual({
success: false,
error: "This record is referenced by other records and cannot be deleted.",
});
});
it("maps Prisma related record not found errors", async () => {
const result = await wrapAction(async () => {
throw { code: "P2015" };
});
expect(result).toEqual({
success: false,
error: "The related record could not be found.",
});
});
it("maps Prisma connection timeout errors", async () => {
const result = await wrapAction(async () => {
throw { code: "P2024" };
});
expect(result).toEqual({
success: false,
error: "Database connection timeout. Please try again.",
});
});
it("maps Prisma multiple errors", async () => {
const result = await wrapAction(async () => {
throw { code: "P2027" };
});
expect(result).toEqual({
success: false,
error: "Multiple database errors occurred. Please try again.",
});
});
it("falls back to generic error for unmapped Prisma codes", async () => {
const result = await wrapAction(async () => {
throw { code: "P9999" };
});
expect(result).toEqual({
success: false,
error: "A database operation failed. Please try again.",
});
});
});