hornwitser.no/utils/resolve-refs.test.tsx
Hornwitser ef8aaa6f6d Ignore absolute URIs when resolving refs
Do not output bogus warnings in the case of links that goes to other
websites.
2025-01-25 08:52:08 +01:00

57 lines
1.9 KiB
TypeScript

import assert from "node:assert/strict";
import { suite, test } from "node:test";
import { resolveRefs } from "./resolve-refs.js";
import type { Element } from "antihtml";
suite("function resolveRefs", () => {
test("root to root relative href", () => {
const el = resolveRefs(<a href="/page.html">Link</a>, "/");
assert.equal((el as Element).attributes.get("href"), "page.html");
});
test("root to subdir relative href", () => {
const el = resolveRefs(<a href="/dir/page.html">Link</a>, "/");
assert.equal((el as Element).attributes.get("href"), "dir/page.html");
});
test("subdir to root relative href", () => {
const el = resolveRefs(<a href="/page.html">Link</a>, "/subdir");
assert.equal((el as Element).attributes.get("href"), "../page.html");
});
test("subdir to subdir relative href", () => {
const el = resolveRefs(<a href="/alt/page.html">Link</a>, "/subdir");
assert.equal((el as Element).attributes.get("href"), "../alt/page.html");
});
test("nested element", () => {
const el = resolveRefs(<div>Content with <a href="/page.html">Link</a></div>, "/");
assert.equal((el.childNodes[1] as Element).attributes.get("href"), "page.html");
});
test("returns element if no changes", () => {
const el = <div>Content with <em>emphasis</em></div>;
const resEl = resolveRefs(el, "/");
assert.equal(el, resEl);
});
test("returns new element if changed", () => {
const el = <div>Content with <a href="/page.html">Link</a></div>;
const resEl = resolveRefs(el, "/");
assert.notEqual(el, resEl);
});
test("does not modify input", () => {
const elFn = () => <div>Content with <a href="/page.html">Link</a></div>;
const el = elFn();
resolveRefs(el, "/");
assert.deepEqual(el, elFn());
});
test("ignores absolute URIs", () => {
const el = <div>Content with <a href="https://example.org/page.html">Link</a></div>;
const resEl = resolveRefs(el, "/");
assert.equal(el, resEl);
});
});