Skip to content

Commit

Permalink
test(src): adds tests for plugin, updates jest.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rockchalkwushock committed Apr 23, 2021
1 parent 650b7a8 commit 4cd581e
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 5 deletions.
8 changes: 7 additions & 1 deletion jest.config.js
@@ -1,8 +1,14 @@
module.exports = {
bail: true,
collectCoverage: true,
collectCoverageFrom: ['src/index.ts'],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json',
},
},
preset: 'ts-jest',
roots: ['<rootDir>'],
testEnvironment: 'node',
transform: { '^.+\\.(ts|tsx|js|jsx)?$': 'ts-jest' },
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(ts|tsx)$'],
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -59,6 +59,7 @@
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"rehype": "^11.0.0",
"standard-version": "^9.2.0",
"ts-jest": "^26.5.5",
"typescript": "^4.2.4"
Expand Down
5 changes: 5 additions & 0 deletions src/__snapshots__/index.test.ts.snap
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rehype-code-titles Snapshot w/ title block 1`] = `"<div class=\\"rehype-code-title\\">lib/mdx.ts</div><pre><code class=\\"language-typescript\\"></code></pre>"`;
exports[`rehype-code-titles Snapshot w/out title block 1`] = `"<pre><code class=\\"language-typescript\\"></code></pre>"`;
45 changes: 45 additions & 0 deletions src/index.test.ts
@@ -0,0 +1,45 @@
import rehype from 'rehype'
import dedent from 'dedent'
import rehypeCodeTitles from './index'

const processHtml = (html: string): string => {
return rehype()
.data('settings', { fragment: true })
.use(rehypeCodeTitles)
.processSync(html)
.toString()
}

const classNameWithTitle = 'language-typescript:lib/mdx.ts'
const className = 'language-typescript'

describe('rehype-code-titles', () => {
test('Snapshot w/ title block', () => {
const result = processHtml(dedent`
<pre><code class="${classNameWithTitle}"></code></pre>
`)
expect(result).toMatchSnapshot()
})
test('Snapshot w/out title block', () => {
const result = processHtml(dedent`
<pre><code class="${className}"></code></pre>
`)
expect(result).toMatchSnapshot()
})
test('Contains title block', () => {
const result = processHtml(dedent`
<pre><code class="${classNameWithTitle}"></code></pre>
`)
expect(result.includes('div')).toEqual(true)
expect(result.includes('rehype-code-title')).toEqual(true)
expect(result.includes('lib/mdx.ts')).toEqual(true)
})
test('Does not contain title block', () => {
const result = processHtml(dedent`
<pre><code class="${className}"></code></pre>
`)
expect(result.includes('div')).not.toEqual(true)
expect(result.includes('rehype-code-title')).not.toEqual(true)
expect(result.includes('lib/mdx.ts')).not.toEqual(true)
})
})
7 changes: 7 additions & 0 deletions tsconfig.test.json
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"allowJs": true,
"noEmit": true
}
}

0 comments on commit 4cd581e

Please sign in to comment.