test: improve test coverage (#7113)

This commit is contained in:
Joshua Chen 2022-04-05 14:09:19 +08:00 committed by GitHub
parent 4194925da9
commit e610a4ac00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 235 additions and 61 deletions

View file

@ -11,7 +11,7 @@ import path from 'path';
import fs from 'fs-extra';
describe('readOutputHTMLFile', () => {
it('trailing slash undefined', async () => {
it('reads both files with trailing slash undefined', async () => {
await expect(
readOutputHTMLFile(
'/file',
@ -41,7 +41,7 @@ describe('readOutputHTMLFile', () => {
).then(String),
).resolves.toBe('folder\n');
});
it('trailing slash true', async () => {
it('reads only folder with trailing slash true', async () => {
await expect(
readOutputHTMLFile(
'/folder',
@ -57,7 +57,7 @@ describe('readOutputHTMLFile', () => {
).then(String),
).resolves.toBe('folder\n');
});
it('trailing slash false', async () => {
it('reads only file trailing slash false', async () => {
await expect(
readOutputHTMLFile(
'/file',
@ -73,6 +73,18 @@ describe('readOutputHTMLFile', () => {
).then(String),
).resolves.toBe('file\n');
});
// Can it ever happen?
it('throws if file does not exist', async () => {
await expect(
readOutputHTMLFile(
'/nonExistent',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Expected output HTML file to be found at <PROJECT_ROOT>/packages/docusaurus-utils/src/__tests__/__fixtures__/build-snap/nonExistent/index.html."`,
);
});
});
describe('generate', () => {

View file

@ -83,18 +83,16 @@ export async function readOutputHTMLFile(
outDir,
`${permalink.replace(/\/$/, '')}.html`,
);
if (trailingSlash) {
return fs.readFile(withTrailingSlashPath);
} else if (trailingSlash === false) {
return fs.readFile(withoutTrailingSlashPath);
}
const HTMLPath = await findAsyncSequential(
[withTrailingSlashPath, withoutTrailingSlashPath],
[
trailingSlash !== false && withTrailingSlashPath,
trailingSlash !== true && withoutTrailingSlashPath,
].filter((p): p is string => Boolean(p)),
fs.pathExists,
);
if (!HTMLPath) {
throw new Error(
`Expected output HTML file to be found at ${withTrailingSlashPath}`,
`Expected output HTML file to be found at ${withTrailingSlashPath}.`,
);
}
return fs.readFile(HTMLPath);