pengzhanbo 4464703b7b
test: add unit test (#262)
* test: add unit test

* chore: tweak

* chore: tweak
2024-10-12 02:09:15 +08:00

80 lines
1.6 KiB
TypeScript

import type { CodeTabsOptions } from '../src/shared/codeTabs.js'
import MarkdownIt from 'markdown-it'
import { describe, expect, it } from 'vitest'
import { codeTabs } from '../src/node/container/codeTabs.js'
function createMarkdown(options?: CodeTabsOptions) {
const md = new MarkdownIt()
md.options.highlight = (str, lang) => {
return `<div class="language-${lang}"><pre><code>${str}</code></pre></div>`
}
md.use(codeTabs, options)
return md
}
const FENCE = '```'
describe('codeTabsPlugin', () => {
const code = `\
::: code-tabs
@tab tab-1
${FENCE}js
const a = 1
${FENCE}
@tab tab-2
${FENCE}js
const b = 2
${FENCE}
@tab tab-3
${FENCE}js
const c = 3
${FENCE}
:::
::: code-tabs
@tab npm
${FENCE}sh
npm i
${FENCE}
@tab:active pnpm
${FENCE}sh
pnpm i
${FENCE}
@tab yarn
${FENCE}sh
yarn
${FENCE}
:::
::: code-tabs#pm
@tab a.ts
${FENCE}ts
const a = 1
${FENCE}
@tab a.js
${FENCE}js
const a = 1
${FENCE}
:::
`
it('should work with default', () => {
const md = createMarkdown()
expect(md.render(code)).toMatchSnapshot()
})
it('should work with no icon', () => {
const md = createMarkdown({ icon: false })
expect(md.render(code)).toMatchSnapshot()
})
it('should work with options: `{ named: false, extensions: false }`', () => {
const md = createMarkdown({ icon: { named: false, extensions: false } })
expect(md.render(code)).toMatchSnapshot()
})
it('should work with options: { named: [npm,pnpm,yarn], extensions: [.js,.ts] }', () => {
const md = createMarkdown({ icon: { named: ['npm', 'pnpm', 'yarn'], extensions: ['.js', '.ts'] } })
expect(md.render(code)).toMatchSnapshot()
})
})