Skip to content

Commit

Permalink
chore(core): added utils specs
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed May 11, 2024
1 parent 91978aa commit a9f83e4
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { camelToTitleCase } from './camel-to-title-case';

describe('camelToTitleCase', () => {
it('should convert camel case to title case correctly', () => {
const input = 'camelCaseText';
const expectedOutput = 'Camel Case Text';
const result = camelToTitleCase(input);
expect(result).toEqual(expectedOutput);
});

it('should handle single word correctly', () => {
const input = 'word';
const expectedOutput = 'Word';
const result = camelToTitleCase(input);
expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { escapeChars } from './escape-chars';

describe('escapeChars', () => {
it('should escape special characters correctly', () => {
const input = 'This is a string with >, <, {, }, _, `, |, [, ], and *';
const expectedOutput =
'This is a string with \\>, \\<, \\{, \\}, \\_, \\`, \\|, \\[, \\], and \\*';
const result = escapeChars(input);
expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { formatMarkdown } from './format-markdown';

describe('formatMarkdown', () => {
it('should trim content and restrict new lines', () => {
const input = `
# headline
Paragraph
## headline
`;
const expectedOutput = `# headline
Paragraph
## headline
`;
const result = formatMarkdown(input);
expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { formatTableColumn } from './format-table-column';

describe('formatTableColumn', () => {
it('should format table column correctly', () => {
const input = `This is a string with
a newline, | a pipe, and a code block:
\`\`\`ts
const x = 10;
\`\`\``;
const expectedOutput =
'This is a string with<br />a newline, \\| a pipe, and a code block:<br />`const x = 10;`';
const result = formatTableColumn(input);
expect(result).toEqual(expectedOutput);
});

it('should remove trailing <br /> tags', () => {
const input = 'This is a string with a trailing <br /> tag<br /> ';
const expectedOutput = 'This is a string with a trailing <br /> tag';
const result = formatTableColumn(input);
expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import path from 'path';
import { getFileNameWithExtension } from './get-file-name-with-extension';

describe('getFileNameWithExtension', () => {
it('should return filename with extension', () => {
const fileName = 'testFile';
const fileExtension = '.txt';
const expectedOutput = 'testFile.txt';

const result = getFileNameWithExtension(fileName, fileExtension);

expect(result).toEqual(expectedOutput);
});

it('should handle fileExtension without dot', () => {
const fileName = 'testFile';
const fileExtension = 'txt';
const expectedOutput = 'testFile.txt';

const result = getFileNameWithExtension(fileName, fileExtension);

expect(result).toEqual(expectedOutput);
});

it('should handle fileName with directories', () => {
const fileName = path.join('dir1', 'dir2', 'testFile');
const fileExtension = 'txt';
const expectedOutput = path.join('dir1', 'dir2', 'testFile.txt');

const result = getFileNameWithExtension(fileName, fileExtension);

expect(result).toEqual(expectedOutput);
});
});

This file was deleted.

4 changes: 1 addition & 3 deletions packages/typedoc-plugin-markdown/src/libs/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ export { escapeChars } from './escape-chars';
export { formatMarkdown } from './format-markdown';
export { formatTableColumn } from './format-table-column';
export { getFileNameWithExtension } from './get-file-name-with-extension';
export { getFirstParagrph } from './get-first-paragraph';
export { isQuoted } from './is-quoted';
export { removeFirstScopedDirectory } from './remove-first-scoped-directory';
export { removeLineBreaks } from './remove-line-breaks';
export { sanitizeComments } from './sanitize-comments';
export { slugifyUrl } from './slugify-url';
export { stripComments } from './strip-comments';
export { slugify } from './slugify';
export { unEscapeChars } from './un-escape-chars';
21 changes: 21 additions & 0 deletions packages/typedoc-plugin-markdown/src/libs/utils/is-quoted.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isQuoted } from './is-quoted';

describe('isQuoted', () => {
it('should return true for quoted strings', () => {
const input = '"This is a quoted string"';
const expectedOutput = true;

const result = isQuoted(input);

expect(result).toEqual(expectedOutput);
});

it('should return false for unquoted strings', () => {
const input = 'This is an unquoted string';
const expectedOutput = false;

const result = isQuoted(input);

expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import path from 'path';
import { removeFirstScopedDirectory } from './remove-first-scoped-directory';

describe('removeFirstScopedDirectory', () => {
it('should remove first scoped directory', () => {
const input = '@scoped/dir1/dir2/file.txt';
const expectedOutput = `dir1${path.sep}dir2${path.sep}file.txt`;

const result = removeFirstScopedDirectory(input);

expect(result).toEqual(expectedOutput);
});

it('should handle string without scoped directory', () => {
const input = 'dir1/dir2/file.txt';
const expectedOutput = `dir1${path.sep}dir2${path.sep}file.txt`;

const result = removeFirstScopedDirectory(input);

expect(result).toEqual(expectedOutput);
});

it('should handle string with multiple scoped directories', () => {
const input = '@scoped/dir1/@scoped/dir2/file.txt';
const expectedOutput = `dir1${path.sep}@scoped${path.sep}dir2${path.sep}file.txt`;

const result = removeFirstScopedDirectory(input);

expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { removeLineBreaks } from './remove-line-breaks';

describe('removeLineBreaks', () => {
it('should remove line breaks and replace with a single space', () => {
const input = 'This is a string\nwith multiple\n\nline breaks';
const expectedOutput = 'This is a string with multiple line breaks';

const result = removeLineBreaks(input);

expect(result).toEqual(expectedOutput);
});

it('should replace multiple spaces with a single space', () => {
const input = 'This is a string with multiple spaces';
const expectedOutput = 'This is a string with multiple spaces';

const result = removeLineBreaks(input);

expect(result).toEqual(expectedOutput);
});
});
21 changes: 21 additions & 0 deletions packages/typedoc-plugin-markdown/src/libs/utils/slugify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { slugify } from './slugify';

describe('slugifyUrl', () => {
it('should convert to slug correctly', () => {
const input = ' Type Alias ';
const expectedOutput = 'type-alias';

const result = slugify(input);

expect(result).toEqual(expectedOutput);
});

it('should handle URL with special characters', () => {
const input = 'Reflection!';
const expectedOutput = 'reflection';

const result = slugify(input);

expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function slugifyUrl(url: string) {
export function slugify(url: string) {
return url
.toLowerCase()
.trim()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { unEscapeChars } from './un-escape-chars';

describe('unEscapeChars', () => {
it('should unescape characters correctly', () => {
const input = '\\*\\<\\>\\_\\{\\}\\`\\*\\|\\]\\[';
const expectedOutput = '*<>_{}`*|][';

const result = unEscapeChars(input);

expect(result).toEqual(expectedOutput);
});

it('should handle string without escaped characters', () => {
const input = 'This is a string without escaped characters';
const expectedOutput = 'This is a string without escaped characters';

const result = unEscapeChars(input);

expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getFileNameWithExtension,
isQuoted,
removeFirstScopedDirectory,
slugifyUrl,
slugify,
} from '@plugin/libs/utils';
import { OutputFileStrategy } from '@plugin/options/option-maps';
import { MarkdownTheme } from '@plugin/theme/markdown-theme';
Expand Down Expand Up @@ -331,9 +331,7 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {

return urlOption.directory
? urlOption.directory
: `${slugifyUrl(
ReflectionKind.singularString(reflection.kind),
)}.${alias}`;
: `${slugify(ReflectionKind.singularString(reflection.kind))}.${alias}`;
};

const filename = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const partials = (context: MarkdownThemeContext) => {
},
) => declaration.apply(context, [model, options]) as string,
/**
* Remders a declaration title.
*
*
* @category Member Partials
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { backTicks, bold, codeBlock } from '@plugin/libs/markdown';
import { escapeChars, stripComments } from '@plugin/libs/utils';
import { escapeChars } from '@plugin/libs/utils';
import { MarkdownThemeContext } from '@plugin/theme';
import { DeclarationReflection } from 'typedoc';

/**
* Remders a declaration title.
*
* @category Member Partials
*/
Expand Down Expand Up @@ -83,7 +82,7 @@ export function declarationTitle(
model.defaultValue !== '...' &&
model.defaultValue !== model.name
) {
md.push(` = \`${stripComments(model.defaultValue)}\``);
md.push(` = \`${model.defaultValue}\``);
}

if (useCodeBlocks) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { heading, link, table } from '@plugin/libs/markdown';
import { escapeChars, getFirstParagrph } from '@plugin/libs/utils';
import { escapeChars } from '@plugin/libs/utils';
import { PLURAL_KIND_KEY_MAP } from '@plugin/options/text-mappings';
import { MarkdownThemeContext } from '@plugin/theme';
import { TextContentMappings } from 'public-api';
Expand Down Expand Up @@ -110,9 +110,7 @@ function getTable(
const comment = context.helpers.getDeclarationComment(child);

if (comment?.summary?.length) {
row.push(
getFirstParagrph(context.partials.commentParts(comment.summary)),
);
row.push(context.partials.commentParts(comment.summary)?.split('\n')[0]);
} else {
row.push('-');
}
Expand Down

0 comments on commit a9f83e4

Please sign in to comment.