Skip to content

Commit

Permalink
test: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yy4382 committed Mar 1, 2024
1 parent bf64c4e commit 20de5d4
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import type { Options } from "../lib/index.ts";
import { assert } from "chai";
import { rehype } from "rehype";

describe("Examples", () => {});
describe("Package", () => {
it('should expose the public api', () => {
assert.isFunction(rehypeExtendedLinks);
});
});

describe("Extended links Tests", () => {
describe("Link type check", () => {
// input: <a href="http://example.com">?</a>
// expected: <a href="http://example.com" rel="nofollow">?</a>
it("should not change a relative link", async () => {
Expand All @@ -15,6 +19,25 @@ describe("Extended links Tests", () => {
assert.equal(result, output);
});


it("should not change a fragment link", async () =>{
const input = '<a href="#example">fragment</a>'
const expected = '<a href="#example">fragment</a>'
const result = await process(input, {});
assert.equal(result, expected);
})
it("should not change a search link", async () =>{
const input = `<a href="?search">search</a>`
const expected = '<a href="?search">search</a>'
const result = await process(input, {});
assert.equal(result, expected);
})
it("should not change a mailto link", async () =>{
const input = '<a href="mailto:a@b.com">mailto</a>'
const expected = '<a href="mailto:a@b.com">mailto</a>'
const result = await process(input, {});
assert.equal(result, expected);
})
// input: <a href="//example.com">?</a>
// expected: <a href="//example.com" rel="nofollow">?</a>
it("should change a protocol-relative link", async () => {
Expand All @@ -23,7 +46,26 @@ describe("Extended links Tests", () => {
const output = `<a href="//example.com" rel="nofollow">?</a>`;
assert.equal(result, output);
});

it("should change a http link", async () =>{
const input = '<a href="http://example.com">http</a>'
const expected = '<a href="http://example.com" rel="nofollow">http</a>'
const result = await process(input, {});
assert.equal(result, expected);
})
it("should change a https link", async () =>{
const input = '<a href="https://example.com">https</a>'
const expected = '<a href="https://example.com" rel="nofollow">https</a>'
const result = await process(input, {});
assert.equal(result, expected);
})
it("should not change a www link", async () =>{
const input = '<a href="www.example.com">www</a>'
const expected = '<a href="www.example.com">www</a>'
const result = await process(input, {});
assert.equal(result, expected);
})
})
describe("Options check", () => {
it("should wrap in span because content exist", async () => {
const input = `<a href="//example.com">?</a>`;
const options: Options = {
Expand Down Expand Up @@ -86,19 +128,31 @@ describe("Dynamically generated tests", () => {
};
},
};
it("should add preContent", async () => {
it("should add preContent to a link that matches the test function", async () => {
const input = `<a href="https://github.com/abc">?</a>`;
const result = await process(input, dynPreContentOptions);
const output = `<span class="wrapped-link-container"><span><svg><use href="#mdi--github"></use></svg></span><a href="https://github.com/abc" class="wrapped-link" rel="nofollow">?</a></span>`;
assert.equal(result, output);
});
it("should not add preContent", async () => {
it("should not add preContent to a link that matches the test function", async () => {
const input = `<a href="https://example.com">?</a>`;
const result = await process(input, dynPreContentOptions);
const output = `<a href="https://example.com" rel="nofollow">?</a>`;
assert.equal(result, output);
});

it('should add rel to a link that matches the test function', async () => {
const input = '<a href="http://example.com">http</a>';
const options: Options = {
test: (node) => {
return node.properties.href === "http://example.com";
},
};
const output = '<a href="http://example.com" rel="nofollow">http</a>';
const result = await process(input, options);
assert.equal(result, output);
});

it("should not add rel to a link that does not match the test function", async () => {
const input = '<a href="http://example.com">http</a>';
const options: Options = {
Expand Down

0 comments on commit 20de5d4

Please sign in to comment.