forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-helpers.test.ts
More file actions
111 lines (95 loc) · 3.85 KB
/
api-helpers.test.ts
File metadata and controls
111 lines (95 loc) · 3.85 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
// src/lib/__tests__/api-helpers.test.ts
import { describe, expect, it } from "vitest"
import { validateIntRange, validateMaxLength } from "@/lib/api-helpers"
// ---------------------------------------------------------------------------
// validateIntRange
// ---------------------------------------------------------------------------
describe("validateIntRange", () => {
describe("valid inputs (returns null)", () => {
it("should return null for a value within range", () => {
const result = validateIntRange(5, 1, 10, "Field")
expect(result).toBeNull()
})
it("should return null for value at minimum boundary", () => {
const result = validateIntRange(1, 1, 10, "Field")
expect(result).toBeNull()
})
it("should return null for value at maximum boundary", () => {
const result = validateIntRange(10, 1, 10, "Field")
expect(result).toBeNull()
})
})
describe("invalid inputs (returns NextResponse)", () => {
it("should return error response for value below minimum", async () => {
const result = validateIntRange(0, 1, 10, "Field")
expect(result).not.toBeNull()
if (!result) return
expect(result.status).toBe(400)
const data = await result.json()
expect(data.error).toContain("between 1 and 10")
})
it("should return error response for value above maximum", async () => {
const result = validateIntRange(11, 1, 10, "Field")
expect(result).not.toBeNull()
if (!result) return
expect(result.status).toBe(400)
const data = await result.json()
expect(data.error).toContain("between 1 and 10")
})
it("should return error response for a float (non-integer)", async () => {
const result = validateIntRange(1.5, 1, 10, "Field")
expect(result).not.toBeNull()
if (!result) return
expect(result.status).toBe(400)
})
it("should return error response for NaN", async () => {
const result = validateIntRange(NaN, 1, 10, "Field")
expect(result).not.toBeNull()
if (!result) return
expect(result.status).toBe(400)
})
it("should include the label in the error message", async () => {
const result = validateIntRange(0, 1, 10, "PollInterval")
expect(result).not.toBeNull()
if (!result) return
const data = await result.json()
expect(data.error).toContain("PollInterval")
})
})
})
// ---------------------------------------------------------------------------
// validateMaxLength
// ---------------------------------------------------------------------------
describe("validateMaxLength", () => {
describe("valid inputs (returns null)", () => {
it("should return null for a string under the limit", () => {
const result = validateMaxLength("hello", 10, "Name")
expect(result).toBeNull()
})
it("should return null for a string exactly at the limit", () => {
const result = validateMaxLength("1234567890", 10, "Name")
expect(result).toBeNull()
})
it("should return null for an empty string", () => {
const result = validateMaxLength("", 10, "Name")
expect(result).toBeNull()
})
})
describe("invalid inputs (returns NextResponse)", () => {
it("should return error response for a string exceeding the limit", async () => {
const result = validateMaxLength("12345678901", 10, "Name")
expect(result).not.toBeNull()
if (!result) return
expect(result.status).toBe(400)
const data = await result.json()
expect(data.error).toContain("10 characters or fewer")
})
it("should include the label in the error message", async () => {
const result = validateMaxLength("this string is way too long", 10, "TrackerName")
expect(result).not.toBeNull()
if (!result) return
const data = await result.json()
expect(data.error).toContain("TrackerName")
})
})
})