This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawl.test.js
96 lines (79 loc) · 2.43 KB
/
crawl.test.js
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
import { JSDOM } from "jsdom"
import { test, expect } from "@jest/globals"
import { normalizeURL, getURLsFromHTML } from "./crawl.js"
describe("normalizeURL function", () => {
const testCases = [
// Basic cases
"https://blog.boot.dev/path/",
"https://blog.boot.dev/path",
"http://blog.boot.dev/path/",
"http://blog.boot.dev/path",
]
testCases.forEach((url) => {
test(`Should normalize ${url} to blog.boot.dev/path`, () => {
expect(normalizeURL(url)).toBe("blog.boot.dev/path")
})
})
// URL with only protocol and domain
test("Should normalize https://blog.boot.dev/ to blog.boot.dev", () => {
expect(normalizeURL("https://blog.boot.dev/")).toBe("blog.boot.dev")
})
test("Should normalize https://blog.boot.dev to blog.boot.dev", () => {
expect(normalizeURL("https://blog.boot.dev")).toBe("blog.boot.dev")
})
})
describe("getURLsFromHTML function", () => {
test("should add baseURL to relative links", () => {
const htmlBody = `
<html>
<body>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<a href="https://example.com">External link</a>
</body>
</html>
`
const baseURL = "http://www.example.com"
const expectedURLs = [
"http://www.example.com/about",
"http://www.example.com/contact",
"https://example.com"
]
const dom = new JSDOM(htmlBody)
const actualURLs = getURLsFromHTML(dom.serialize(), baseURL)
expect(actualURLs.length).toBe(3)
expect(actualURLs).toEqual(expect.arrayContaining(expectedURLs))
})
test("should handle absolute links without modification", () => {
const htmlBody = `
<html>
<body>
<a href="/about">About</a>
<a href="https://example.com">External link</a>
</body>
</html>
`
const baseURL = "http://www.example.com"
const expectedURLs = [
"http://www.example.com/about",
"https://example.com"
]
const dom = new JSDOM(htmlBody)
const actualURLs = getURLsFromHTML(dom.serialize(), baseURL)
expect(actualURLs.length).toBe(2)
expect(actualURLs).toEqual(expect.arrayContaining(expectedURLs))
})
test("should return an empty array for no links", () => {
const htmlBody = `
<html>
<body>
<p>No links in this HTML content</p>
</body>
</html>
`
const baseURL = "http://www.example.com"
const dom = new JSDOM(htmlBody)
const actualURLs = getURLsFromHTML(dom.serialize(), baseURL)
expect(actualURLs.length).toBe(0)
})
})