Skip to content

Commit

Permalink
test: add plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v committed Nov 18, 2024
1 parent 7d64a3c commit fe2c3d1
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
testEnvironment: 'jsdom',
transformIgnorePatterns: [],
snapshotSerializers: ['jest-serializer-html'],
// snapshotSerializers: ['jest-serializer-html'],
transform: {
'^.+\\.(j|t)s?$': ['esbuild-jest', {tsconfig: './tsconfig.json'}],
},
Expand Down
166 changes: 166 additions & 0 deletions tests/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`File extension - plugin should add extra attrs 1`] = `
"<p><a href="../file" download="file.txt" class="yfm-file" data-yfm-file="yes"><span class="yfm-file__icon"></span>file.txt</a></p>
"
`;

exports[`File extension - plugin should add extra attrs by passing them to plugin options 1`] = `
"<p><a href="../file" download="file.txt" class="yfm-file" data-yfm="1" data-file="2"><span class="yfm-file__icon"></span>file.txt</a></p>
"
`;

exports[`File extension - plugin should allow quoutes in attribute value 1`] = `
"<p><a href="ind'ex.txt" download="ind&quot;ex.html" class="yfm-file"><span class="yfm-file__icon"></span>ind"ex.html</a></p>
"
`;

exports[`File extension - plugin should generate yfm-file token 1`] = `
[
Token {
"attrs": null,
"block": true,
"children": null,
"content": "",
"hidden": false,
"info": "",
"level": 0,
"map": [
0,
1,
],
"markup": "",
"meta": null,
"nesting": 1,
"tag": "p",
"type": "paragraph_open",
},
Token {
"attrs": null,
"block": true,
"children": [
Token {
"attrs": [
[
"href",
"../file",
],
[
"download",
"file.txt",
],
[
"class",
"yfm-file",
],
],
"block": false,
"children": null,
"content": "file.txt",
"hidden": false,
"info": "",
"level": 0,
"map": null,
"markup": "{% file ",
"meta": null,
"nesting": 0,
"tag": "",
"type": "yfm_file",
},
],
"content": "{% file src="../file" name="file.txt" %}",
"hidden": false,
"info": "",
"level": 1,
"map": [
0,
1,
],
"markup": "",
"meta": null,
"nesting": 0,
"tag": "",
"type": "inline",
},
Token {
"attrs": null,
"block": true,
"children": null,
"content": "",
"hidden": false,
"info": "",
"level": 0,
"map": null,
"markup": "",
"meta": null,
"nesting": -1,
"tag": "p",
"type": "paragraph_close",
},
]
`;

