-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.test.ts
More file actions
51 lines (41 loc) · 1.6 KB
/
Copy pathutils.test.ts
File metadata and controls
51 lines (41 loc) · 1.6 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
// src/lib/image-hosting/__tests__/utils.test.ts
import { describe, expect, it } from "vitest"
import { safeImageUrl, validateImageUrl } from "@/lib/validators"
describe("validateImageUrl", () => {
it("returns the URL for a valid https URL", () => {
const url = "https://example.com/img.png"
expect(validateImageUrl(url)).toBe(url)
})
it("returns the URL for a valid http URL", () => {
const url = "http://example.com/img.png"
expect(validateImageUrl(url)).toBe(url)
})
it("throws for an ftp URL", () => {
expect(() => validateImageUrl("ftp://example.com/img.png")).toThrow(
"Invalid URL in image host response"
)
})
it("throws for a non-URL string", () => {
expect(() => validateImageUrl("not-a-url")).toThrow("Invalid URL in image host response")
})
it("throws for an empty string", () => {
expect(() => validateImageUrl("")).toThrow("Invalid URL in image host response")
})
})
describe("safeImageUrl", () => {
it("returns undefined for undefined input", () => {
expect(safeImageUrl(undefined)).toBeUndefined()
})
it("returns the URL for a valid https URL", () => {
expect(safeImageUrl("https://example.com/img.png")).toBe("https://example.com/img.png")
})
it("returns undefined for an ftp URL instead of throwing", () => {
expect(safeImageUrl("ftp://bad.com/x")).toBeUndefined()
})
it("returns undefined for a non-URL string instead of throwing", () => {
expect(safeImageUrl("not-a-url")).toBeUndefined()
})
it("returns undefined for an empty string instead of throwing", () => {
expect(safeImageUrl("")).toBeUndefined()
})
})