From 0613687c425853dbc6526e49bfca15ae85e60d56 Mon Sep 17 00:00:00 2001 From: martyanov-av Date: Thu, 14 Nov 2024 22:00:36 +0300 Subject: [PATCH] chore: add tests for includes --- test/includes.test.ts | 92 ++++++++++++++++++- .../mocks/include-outside-symlink.expect.html | 3 + test/mocks/include.expect.html | 4 + 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 test/mocks/include-outside-symlink.expect.html create mode 100644 test/mocks/include.expect.html diff --git a/test/includes.test.ts b/test/includes.test.ts index 51da7052..7b80a3c0 100644 --- a/test/includes.test.ts +++ b/test/includes.test.ts @@ -1,5 +1,8 @@ -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'; @@ -7,11 +10,77 @@ 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'); @@ -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); + }); }); diff --git a/test/mocks/include-outside-symlink.expect.html b/test/mocks/include-outside-symlink.expect.html new file mode 100644 index 00000000..48860679 --- /dev/null +++ b/test/mocks/include-outside-symlink.expect.html @@ -0,0 +1,3 @@ +

start main

+

{% include test %}

+

end main

diff --git a/test/mocks/include.expect.html b/test/mocks/include.expect.html new file mode 100644 index 00000000..d18b40a5 --- /dev/null +++ b/test/mocks/include.expect.html @@ -0,0 +1,4 @@ +

start main

+

Title

+

Content

+

end main