test(utils, mdx-loader, core): improve coverage (#6303)

* test(utils, mdx-loader, core): improve coverage

* windows...

* fix
This commit is contained in:
Joshua Chen 2022-01-10 15:00:51 +08:00 committed by GitHub
parent cf265c051e
commit a79c23bc45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 841 additions and 219 deletions

View file

@ -0,0 +1 @@
![img](./notFound.png)

View file

@ -2,7 +2,7 @@
![](./static/img.png)
![img](./static/img.png)
![img](static/img.png)
![img from second static folder](/img2.png)

View file

@ -2,6 +2,8 @@
exports[`transformImage plugin fail if image does not exist 1`] = `"Image packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/static/img/doesNotExist.png or packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/static2/img/doesNotExist.png used in packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/fail.md not found."`;
exports[`transformImage plugin fail if image relative path does not exist 1`] = `"Image packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/notFound.png used in packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/fail2.md not found."`;
exports[`transformImage plugin fail if image url is absent 1`] = `"Markdown image URL is mandatory in \\"packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/noUrl.md\\" file"`;
exports[`transformImage plugin pathname protocol 1`] = `

View file

@ -45,6 +45,11 @@ describe('transformImage plugin', () => {
processFixture('fail', {staticDirs}),
).rejects.toThrowErrorMatchingSnapshot();
});
test('fail if image relative path does not exist', async () => {
await expect(
processFixture('fail2', {staticDirs}),
).rejects.toThrowErrorMatchingSnapshot();
});
test('fail if image url is absent', async () => {
await expect(
processFixture('noUrl', {staticDirs}),

View file

@ -33,13 +33,9 @@ const createJSX = (node: Image, pathUrl: string) => {
(jsxNode as unknown as Literal).type = 'jsx';
(jsxNode as unknown as Literal).value = `<img ${
node.alt ? `alt={"${escapeHtml(node.alt)}"} ` : ''
}${
node.url
? `src={require("${inlineMarkdownImageFileLoader}${escapePath(
pathUrl,
)}").default}`
: ''
}${node.title ? ` title="${escapeHtml(node.title)}"` : ''} />`;
}${`src={require("${inlineMarkdownImageFileLoader}${escapePath(
pathUrl,
)}").default}`}${node.title ? ` title="${escapeHtml(node.title)}"` : ''} />`;
if (jsxNode.url) {
delete (jsxNode as Partial<Image>).url;

View file

@ -8,6 +8,8 @@
[asset](asset.pdf 'Title')
[page](noUrl.md)
## Heading
```md

View file

@ -0,0 +1 @@
[nonexistent](@site/foo.pdf)

View file

@ -2,6 +2,8 @@
exports[`transformAsset plugin fail if asset url is absent 1`] = `"Markdown link URL is mandatory in \\"packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__fixtures__/noUrl.md\\" file (title: asset, line: 1)."`;
exports[`transformAsset plugin fail if asset with site alias does not exist 1`] = `"Asset packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__fixtures__/foo.pdf used in packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__fixtures__/nonexistentSiteAlias.md not found."`;
exports[`transformAsset plugin pathname protocol 1`] = `
"[asset](pathname:///asset/unchecked.pdf)
"
@ -18,6 +20,8 @@ exports[`transformAsset plugin transform md links to <a /> 1`] = `
<a target=\\"_blank\\" href={require('![CWD]/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default} title=\\"Title\\">asset</a>
[page](noUrl.md)
## Heading
\`\`\`md

View file

@ -43,6 +43,12 @@ describe('transformAsset plugin', () => {
).rejects.toThrowErrorMatchingSnapshot();
});
test('fail if asset with site alias does not exist', async () => {
await expect(
processFixture('nonexistentSiteAlias'),
).rejects.toThrowErrorMatchingSnapshot();
});
test('transform md links to <a />', async () => {
const result = await processFixture('asset');
expect(result).toMatchSnapshot();

View file

@ -59,11 +59,11 @@ function toAssetRequireNode({
path.relative(path.dirname(filePath), requireAssetPath),
);
const hash = hashRegex.test(node.url)
? node.url.substr(node.url.indexOf('#'))
? node.url.substring(node.url.indexOf('#'))
: '';
// nodejs does not like require("assets/file.pdf")
relativeRequireAssetPath = relativeRequireAssetPath.startsWith('.')
// require("assets/file.pdf") means requiring from a package called assets
relativeRequireAssetPath = relativeRequireAssetPath.startsWith('./')
? relativeRequireAssetPath
: `./${relativeRequireAssetPath}`;
@ -90,7 +90,7 @@ async function convertToAssetLinkIfNeeded(
const hasSiteAlias = assetPath.startsWith('@site/');
const hasAssetLikeExtension =
path.extname(assetPath) && !assetPath.match(/#|.md|.mdx|.html/);
path.extname(assetPath) && !assetPath.match(/#|\.md$|\.mdx$|\.html$/);
const looksLikeAssetLink = hasSiteAlias || hasAssetLikeExtension;