Skip to content

Commit

Permalink
[#98] Add tests for various node parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
allenkinzalow committed Mar 14, 2024
1 parent 3699c36 commit e234cba
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-hats-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"osrs-web-scraper": patch
---

Add tests for various node parsers.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`bold node Bold text should parse and render 1`] = `"'''test'''"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`summary node summary elements should parse and render 1`] = `
"{{Collapsed section|18|test}}
'''test'''{{Collapsed section end}}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`div node poll-box class should parse and render 1`] = `
"{{News Poll|1|If While Guthix Sleeps is added to the game, should we continue to explore the idea of offering the Rites of Balance as a rewards from the quest? This question is for developer consideration and not binding.}}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`font node Font node should parse and render a 2-level header 1`] = `"==test=="`;

exports[`font node Font node should parse and render a 3-level header 1`] = `"===test==="`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`italics node Italic text should parse and render 1`] = `"''test''"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`link node Links should parse and render 1`] = `"[link test]"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`list node list should parse and render 1`] = `
"
* test1
* test2
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`p node Paragraph should parse and render 1`] = `
"'''test'''
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`underline node Underlined text should parse and render 1`] = `"'''test'''"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import boldParser from "../bold";

describe("bold node", () => {
test("Bold text should parse and render", () => {
const root = parse("<b>test</b>");
const builder = new MediaWikiBuilder();
builder.addContents([boldParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import detailsParser from "../details";

describe("summary node", () => {
test("summary elements should parse and render", () => {
const root = parse("<details><summary>test</summary><b>test</b></details>");
const builder = new MediaWikiBuilder();
builder.addContents([detailsParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
15 changes: 15 additions & 0 deletions src/scrapers/news/sections/newsContent/nodes/__tests__/div.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import divParser from "../div";

describe("div node", () => {
test("poll-box class should parse and render", () => {
const root = parse(
"<div class='poll-box'><p>Question #X:</p><p><b>If While Guthix Sleeps is added to the game, should we continue to explore the idea of offering the Rites of Balance as a rewards from the quest? This question is for developer consideration and not binding.</b></p></div>"
);
const builder = new MediaWikiBuilder();
builder.addContents([divParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import fontParser from "../font";

describe("font node", () => {
test("Font node should parse and render a 2-level header", () => {
const root = parse(
'<font style="font-family:Cinzel,serif;font-size:22px;font-weight:bold;color:#FFFFFF;text-shadow:1px 1px #121212;">test</font>'
);
const builder = new MediaWikiBuilder();
builder.addContents([fontParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});

test("Font node should parse and render a 3-level header", () => {
const root = parse(
'<font style="font-size:18px;font-weight:bold;color:#000000;">test</font>'
);
const builder = new MediaWikiBuilder();
builder.addContents([fontParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import italicsParser from "../italics";

describe("italics node", () => {
test("Italic text should parse and render", () => {
const root = parse("<b>test</b>");
const builder = new MediaWikiBuilder();
builder.addContents([italicsParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import linkParser from "../link";

describe("link node", () => {
test("Links should parse and render", () => {
const root = parse("<a href='link'>test</a>");
const builder = new MediaWikiBuilder();
builder.addContents([linkParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import listParser from "../list";

describe("list node", () => {
test("list should parse and render", () => {
const root = parse("<ul><li>test1</li><li>test2</li></ul>");
const builder = new MediaWikiBuilder();
builder.addContents([listParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import paragraphParser from "../paragraph";

describe("p node", () => {
test("Paragraph should parse and render", () => {
const root = parse("<p><b>test</b></p>");
const builder = new MediaWikiBuilder();
builder.addContents([paragraphParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import boldParser from "../bold";

describe("underline node", () => {
test("Underlined text should parse and render", () => {
const root = parse("<u>test</u>");
const builder = new MediaWikiBuilder();
builder.addContents([boldParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions src/scrapers/news/sections/newsContent/nodes/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const listParser: ContentNodeParser = (node, options) => {
if (node instanceof HTMLElement) {
const list = node as HTMLElement;
const ordered = list.tagName === "ol";
const level = ((options.level as number) ?? 0) + 1;
const level = ((options?.level as number) ?? 0) + 1;
const content = node.childNodes
.filter((childNode) => childNode instanceof HTMLElement)
.map((childNode) => {
Expand All @@ -23,7 +23,7 @@ export const listParser: ContentNodeParser = (node, options) => {
return textParser(childNode, { ...options, ordered, level });
})
.flat();
if (!options.level) {
if (!options?.level) {
content.push(new MediaWikiBreak());
}
return content;
Expand Down

0 comments on commit e234cba

Please sign in to comment.