Skip to content

Commit

Permalink
chore: increase test coverage (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
deer committed Jan 15, 2024
1 parent b8c429c commit 6b01882
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
7 changes: 5 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"tasks": {
"build": "deno run --allow-read --allow-write --allow-net --allow-run --allow-env ./style/patch.ts && deno fmt",
"check:types": "deno check **/*.ts",
"coverage": "deno test --allow-read --coverage=cov_profile",
"dev": "deno run -A --unstable --watch --no-check ./example/main.ts",
"test": "deno test --allow-read",
"coverage": "deno test --allow-read --coverage=cov_profile"
"ok": "deno fmt --check && deno lint && deno task check:types && deno task test",
"report": "deno coverage cov_profile --html",
"test": "deno test --allow-read"
},
"fmt": {
"exclude": ["./test/fixtures/alerts.md"]
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/codeMath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mo>=</mo><mi>m</mi><mi>x</mi><mo>+</mo><mi>b</mi></mrow><annotation encoding="application/x-tex">y = mx + b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em"></span><span class="mathnormal" style="margin-right:0.03588em">y</span><span class="mspace" style="margin-right:0.2778em"></span><span>=</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em"></span><span class="mathnormal">m</span><span class="mathnormal">x</span><span class="mspace" style="margin-right:0.2222em"></span><span>+</span><span class="mspace" style="margin-right:0.2222em"></span></span><span class="base"><span class="strut" style="height:0.6944em"></span><span class="mathnormal">b</span></span></span></span></span>
106 changes: 104 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals } from "https://deno.land/std@0.178.0/testing/asserts.ts";
import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.35-alpha/deno-dom-wasm.ts";
import { assertEquals } from "https://deno.land/std@0.211.0/assert/assert_equals.ts";
import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.43/deno-dom-wasm.ts";
import { render, Renderer } from "../mod.ts";

Deno.test("Basic markdown", async () => {
Expand Down Expand Up @@ -106,3 +106,105 @@ Deno.test(
assertEquals(html, expected);
},
);

Deno.test("Iframe rendering", () => {
const markdown =
'Here is an iframe:\n\n<iframe src="https://example.com" width="300" height="200"></iframe>';
const expected =
`<p>Here is an iframe:</p>\n<iframe src="https://example.com" width="300" height="200"></iframe>`;

const html = render(markdown, { allowIframes: true });
assertEquals(html, expected);
});

Deno.test("Iframe rendering disabled", () => {
const markdown =
'Here is an iframe:\n\n<iframe src="https://example.com" width="300" height="200"></iframe>';
const expectedWithoutIframe = `<p>Here is an iframe:</p>\n`;

const html = render(markdown);
assertEquals(html, expectedWithoutIframe);
});

Deno.test("Media URL transformation", () => {
const markdown = "![Image](image.jpg)\n\n![Video](video.mp4)";
const mediaBaseUrl = "https://cdn.example.com/";
const expected =
`<p><img src="https://cdn.example.com/image.jpg" alt="Image" /></p>\n<p><img src="https://cdn.example.com/video.mp4" alt="Video" /></p>\n`;

const html = render(markdown, { mediaBaseUrl: mediaBaseUrl });
assertEquals(html, expected);
});

Deno.test("Media URL transformation without base URL", () => {
const markdown = "![Image](image.jpg)\n\n![Video](video.mp4)";
const expectedWithoutTransformation =
`<p><img src="image.jpg" alt="Image" /></p>\n<p><img src="video.mp4" alt="Video" /></p>\n`;

const html = render(markdown);
assertEquals(html, expectedWithoutTransformation);
});

Deno.test("Media URL transformation with invalid URL", () => {
const markdown = "![Image](invalid-url)";
const mediaBaseUrl = "this is an invalid url";
const expected = `<p><img alt="Image" /></p>\n`;

const html = render(markdown, { mediaBaseUrl: mediaBaseUrl });
assertEquals(html, expected);
});

Deno.test("Inline rendering", () => {
const markdown = "My [Deno](https://deno.land) Blog";
const expected =
`My <a href="https://deno.land" rel="noopener noreferrer">Deno</a> Blog`;

const html = render(markdown, { inline: true });
assertEquals(html, expected);
});

Deno.test("Inline rendering false", () => {
const markdown = "My [Deno](https://deno.land) Blog";
const expected =
`<p>My <a href="https://deno.land" rel="noopener noreferrer">Deno</a> Blog</p>\n`;

const html = render(markdown, { inline: false });
assertEquals(html, expected);
});

Deno.test("Link URL resolution with base URL", () => {
const markdown = "[Test Link](/path/to/resource)";
const baseUrl = "https://example.com/";
const expected =
`<p><a href="https://example.com/path/to/resource" rel="noopener noreferrer">Test Link</a></p>\n`;

const html = render(markdown, { baseUrl: baseUrl });
assertEquals(html, expected);
});

Deno.test("Link URL resolution without base URL", () => {
const markdown = "[Test Link](/path/to/resource)";
const expected =
`<p><a href="/path/to/resource" rel="noopener noreferrer">Test Link</a></p>\n`;

const html = render(markdown);
assertEquals(html, expected);
});

Deno.test("Link URL resolution with invalid URL and base URL", () => {
const markdown = "[Test Link](/path/to/resource)";
const baseUrl = "this is an invalid url";
const expected =
`<p><a href="/path/to/resource" rel="noopener noreferrer">Test Link</a></p>\n`;

const html = render(markdown, { baseUrl: baseUrl });
assertEquals(html, expected);
});

Deno.test("Math rendering in code block", () => {
const markdown = "```math\ny = mx + b\n```";
const expected = Deno.readTextFileSync("./test/fixtures/codeMath.html");

const html = render(markdown, { allowMath: true });
assertEquals(html, expected);
});

0 comments on commit 6b01882

Please sign in to comment.