diff --git a/src/app/constants.ts b/src/app/constants.ts new file mode 100644 index 0000000..f1fdba1 --- /dev/null +++ b/src/app/constants.ts @@ -0,0 +1,6 @@ +import path from 'path'; + +export const DOCS_PATH = path.join( + process.cwd(), + 'src/docs' +); diff --git a/src/app/lib/markdoc.ts b/src/app/lib/markdoc.ts index 87b2cbb..fb68c5b 100644 --- a/src/app/lib/markdoc.ts +++ b/src/app/lib/markdoc.ts @@ -5,11 +5,10 @@ import React, { ReactNode } from 'react'; import Clients from '../components/Clients'; import CodeSnippet from '../components/CodeSnippet'; import Fence from '../components/Fence'; +import { DOCS_PATH } from '../constants'; import { Doc, DocSection } from './../types/types'; -const DOCS_PATH = path.join(process.cwd(), 'src/docs'); - -function getCopyPath( +export function getCopyPath( version: string, doc: Doc, docPath: string @@ -21,7 +20,7 @@ function getCopyPath( return path.join(DOCS_PATH, version, docPath, doc.copy); } -function getExamplePath( +export function getExamplePath( version: string, doc: Doc, docPath: string diff --git a/tests/unit/Markdoc.spec.ts b/tests/unit/Markdoc.spec.ts index 555fe08..c44339c 100644 --- a/tests/unit/Markdoc.spec.ts +++ b/tests/unit/Markdoc.spec.ts @@ -1,5 +1,151 @@ -describe('test', () => { - it('should pass', () => { - expect(true).toBe(true); +import { DOCS_PATH } from '@/app/constants'; +import { + getCopyPath, + getExamplePath, +} from '@/app/lib/markdoc'; +import { Doc } from '@/app/types/types'; +import path from 'path'; + +describe('Markdoc lib functions', () => { + describe('getCopyPath', () => { + const version = 'v1.0.0'; + const doc: Doc = { + title: 'Title', + folder: 'folder', + copy: 'copy.md', + }; + const docPath = 'path/to/doc'; + + it('returns empty string if doc.copy is undefined', () => { + const doc: Doc = { + title: 'Title', + folder: 'folder', + }; + const result = getCopyPath(version, doc, docPath); + expect(result).toEqual(''); + }); + + it('returns correct copy path', () => { + const result = getCopyPath(version, doc, docPath); + const expected = path.join( + DOCS_PATH, + version, + docPath, + doc.copy! + ); + expect(result).toEqual(expected); + }); + + it('returns correct copy path with leading/trailing slashes in docPath', () => { + const result = getCopyPath( + version, + doc, + '/path/to/doc/' + ); + const expected = path.join( + DOCS_PATH, + version, + docPath, + doc.copy! + ); + expect(result).toEqual(expected); + }); + + it('returns correct copy path with different version', () => { + const result = getCopyPath('v2.0.0', doc, docPath); + const expected = path.join( + DOCS_PATH, + 'v2.0.0', + docPath, + doc.copy! + ); + expect(result).toEqual(expected); + }); + + it('returns correct copy path with nested docPath', () => { + const result = getCopyPath( + version, + doc, + 'path/to/nested/doc' + ); + const expected = path.join( + DOCS_PATH, + version, + 'path/to/nested/doc', + doc.copy! + ); + expect(result).toEqual(expected); + }); + }); + + describe('getExamplePath', () => { + const version = 'v1.0.0'; + const doc: Doc = { + title: 'Title', + folder: 'folder', + example: 'example.md', + }; + const docPath = 'path/to/doc'; + + it('returns empty string if doc.example is undefined', () => { + const doc: Doc = { + title: 'Title', + folder: 'folder', + }; + const result = getExamplePath(version, doc, docPath); + expect(result).toEqual(''); + }); + + it('returns correct example path', () => { + const result = getExamplePath(version, doc, docPath); + const expected = path.join( + DOCS_PATH, + version, + docPath, + doc.example! + ); + expect(result).toEqual(expected); + }); + + it('returns correct example path with leading/trailing slashes in docPath', () => { + const result = getExamplePath( + version, + doc, + '/path/to/doc/' + ); + const expected = path.join( + DOCS_PATH, + version, + docPath, + doc.example! + ); + expect(result).toEqual(expected); + }); + + it('returns correct example path with different version', () => { + const result = getExamplePath('v2.0.0', doc, docPath); + const expected = path.join( + DOCS_PATH, + 'v2.0.0', + docPath, + doc.example! + ); + expect(result).toEqual(expected); + }); + + it('returns correct example path with nested docPath', () => { + const result = getExamplePath( + version, + doc, + 'path/to/nested/doc' + ); + const expected = path.join( + DOCS_PATH, + version, + 'path/to/nested/doc', + doc.example! + ); + expect(result).toEqual(expected); + }); }); });