From 1efc518571ea19990916c87e65fb78187034aa2b 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 | 71 ++++++++++++++++++- .../mocks/include-outside-symlink.expect.html | 3 + test/mocks/include.expect.html | 4 ++ 3 files changed, 77 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..4698c679 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'); 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