exports[`File extension - plugin should ignore additional file markup 1`] = `
"<p>{% file <a href="index.txt" download="index.html" class="yfm-file"><span class="yfm-file__icon"></span>index.html</a> %}</p>
"
`;
exports[`File extension - plugin should ignore additional special characters 1`] = `
"<p>{% <a href="index.txt" download="index.html" class="yfm-file"><span class="yfm-file__icon"></span>index.html</a> %}</p>
"
`;
exports[`File extension - plugin should ignore unknown attrs 1`] = `
"<p><a href="../file" download="file.txt" class="yfm-file"><span class="yfm-file__icon"></span>file.txt</a></p>
"
`;
exports[`File extension - plugin should map all specific file attrs to link html attrs 1`] = `
"<p><a href="../file2" download="file2.txt" hreflang="en" class="yfm-file"><span class="yfm-file__icon"></span>file2.txt</a></p>
"
`;
exports[`File extension - plugin should parse attrs with single quotes 1`] = `
"<p><a href="index.txt" download="index.html" class="yfm-file"><span class="yfm-file__icon"></span>index.html</a></p>
"
`;
exports[`File extension - plugin should pass allowed link html attrs 1`] = `
"<p><a href="../file1" download="file1.txt" referrerpolicy="origin" rel="help" target="_top" type="text/css" class="yfm-file"><span class="yfm-file__icon"></span>file1.txt</a></p>
"
`;
exports[`File extension - plugin should render file 1`] = `
"<p><a href="../file" download="file.txt" class="yfm-file"><span class="yfm-file__icon"></span>file.txt</a></p>
"
`;
exports[`File extension - plugin should render file between text 1`] = `
"<p>text1 <a href="../file" download="file.txt" class="yfm-file"><span class="yfm-file__icon"></span>file.txt</a> 2text</p>
"
`;
exports[`File extension - plugin should render file with different order of attrs 1`] = `
"<p><a type="text/html" download="page.html" href="../index.html" class="yfm-file"><span class="yfm-file__icon"></span>page.html</a></p>
"
`;
exports[`File extension - plugin should render file with extra spaces around attrs 1`] = `
"<p><a href="index.txt" download="index.html" type="text/html" class="yfm-file"><span class="yfm-file__icon"></span>index.html</a></p>
"
`;
exports[`File extension - plugin should render file with text after 1`] = `
"<p><a href="../file" download="file.txt" class="yfm-file"><span class="yfm-file__icon"></span>file.txt</a> don't download it</p>
"
`;
exports[`File extension - plugin should render file with text before 1`] = `
"<p>download it <a href="../file" download="file.txt" class="yfm-file"><span class="yfm-file__icon"></span>file.txt</a></p>
"
`;
exports[`File extension - plugin should render with file markup in attributes 1`] = `
"<p><a href="in%}dex.txt" download="{% file src='a' name='b' %}" class="yfm-file"><span class="yfm-file__icon"></span>{% file src='a' name='b' %}</a></p>
"
`;
179 changes: 176 additions & 3 deletions tests/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,178 @@
describe('test', () => {
it('should be test', () => {
expect(test).toBe('test');
import MarkdownIt from 'markdown-it';
import transform from '@diplodoc/transform';

import {type TransformOptions, transform as fileTransformer} from '../../src/plugin';

function html(markup: string, opts?: TransformOptions) {
return transform(markup, {
// override the default markdown-it-attrs delimiters,
// to make it easier to check html for non-valid file markup
leftDelimiter: '[',
rightDelimiter: ']',
plugins: [fileTransformer({bundle: false, ...opts})],
}).result.html;
}

function meta(markup: string, opts?: TransformOptions) {
return transform(markup, {
leftDelimiter: '[',
rightDelimiter: ']',
plugins: [fileTransformer({bundle: false, ...opts})],
}).result.meta;
}

function tokens(markup: string, opts?: TransformOptions) {
const md = new MarkdownIt().use(fileTransformer({bundle: false, ...opts}));
return md.parse(markup, {});
}

describe('File extension - plugin', () => {
it('should render file', () => {
expect(html('{% file src="../file" name="file.txt" %}')).toMatchSnapshot();
});

it('should ignore file markup without params', () => {
expect(html('{% file %}')).toBe('<p>{% file %}</p>\n');
});

it('should not render file without all required attrs', () => {
expect(html('{% file src="../file" %}')).toBe('<p>{% file src="../file" %}</p>\n');
expect(html('{% file name="file.txt" %}')).toBe('<p>{% file name="file.txt" %}</p>\n');
});

it('should render file with text before', () => {
expect(html('download it {% file src="../file" name="file.txt" %}')).toMatchSnapshot();
});

it('should render file with text after', () => {
expect(
html('{% file src="../file" name="file.txt" %} don\'t download it'),
).toMatchSnapshot();
});

it('should render file between text', () => {
expect(html('text1 {% file src="../file" name="file.txt" %} 2text')).toMatchSnapshot();
});

it('should map all specific file attrs to link html attrs', () => {
expect(html('{% file src="../file2" name="file2.txt" lang="en" %}')).toMatchSnapshot();
});

it('should pass allowed link html attrs', () => {
expect(
html(
'{% file src="../file1" name="file1.txt" referrerpolicy="origin" rel="help" target="_top" type="text/css" %}',
),
).toMatchSnapshot();
});

it('should ignore unknown attrs', () => {
expect(
html('{% file src="../file" name="file.txt" foo="1" bar="2" baz="3" %}'),
).toMatchSnapshot();
});

it('should add extra attrs', () => {
expect(
html('{% file src="../file" name="file.txt" %}', {
extraAttrs: [['data-yfm-file', 'yes']],
}),
).toMatchSnapshot();
});

it('should parse attrs with single quotes', () => {
expect(html("{% file src='index.txt' name='index.html' %}")).toMatchSnapshot();
});

it('should render with file markup in attributes', () => {
expect(
html('{% file src="in%}dex.txt" name="{% file src=\'a\' name=\'b\' %}" %}'),
).toMatchSnapshot();
});

it('should render file with different order of attrs', () => {
expect(
html('{% file type="text/html" name="page.html" src="../index.html" %}'),
).toMatchSnapshot();
});

it('should ignore additional special characters', () => {
expect(html('{% {% file src="index.txt" name="index.html" %} %}')).toMatchSnapshot();
});

it('should ignore additional file markup', () => {
expect(html('{% file {% file src="index.txt" name="index.html" %} %}')).toMatchSnapshot();
});

it('should allow quoutes in attribute value', () => {
expect(html('{% file src="ind\'ex.txt" name=\'ind"ex.html\' %}')).toMatchSnapshot();
});

it('should render file with extra spaces around attrs', () => {
expect(
html('{% file src="index.txt" name="index.html" type="text/html" %}'),
).toMatchSnapshot();
});

it('should not render file without spaces around attrs', () => {
expect(html('{% file src="index.txt"name="index.html"type="text/html" %}')).toBe(
`<p>{% file src="index.txt"name="index.html"type="text/html" %}</p>\n`,
);
});

it('should not render file with no spaces near borders', () => {
expect(html("{%file src='index.txt' name='index.html'%}")).toBe(
`<p>{%file src='index.txt' name='index.html'%}</p>\n`,
);
});

it('should not add meta if file not parsed', () => {
expect(meta('parapraph')).toBeUndefined();
});

it('should add default style assets to meta', () => {
expect(meta('{% file src="../file" name="file.txt" %}')).toStrictEqual({
style: ['_assets/file-extension.css'],
});
});

it('should override style assets name', () => {
expect(
meta('{% file src="../file" name="file.txt" %}', {runtime: 'file-ext'}),
).toStrictEqual({style: ['file-ext']});
});

it('should override style assets name 2', () => {
expect(
meta('{% file src="../file" name="file.txt" %}', {runtime: {style: 'yfm-file'}}),
).toStrictEqual({style: ['yfm-file']});
});

it('should not call onBundle', () => {
const onBundle = jest.fn(() => {});
html('text', {bundle: true, onBundle});
expect(onBundle).not.toHaveBeenCalled();
});

it('should call onBundle', () => {
const onBundle = jest.fn(() => {});
html('{% file src="../file" name="file.txt" %}', {bundle: true, onBundle});
expect(onBundle).toHaveBeenCalled();
});

it('should generate yfm-file token', () => {
expect(tokens('{% file src="../file" name="file.txt" %}')).toMatchSnapshot();
});

it('should add extra attrs by passing them to plugin options', () => {
const md = new MarkdownIt();
const filePlugin = fileTransformer({bundle: false});
filePlugin(md, {
fileExtraAttrs: [
['data-yfm', '1'],
['data-file', '2'],
],
});
expect(md.render('{% file src="../file" name="file.txt" %}')).toMatchSnapshot();
});
});

0 comments on commit fe2c3d1

Please sign in to comment.