Skip to content

Commit

Permalink
tests & tests & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jennspencer committed May 10, 2024
1 parent ec7af0d commit 175589e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 292 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion __tests__/browser/markdown.test.js
Expand Up @@ -18,7 +18,7 @@ describe('visual regression tests', () => {
//'features',
// 'headings',
'images',
// 'lists',
'htmlTests',
// 'tables',
// 'tablesTests',
'codeBlockTests',
Expand Down
@@ -1,49 +1,43 @@
import { render, screen } from '@testing-library/react';
import { render, screen, cleanup } from '@testing-library/react';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { vi } from 'vitest';

import createHTMLBlock from '../../components/HTMLBlock';
import HTMLBlock from '../../components/HTMLBlock';
import { compile, run } from '../../index';
import createSchema from '../../sanitize.schema';

const HTMLBlock = createHTMLBlock(createSchema(), {});

describe.skip('HTML Block', () => {
describe('HTML Block', () => {
beforeEach(() => {
global.window = true;
global.mockFn = vi.fn();
});

afterEach(() => {
cleanup();
vi.restoreAllMocks();
});

it('runs user scripts in compat mode', () => {
render(<HTMLBlock html="<script>mockFn()</script>" runScripts={true} />);
render(<HTMLBlock runScripts={true}>{`<script>mockFn()</script>`}</HTMLBlock>);
expect(global.mockFn).toHaveBeenCalledTimes(1);
});

it("doesn't run user scripts by default", () => {
render(<HTMLBlock html="<script>mockFn()</script>" runScripts={false} />);
render(<HTMLBlock>{`<script>mockFn()</script>`}</HTMLBlock>);
expect(global.mockFn).toHaveBeenCalledTimes(0);
});

it("doesn't render user scripts by default", () => {
render(<HTMLBlock html="<script>mockFn()</script>" runScripts={false} />);

render(<HTMLBlock>{`<script>mockFn()</script>`}</HTMLBlock>);
expect(screen.queryByText('mockFn()')).not.toBeInTheDocument();
});

it("doesn't render user scripts with weird endings", () => {
render(<HTMLBlock html="<script>mockFn()</script foo='bar'>" runScripts={false} />);

render(<HTMLBlock>{`<script>mockFn()</script foo='bar'>`}</HTMLBlock>);
expect(screen.queryByText('mockFn()')).not.toBeInTheDocument();
});

it("doesn't render user scripts with a malicious string", () => {
render(<HTMLBlock html="<scrip<script></script>t>mockFn()</s<script></script>cript>" runScripts={false} />);

render(<HTMLBlock>{`<scrip<script></script>t>mockFn()</s<script></script>cript>`}</HTMLBlock>);
expect(screen.queryByText('mockFn()')).not.toBeInTheDocument();
});

Expand All @@ -52,24 +46,19 @@ describe.skip('HTML Block', () => {
<h1>Hello World</h1>
<script>mockFn()</script>
`;
const elem = <HTMLBlock html={html} runScripts={true} />;
const elem = <HTMLBlock runScripts={true}>{html}</HTMLBlock>;
const view = renderToString(elem);
expect(elem.props.runScripts).toBe(true);
expect(view.indexOf('<script>')).toBeLessThan(0);
expect(view.indexOf('<h1>')).toBeGreaterThanOrEqual(0);
});

it('renders the html in a `<pre>` tag if safeMode={true}', () => {
const md = `
[block:html]
${JSON.stringify({
html: '<button onload="alert(\'gotcha!\')"/>',
})}
[/block]
`;

expect(renderToString(run(compile(md, { safeMode: true })))).toMatchInlineSnapshot(
'"<pre class=\\"html-unsafe\\" data-reactroot=\\"\\"><code>&lt;button onload=&quot;alert(&#39;gotcha!&#39;)&quot;/&gt;</code></pre>"'
it('renders the html in a `<pre>` tag if safeMode={true}', async () => {
const md = '<HTMLBlock safeMode={true}>{`<button onload="alert(\'gotcha!\')"/>`}</HTMLBlock>';
const code = compile(md);
const Component = await run(code);
expect(renderToString(<Component />)).toMatchInlineSnapshot(

Check failure on line 60 in __tests__/components/HTMLBlock.test.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 16)

__tests__/components/HTMLBlock.test.tsx > HTML Block > renders the html in a `<pre>` tag if safeMode={true}

Error: Snapshot `HTML Block > renders the html in a `<pre>` tag if safeMode={true} 1` mismatched - Expected + Received - "<pre class="html-unsafe"><code>&lt;button onload=&quot;alert(&#x27;gotcha!&#x27;)&quot;/&gt;</code></pre>" + "<pre class="html-unsafe" data-reactroot=""><code>&lt;button onload=&quot;alert(&#x27;gotcha!&#x27;)&quot;/&gt;</code></pre>" ❯ __tests__/components/HTMLBlock.test.tsx:60:43

Check failure on line 60 in __tests__/components/HTMLBlock.test.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 17)

__tests__/components/HTMLBlock.test.tsx > HTML Block > renders the html in a `<pre>` tag if safeMode={true}

Error: Snapshot `HTML Block > renders the html in a `<pre>` tag if safeMode={true} 1` mismatched - Expected + Received - "<pre class="html-unsafe"><code>&lt;button onload=&quot;alert(&#x27;gotcha!&#x27;)&quot;/&gt;</code></pre>" + "<pre class="html-unsafe" data-reactroot=""><code>&lt;button onload=&quot;alert(&#x27;gotcha!&#x27;)&quot;/&gt;</code></pre>" ❯ __tests__/components/HTMLBlock.test.tsx:60:43
'"<pre class="html-unsafe"><code>&lt;button onload=&quot;alert(&#x27;gotcha!&#x27;)&quot;/&gt;</code></pre>"',
);
});
});
13 changes: 0 additions & 13 deletions __tests__/components/__snapshots__/test.js.snap

This file was deleted.

230 changes: 0 additions & 230 deletions __tests__/components/test.js

This file was deleted.

9 changes: 3 additions & 6 deletions components/HTMLBlock/index.tsx
Expand Up @@ -13,12 +13,9 @@ const extractScripts = (html: string = ''): [string, () => void] => {
return [cleaned, () => scripts.map(js => window.eval(js))];
};

const HTMLBlock = props => {
const { children, runScripts, safeMode = false } = props;
const HTMLBlock = ({ children = '', runScripts = false, safeMode = false }) => {
let html = children;

if (typeof html !== 'string') html = renderToStaticMarkup(html);

const [cleanedHtml, exec] = extractScripts(html);

useEffect(() => {
Expand All @@ -28,12 +25,12 @@ const HTMLBlock = props => {
if (safeMode) {
return (
<pre className="html-unsafe">
<code>{cleanedHtml}</code>
<code>{html}</code>
</pre>
);
}

return <div className="rdmd-html" dangerouslySetInnerHTML={{ __html: html }} />;
return <div className="rdmd-html" dangerouslySetInnerHTML={{ __html: cleanedHtml }} />;
};

export default HTMLBlock;

0 comments on commit 175589e

Please sign in to comment.