forked from Arisamiga/Linkedin-RSS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss-parser.test.js
More file actions
101 lines (82 loc) · 2.85 KB
/
Copy pathrss-parser.test.js
File metadata and controls
101 lines (82 loc) · 2.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
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import RSSParser from "rss-parser";
import nock from "nock";
import { mockRssXml } from "./fixtures/mock-responses.js";
describe("RSS Feed Parsing", () => {
beforeEach(() => {
nock.cleanAll();
});
afterEach(() => {
nock.cleanAll();
});
it("should parse mocked RSS feed", async () => {
nock("https://example.com")
.get("/feed.xml")
.reply(200, mockRssXml, { "Content-Type": "application/xml" });
const parser = new RSSParser();
const feed = await parser.parseURL("https://example.com/feed.xml");
expect(feed.title).toBe("Posts on Home");
expect(feed.items.length).toBe(1);
expect(feed.items[0].title).toBe("KVM + Libvirt Setup Guide");
expect(feed.items[0].link).toBe(
"https://mrmcmuffinz.github.io/posts/kvm_libvirt_setup_guide/",
);
});
it("should handle empty RSS feed", async () => {
const emptyFeed = `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Empty Feed</title>
</channel>
</rss>`;
nock("https://example.com")
.get("/empty.xml")
.reply(200, emptyFeed, { "Content-Type": "application/xml" });
const parser = new RSSParser();
const feed = await parser.parseURL("https://example.com/empty.xml");
expect(feed.title).toBe("Empty Feed");
expect(feed.items.length).toBe(0);
});
it("should handle malformed XML", async () => {
nock("https://example.com")
.get("/bad.xml")
.reply(200, "Not valid XML", { "Content-Type": "text/plain" });
const parser = new RSSParser();
await expect(
parser.parseURL("https://example.com/bad.xml"),
).rejects.toThrow();
});
it("should handle feed with multiple items", async () => {
const multiItemFeed = `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<item>
<title>Post 1</title>
<link>https://example.com/post1</link>
</item>
<item>
<title>Post 2</title>
<link>https://example.com/post2</link>
</item>
<item>
<title>Post 3</title>
<link>https://example.com/post3</link>
</item>
</channel>
</rss>`;
nock("https://example.com").get("/multi.xml").reply(200, multiItemFeed);
const parser = new RSSParser();
const feed = await parser.parseURL("https://example.com/multi.xml");
expect(feed.items.length).toBe(3);
expect(feed.items[0].title).toBe("Post 1");
expect(feed.items[1].title).toBe("Post 2");
expect(feed.items[2].title).toBe("Post 3");
});
it("should parse feed with contentSnippet", async () => {
nock("https://example.com").get("/feed.xml").reply(200, mockRssXml);
const parser = new RSSParser();
const feed = await parser.parseURL("https://example.com/feed.xml");
expect(feed.items[0]).toHaveProperty("contentSnippet");
});
});