tests: write test for parseMarkdown function

This commit is contained in:
Stevan Freeborn
2023-04-30 16:11:17 -05:00
parent 734189ba35
commit ce9c61ff23
2 changed files with 108 additions and 2 deletions
+4 -2
View File
@@ -8,7 +8,7 @@ import Fence from '../components/Fence';
import { DOCS_PATH } from '../constants'; import { DOCS_PATH } from '../constants';
import { Doc, DocSection } from './../types/types'; import { Doc, DocSection } from './../types/types';
const markDocConfig = { export const markDocConfig = {
nodes: { nodes: {
fence: { fence: {
render: 'Fence', render: 'Fence',
@@ -48,7 +48,9 @@ const markDocConfig = {
}, },
}; };
function parseMarkdown(sourcePath: string): ReactNode { export function parseMarkdown(
sourcePath: string
): ReactNode {
const source = fs.readFileSync(sourcePath, 'utf8'); const source = fs.readFileSync(sourcePath, 'utf8');
const ast = Markdoc.parse(source); const ast = Markdoc.parse(source);
const content = Markdoc.transform( const content = Markdoc.transform(
@@ -1,19 +1,29 @@
import Clients from '@/app/components/Clients';
import CodeSnippet from '@/app/components/CodeSnippet';
import Fence from '@/app/components/Fence';
import { DOCS_PATH } from '@/app/constants'; import { DOCS_PATH } from '@/app/constants';
import { import {
getCopyPath, getCopyPath,
getExamplePath, getExamplePath,
markDocConfig,
parseMarkdown,
} from '@/app/lib/markdoc'; } from '@/app/lib/markdoc';
import { Doc } from '@/app/types/types'; import { Doc } from '@/app/types/types';
import { default as Markdoc } from '@markdoc/markdoc';
import fs from 'fs';
import path from 'path'; import path from 'path';
import React from 'react';
describe('Markdoc lib functions', () => { describe('Markdoc lib functions', () => {
describe('getCopyPath', () => { describe('getCopyPath', () => {
const version = 'v1.0.0'; const version = 'v1.0.0';
const doc: Doc = { const doc: Doc = {
title: 'Title', title: 'Title',
folder: 'folder', folder: 'folder',
copy: 'copy.md', copy: 'copy.md',
}; };
const docPath = 'path/to/doc'; const docPath = 'path/to/doc';
it('returns empty string if doc.copy is undefined', () => { it('returns empty string if doc.copy is undefined', () => {
@@ -21,18 +31,22 @@ describe('Markdoc lib functions', () => {
title: 'Title', title: 'Title',
folder: 'folder', folder: 'folder',
}; };
const result = getCopyPath(version, doc, docPath); const result = getCopyPath(version, doc, docPath);
expect(result).toEqual(''); expect(result).toEqual('');
}); });
it('returns correct copy path', () => { it('returns correct copy path', () => {
const result = getCopyPath(version, doc, docPath); const result = getCopyPath(version, doc, docPath);
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
version, version,
docPath, docPath,
doc.copy! doc.copy!
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
@@ -42,23 +56,27 @@ describe('Markdoc lib functions', () => {
doc, doc,
'/path/to/doc/' '/path/to/doc/'
); );
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
version, version,
docPath, docPath,
doc.copy! doc.copy!
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it('returns correct copy path with different version', () => { it('returns correct copy path with different version', () => {
const result = getCopyPath('v2.0.0', doc, docPath); const result = getCopyPath('v2.0.0', doc, docPath);
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
'v2.0.0', 'v2.0.0',
docPath, docPath,
doc.copy! doc.copy!
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
@@ -68,23 +86,27 @@ describe('Markdoc lib functions', () => {
doc, doc,
'path/to/nested/doc' 'path/to/nested/doc'
); );
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
version, version,
'path/to/nested/doc', 'path/to/nested/doc',
doc.copy! doc.copy!
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
describe('getExamplePath', () => { describe('getExamplePath', () => {
const version = 'v1.0.0'; const version = 'v1.0.0';
const doc: Doc = { const doc: Doc = {
title: 'Title', title: 'Title',
folder: 'folder', folder: 'folder',
example: 'example.md', example: 'example.md',
}; };
const docPath = 'path/to/doc'; const docPath = 'path/to/doc';
it('returns empty string if doc.example is undefined', () => { it('returns empty string if doc.example is undefined', () => {
@@ -92,18 +114,22 @@ describe('Markdoc lib functions', () => {
title: 'Title', title: 'Title',
folder: 'folder', folder: 'folder',
}; };
const result = getExamplePath(version, doc, docPath); const result = getExamplePath(version, doc, docPath);
expect(result).toEqual(''); expect(result).toEqual('');
}); });
it('returns correct example path', () => { it('returns correct example path', () => {
const result = getExamplePath(version, doc, docPath); const result = getExamplePath(version, doc, docPath);
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
version, version,
docPath, docPath,
doc.example! doc.example!
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
@@ -113,23 +139,27 @@ describe('Markdoc lib functions', () => {
doc, doc,
'/path/to/doc/' '/path/to/doc/'
); );
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
version, version,
docPath, docPath,
doc.example! doc.example!
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it('returns correct example path with different version', () => { it('returns correct example path with different version', () => {
const result = getExamplePath('v2.0.0', doc, docPath); const result = getExamplePath('v2.0.0', doc, docPath);
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
'v2.0.0', 'v2.0.0',
docPath, docPath,
doc.example! doc.example!
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
@@ -139,12 +169,86 @@ describe('Markdoc lib functions', () => {
doc, doc,
'path/to/nested/doc' 'path/to/nested/doc'
); );
const expected = path.join( const expected = path.join(
DOCS_PATH, DOCS_PATH,
version, version,
'path/to/nested/doc', 'path/to/nested/doc',
doc.example! doc.example!
); );
expect(result).toEqual(expected);
});
});
describe('parseMarkdown', () => {
const sourcePath = 'test.md';
const sourceContent = '# Test Heading';
beforeAll(() => {
jest
.spyOn(fs, 'readFileSync')
.mockReturnValue(sourceContent);
});
it('should parse markdown file and return react node', () => {
const expected = (
<article>
<h1>Test Heading</h1>
</article>
);
const result = parseMarkdown(sourcePath);
expect(result).toEqual(expected);
});
it('should read source file using the correct path', () => {
parseMarkdown(sourcePath);
expect(fs.readFileSync).toHaveBeenCalledWith(
sourcePath,
'utf8'
);
});
it('should transform the AST using markDocConfig', () => {
const ast = Markdoc.parse(sourceContent);
const spy = jest.spyOn(Markdoc, 'transform');
parseMarkdown(sourcePath);
expect(spy).toHaveBeenCalledWith(
ast,
markDocConfig as any
);
});
it('should render react components using Markdoc.renderers', () => {
const ast = Markdoc.parse(sourceContent);
const content = Markdoc.transform(
ast,
markDocConfig as any
);
const expected = (
<article>
<h1>Test Heading</h1>
</article>
);
const spy = jest.spyOn(Markdoc.renderers, 'react');
const result = parseMarkdown(sourcePath);
expect(spy).toHaveBeenCalledWith(content, React, {
components: {
CodeSnippet: CodeSnippet,
Fence: Fence,
Clients: Clients,
},
});
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });