Skip to content

Commit

Permalink
wip: unit test mock
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Oct 12, 2023
1 parent 2f6cddb commit 0478dd8
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 52 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`directives remark plugin default behavior for custom keyword 1`] = `
"<p>Test directives</p>
<p><div></div> <div></div></p>
<p>Text<div></div></p>
<p>Text:base</p>
<p>Text: base</p>
<div></div>
<div><p>::::info <strong>Weather</strong>
On nice days, you can enjoy skiing in the mountains.</p><p>:::danger <em>Storms</em>
Take care of snowstorms...</p></div>
<p>::::</p>"
"<admonition type="danger"><p>Take care of snowstorms...</p></admonition>
<div><p>unused directive content</p></div>
<p>:::NotAContainerDirective with a phrase after</p>
<p>:::</p>
<p>Phrase before :::NotAContainerDirective</p>
<p>:::</p>"
`;

exports[`directives remark plugin default behavior for custom keyword 2`] = `
[
[
"Unused Directives found: ",
[
{
"name": "danger",
"type": "containerDirective",
},
{
"name": "unusedDirective",
"type": "containerDirective",
},
],
],
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,37 @@ import path from 'path';
import remark2rehype from 'remark-rehype';
import stringify from 'rehype-stringify';
import vfile from 'to-vfile';
import preprocessor from '../../../preprocessor';
import plugin from '../index';
import admonition from '../../admonitions';

const processFixture = async (name: string) => {
const {remark} = await import('remark');
const {default: directives} = await import('remark-directive');

const filePath = path.join(__dirname, '__fixtures__', `${name}.md`);
const file = await vfile.read(filePath);
const fileContentPreprocessed = preprocessor({
fileContent: file.toString(),
filePath,
markdownConfig: {
mermaid: false,
mdx1Compat: {
admonitions: false,
comments: false,
headingIds: false,
},
},
});

const result = await remark()
.use(directives)
.use(admonition)
.use(plugin)
.use(remark2rehype)
.use(stringify)
.process(fileContentPreprocessed);
.process(file);

return result.value;
};

describe('directives remark plugin', () => {
it('default behavior for custom keyword', async () => {
const result = await processFixture('directives');
const consoleMock = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});

const result = await processFixture('containerDirectives');

expect(result).toMatchSnapshot();

expect(consoleMock.mock.calls).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
import visit from 'unist-util-visit';
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
import type {Transformer, Processor, Parent} from 'unified';
// import type {
// ContainerDirective,
// LeafDirective,
// TextDirective,
// // @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
// } from 'mdast-util-directive';

// TODO as of April 2023, no way to import/re-export this ESM type easily :/
// This might change soon, likely after TS 5.2
Expand All @@ -20,6 +26,7 @@ const plugin: Plugin = function plugin(this: Processor): Transformer {
name: string | null;
type: string;
}> = [];

const directiveTypes = [
'containerDirective',
'leafDirective',
Expand All @@ -35,16 +42,17 @@ const plugin: Plugin = function plugin(this: Processor): Transformer {
// path: ` ${filePath}:${node.position.start.line}:${node.position.start.column}`,
});

if (node.children) {
node.children.forEach((child: any) => directiveVisitor(child));
}
// if (node.children) {
// node.children.forEach((child: any) => directiveVisitor(child));
// }
}
};

visit<Parent>(tree, 'root', directiveVisitor);
visit<Parent>(tree, directiveTypes, directiveVisitor);
// visit<Parent>(tree, '', directiveVisitor);

if (unusedDirectives.length > 0) {
console.warn('unusedDirectives', unusedDirectives);
console.warn('Unused Directives found: ', unusedDirectives);
}
};
};
Expand Down

0 comments on commit 0478dd8

Please sign in to comment.