forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.test.ts
More file actions
103 lines (94 loc) · 2.67 KB
/
errors.test.ts
File metadata and controls
103 lines (94 loc) · 2.67 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
import { mockApiError } from "testHelpers/entities";
import {
getValidationErrorMessage,
isApiError,
mapApiErrorToFieldErrors,
getErrorMessage,
} from "./errors";
describe("isApiError", () => {
it("returns true when the object is an API Error", () => {
expect(
isApiError(
mockApiError({
message: "Invalid entry",
validations: [
{ detail: "Username is already in use", field: "username" },
],
}),
),
).toBe(true);
});
it("returns false when the object is Error", () => {
expect(isApiError(new Error())).toBe(false);
});
it("returns false when the object is undefined", () => {
expect(isApiError(undefined)).toBe(false);
});
});
describe("mapApiErrorToFieldErrors", () => {
it("returns correct field errors", () => {
expect(
mapApiErrorToFieldErrors({
message: "Invalid entry",
validations: [
{ detail: "Username is already in use", field: "username" },
],
}),
).toEqual({
username: "Username is already in use",
});
});
});
describe("getValidationErrorMessage", () => {
it("returns multiple validation messages", () => {
expect(
getValidationErrorMessage(
mockApiError({
message: "Invalid user search query.",
validations: [
{
field: "status",
detail: `Query param "status" has invalid value: "inactive" is not a valid user status`,
},
{
field: "q",
detail: `Query element "role:a:e" can only contain 1 ':'`,
},
],
}),
),
).toEqual(
`Query param "status" has invalid value: "inactive" is not a valid user status\nQuery element "role:a:e" can only contain 1 ':'`,
);
});
it("non-API error returns empty validation message", () => {
expect(
getValidationErrorMessage(new Error("Invalid user search query.")),
).toEqual("");
});
it("no validations field returns empty validation message", () => {
expect(
getValidationErrorMessage(
mockApiError({
message: "Invalid user search query.",
detail: `Query element "role:a:e" can only contain 1 ':'`,
}),
),
).toEqual("");
});
it("returns default message for error that is empty string", () => {
expect(getErrorMessage("", "Something went wrong.")).toBe(
"Something went wrong.",
);
});
it("returns default message for 404 API response", () => {
expect(
getErrorMessage(
mockApiError({
message: "",
}),
"Something went wrong.",
),
).toBe("Something went wrong.");
});
});