Skip to content

Commit

Permalink
chore: add tests for includes
Browse files Browse the repository at this point in the history
  • Loading branch information
martyanovandrey committed Nov 14, 2024
1 parent 8426e1f commit 0613687
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
92 changes: 91 additions & 1 deletion test/includes.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,86 @@
import {dirname} from 'path';
import {readFile, symlink, unlink} from 'node:fs/promises';
import {dirname, resolve} from 'path';
import dedent from 'ts-dedent';

import transform from '../src/transform';
import includes from '../src/transform/plugins/includes';
import yfmlint from '../src/transform/yfmlint';
import {log} from '../src/transform/log';

import {callPlugin, tokenize} from './utils';
import {codeInBackQuote, notitle, sharpedFile, title} from './data/includes';

const mocksPath = require.resolve('./mocks/link.md');
const symLinkPath = resolve(__dirname, './mocks/symlink.md');
const transformYfm = (text: string) => {
const {
result: {html},
} = transform(text, {
plugins: [includes],
path: mocksPath,
root: dirname(mocksPath),
});
return html;
};

describe('Includes', () => {
beforeEach(() => {
log.clear();
});

test('Simple include', async () => {
const expectPath = resolve(__dirname, './mocks/include.expect.html');
const expectContent = await readFile(expectPath, 'utf8');

const html = transformYfm(dedent`
start main
{% include [test](./include.md) %}
end main
`);

expect(html).toBe(expectContent);
});

test('Symlink include', async () => {
const expectPath = resolve(__dirname, './mocks/include.expect.html');
const expectContent = await readFile(expectPath, 'utf8');

await symlink(resolve(__dirname, './mocks/include.md'), symLinkPath);

const html = transformYfm(dedent`
start main
{% include [test](./symlink.md) %}
end main
`);

expect(html).toBe(expectContent);

await unlink(symLinkPath);
});

test('Include symlink outside root', async () => {
const expectPath = resolve(__dirname, './mocks/include-outside-symlink.expect.html');
const expectContent = await readFile(expectPath, 'utf8');

await symlink(resolve(__dirname, 'includes.test.ts'), symLinkPath);

const html = transformYfm(dedent`
start main
{% include [test](./symlink.md) %}
end main
`);

expect(html).toBe(expectContent);

await unlink(symLinkPath);
});

test('Should include with title', () => {
const mocksPath = require.resolve('./utils.ts');

Expand Down Expand Up @@ -152,4 +221,25 @@ describe('Includes', () => {

expect(result).toEqual(sharpedFile);
});

test('Should include with title', () => {
const mocksPath = require.resolve('./utils.ts');

const result = callPlugin(
includes,
tokenize([
'Text before include',
'',
'{% include [create-folder](./mocks/include.md) %}',
'',
'After include',
]),
{
path: mocksPath,
root: dirname(mocksPath),
},
);

expect(result).toEqual(title);
});
});
3 changes: 3 additions & 0 deletions test/mocks/include-outside-symlink.expect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>start main</p>
<p>{% include <a href="./symlink.md">test</a> %}</p>
<p>end main</p>
4 changes: 4 additions & 0 deletions test/mocks/include.expect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p>start main</p>
<h1>Title</h1>
<p>Content</p>
<p>end main</p>

0 comments on commit 0613687

Please sign in to comment.