From be9333271d4fd43794ab524ef9f6bab3af77ead7 Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Fri, 18 Oct 2024 12:52:15 +0800 Subject: [PATCH] test: update unit test (#294) --- .../__snapshots__/codeTabsPlugin.spec.ts.snap | 136 +++--- .../__snapshots__/npmToPlugin.spec.ts.snap | 438 ++++++++--------- .../__test__/codeTabsPlugin.spec.ts | 7 +- .../__test__/npmToPlugin.spec.ts | 11 +- .../collapsedLinesPlugin.spec.ts.snap | 456 ++++++++++++++++++ .../__test__/collapsedLinesPlugin.spec.ts | 68 +++ .../__test__/copyCodeButton.spec.ts | 49 ++ .../createCopyCodeButtonRender.ts | 9 +- .../src/node/markdown/collapsedLinesPlugin.ts | 2 +- 9 files changed, 877 insertions(+), 299 deletions(-) create mode 100644 plugins/plugin-shikiji/__test__/__snapshots__/collapsedLinesPlugin.spec.ts.snap create mode 100644 plugins/plugin-shikiji/__test__/collapsedLinesPlugin.spec.ts create mode 100644 plugins/plugin-shikiji/__test__/copyCodeButton.spec.ts diff --git a/plugins/plugin-md-power/__test__/__snapshots__/codeTabsPlugin.spec.ts.snap b/plugins/plugin-md-power/__test__/__snapshots__/codeTabsPlugin.spec.ts.snap index fb07abcd..a5587ae4 100644 --- a/plugins/plugin-md-power/__test__/__snapshots__/codeTabsPlugin.spec.ts.snap +++ b/plugins/plugin-md-power/__test__/__snapshots__/codeTabsPlugin.spec.ts.snap @@ -1,81 +1,81 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`codeTabsPlugin > should work with default 1`] = ` -"" +"" `; exports[`codeTabsPlugin > should work with no icon 1`] = ` -"" +"" `; exports[`codeTabsPlugin > should work with options: \`{ named: false, extensions: false }\` 1`] = ` -"" +"" `; exports[`codeTabsPlugin > should work with options: { named: [npm,pnpm,yarn], extensions: [.js,.ts] } 1`] = ` -"" +"" `; diff --git a/plugins/plugin-md-power/__test__/__snapshots__/npmToPlugin.spec.ts.snap b/plugins/plugin-md-power/__test__/__snapshots__/npmToPlugin.spec.ts.snap index 33523420..f4c12f1b 100644 --- a/plugins/plugin-md-power/__test__/__snapshots__/npmToPlugin.spec.ts.snap +++ b/plugins/plugin-md-power/__test__/__snapshots__/npmToPlugin.spec.ts.snap @@ -1,361 +1,361 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`npmToPlugin > should work width options: [npm, yarn, pnpm] 1`] = ` -"

anything

+
+
" `; exports[`npmToPlugin > should work width options: { tabs: [npm, yarn, pnpm] } 1`] = ` -"

anything

+
+
" `; exports[`npmToPlugin > should work with default options 1`] = ` -"

anything

+
+
" `; diff --git a/plugins/plugin-md-power/__test__/codeTabsPlugin.spec.ts b/plugins/plugin-md-power/__test__/codeTabsPlugin.spec.ts index d05caf54..f86d72bf 100644 --- a/plugins/plugin-md-power/__test__/codeTabsPlugin.spec.ts +++ b/plugins/plugin-md-power/__test__/codeTabsPlugin.spec.ts @@ -5,9 +5,10 @@ import { codeTabs } from '../src/node/container/codeTabs.js' function createMarkdown(options?: CodeTabsOptions) { const md = new MarkdownIt() - md.options.highlight = (str, lang) => { - return `
${str}
` - } + md.options.highlight = str => `
${str}
` + const fence = md.renderer.rules.fence! + md.renderer.rules.fence = (...args) => `
${fence(...args)}
` + md.use(codeTabs, options) return md } diff --git a/plugins/plugin-md-power/__test__/npmToPlugin.spec.ts b/plugins/plugin-md-power/__test__/npmToPlugin.spec.ts index 9d64b42b..5be2840e 100644 --- a/plugins/plugin-md-power/__test__/npmToPlugin.spec.ts +++ b/plugins/plugin-md-power/__test__/npmToPlugin.spec.ts @@ -5,9 +5,14 @@ import { codeTabs } from '../src/node/container/codeTabs.js' import { npmToPlugins, parseLine } from '../src/node/container/npmTo.js' function createMarkdown(options?: NpmToOptions) { - return new MarkdownIt({ - highlight: (str, lang) => `
${str}
`, - }).use(codeTabs).use(npmToPlugins, options) + const md = new MarkdownIt() + md.options.highlight = str => `
${str}
` + const fence = md.renderer.rules.fence! + md.renderer.rules.fence = (...args) => `
${fence(...args)}
` + + md.use(codeTabs).use(npmToPlugins, options) + + return md } const FENCE = '```' diff --git a/plugins/plugin-shikiji/__test__/__snapshots__/collapsedLinesPlugin.spec.ts.snap b/plugins/plugin-shikiji/__test__/__snapshots__/collapsedLinesPlugin.spec.ts.snap new file mode 100644 index 00000000..cc26df76 --- /dev/null +++ b/plugins/plugin-shikiji/__test__/__snapshots__/collapsedLinesPlugin.spec.ts.snap @@ -0,0 +1,456 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`collapsedLinesPlugin > should not work with false 1`] = ` +"
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+
+
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+const line11 = 11
+const line12 = 12
+const line13 = 13
+const line14 = 14
+const line15 = 15
+const line16 = 16
+const line17 = 17
+const line18 = 18
+const line19 = 19
+const line20 = 20
+
+
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+const line11 = 11
+const line12 = 12
+const line13 = 13
+const line14 = 14
+const line15 = 15
+const line16 = 16
+const line17 = 17
+const line18 = 18
+const line19 = 19
+const line20 = 20
+
+
" +`; + +exports[`collapsedLinesPlugin > should not work with includes styles 1`] = ` +"
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+
+
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+const line11 = 11
+const line12 = 12
+const line13 = 13
+const line14 = 14
+const line15 = 15
+const line16 = 16
+const line17 = 17
+const line18 = 18
+const line19 = 19
+const line20 = 20
+
+
" +`; + +exports[`collapsedLinesPlugin > should work with default 1`] = ` +"
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+
+
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+const line11 = 11
+const line12 = 12
+const line13 = 13
+const line14 = 14
+const line15 = 15
+const line16 = 16
+const line17 = 17
+const line18 = 18
+const line19 = 19
+const line20 = 20
+
+
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+const line11 = 11
+const line12 = 12
+const line13 = 13
+const line14 = 14
+const line15 = 15
+const line16 = 16
+const line17 = 17
+const line18 = 18
+const line19 = 19
+const line20 = 20
+
+
" +`; + +exports[`collapsedLinesPlugin > should work with number 1`] = ` +"
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+const line11 = 11
+const line12 = 12
+const line13 = 13
+const line14 = 14
+const line15 = 15
+const line16 = 16
+const line17 = 17
+const line18 = 18
+const line19 = 19
+const line20 = 20
+
+
" +`; + +exports[`collapsedLinesPlugin > should work with true 1`] = ` +"
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+
+
const line1 = 1
+const line2 = 2
+const line3 = 3
+const line4 = 4
+const line5 = 5
+const line6 = 6
+const line7 = 7
+const line8 = 8
+const line9 = 9
+const line10 = 10
+const line11 = 11
+const line12 = 12
+const line13 = 13
+const line14 = 14
+const line15 = 15
+const line16 = 16
+const line17 = 17
+const line18 = 18
+const line19 = 19
+const line20 = 20
+
+
" +`; diff --git a/plugins/plugin-shikiji/__test__/collapsedLinesPlugin.spec.ts b/plugins/plugin-shikiji/__test__/collapsedLinesPlugin.spec.ts new file mode 100644 index 00000000..f66a56bd --- /dev/null +++ b/plugins/plugin-shikiji/__test__/collapsedLinesPlugin.spec.ts @@ -0,0 +1,68 @@ +import MarkdownIt from 'markdown-it' +import { describe, expect, it } from 'vitest' +import { collapsedLinesPlugin } from '../src/node/markdown/collapsedLinesPlugin.js' + +const FENCE = '```' + +function createMarkdown(collapsedLines?: boolean | number, hasStyles = false) { + const md = new MarkdownIt() + + md.options.highlight = str => `
${str}
` + const fence = md.renderer.rules.fence! + md.renderer.rules.fence = (...args) => + `
${fence(...args)}
` + + md.use(collapsedLinesPlugin, collapsedLines ? { collapsedLines } : undefined) + + return md +} + +describe('collapsedLinesPlugin', () => { + const genLines = (n: number) => + Array.from({ length: n }).map((_, i) => `const line${i + 1} = ${i + 1}`).join('\n') + const code = `\ +${FENCE} +${genLines(10)} +${FENCE} + +${FENCE}js +${genLines(20)} +${FENCE} + +${FENCE}js :collapsed-lines +${genLines(20)} +${FENCE} + +${FENCE}js :collapsed-lines=10 +${genLines(12)} +${FENCE} + +${FENCE}js :no-collapsed-lines +${genLines(20)} +${FENCE} +` + it('should work with default', () => { + const md = createMarkdown() + expect(md.render(code)).toMatchSnapshot() + }) + + it('should work with true', () => { + const md = createMarkdown(true) + expect(md.render(code)).toMatchSnapshot() + }) + + it('should work with number', () => { + const md = createMarkdown(10) + expect(md.render(code)).toMatchSnapshot() + }) + + it('should not work with includes styles', () => { + const md = createMarkdown(true, true) + expect(md.render(code)).toMatchSnapshot() + }) + + it('should not work with false', () => { + const md = createMarkdown(false) + expect(md.render(code)).toMatchSnapshot() + }) +}) diff --git a/plugins/plugin-shikiji/__test__/copyCodeButton.spec.ts b/plugins/plugin-shikiji/__test__/copyCodeButton.spec.ts new file mode 100644 index 00000000..e3afd563 --- /dev/null +++ b/plugins/plugin-shikiji/__test__/copyCodeButton.spec.ts @@ -0,0 +1,49 @@ +import type { App } from 'vuepress/core' +import type { CopyCodeOptions } from '../src/node/types.js' +import MarkdownIt from 'markdown-it' +import { describe, expect, it } from 'vitest' +import { copyCodeButtonPlugin } from '../src/node/copy-code-button/index.js' + +function createMarkdown(options: boolean | CopyCodeOptions = {}, lang = 'en-US') { + const md = new MarkdownIt() + md.options.highlight = str => `
${str}
` + const fence = md.renderer.rules.fence! + md.renderer.rules.fence = (...args) => `
${fence(...args)}
` + const app = { + env: { isDebug: false }, + siteData: { + lang, + locales: { '/': { lang }, '/zh/': { lang: 'zh-CN' }, '/en/': { lang: 'en-US' } }, + }, + } as unknown as App + copyCodeButtonPlugin(md, app, options) + return md +} + +describe('copy code button plugin', () => { + it('should work with default', () => { + const md = createMarkdown() + + expect(md.render('```js\nconst a = 1\n```', { filePathRelative: '/test.md' })).toContain('` + return `` } } diff --git a/plugins/plugin-shikiji/src/node/markdown/collapsedLinesPlugin.ts b/plugins/plugin-shikiji/src/node/markdown/collapsedLinesPlugin.ts index 468ca1b0..ab8480f0 100644 --- a/plugins/plugin-shikiji/src/node/markdown/collapsedLinesPlugin.ts +++ b/plugins/plugin-shikiji/src/node/markdown/collapsedLinesPlugin.ts @@ -1,5 +1,5 @@ import type { Markdown } from 'vuepress/markdown' -import { resolveCollapsedLines } from '../utils/index.js' +import { resolveCollapsedLines } from '../utils/collapsedLines.js' export interface MarkdownItCollapsedLinesOptions { /**