docusaurus/packages/docusaurus-utils/src/__tests__/emitUtils.test.ts
Sébastien Lorber de972142a8
chore: backport retro compatible commits for the Docusaurus v2.2 release (#8264)
Co-authored-by: Jan Peer Stoecklmair <jan.peer.stoecklmair@dynatrace.com>
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
Co-authored-by: LittleboyHarry <littleboyharry@qq.com>
Co-authored-by: Mikey O'Toole <mikey@homotechsual.dev>
Co-authored-by: Jan Peer Stöcklmair <jan.oster94@gmail.com>
Co-authored-by: Nguyễn Thành Nam <namnguyenthanh.work@gmail.com>
Co-authored-by: Sanjaiyan Parthipan <parthipankalayini@gmail.com>
Co-authored-by: Ramazan SANCAR <ramazansancar4545@gmail.com>
Co-authored-by: mturoci <64769322+mturoci@users.noreply.github.com>
Co-authored-by: Adnan Hashmi <56730784+adnanhashmi09@users.noreply.github.com>
Co-authored-by: Pranav Joglekar <pranav2000joglekar@gmail.com>
Co-authored-by: forgeRW <20483211+forgeRW@users.noreply.github.com>
Co-authored-by: Masahiko Hara <pasora@sfc.wide.ad.jp>
Co-authored-by: Johan Fagerberg <johanringmann@gmail.com>
Co-authored-by: John Reilly <johnny_reilly@hotmail.com>
Co-authored-by: Sam Wall <oss@samuelwall.co.uk>
Co-authored-by: Jeferson S. Brito <30840709+jeferson-sb@users.noreply.github.com>
Co-authored-by: evan <evanmccarthy@outlook.com>
Co-authored-by: Xabier Lahuerta Vazquez <xlahuerta@protonmail.com>
Co-authored-by: Forresst <forresst17@gmail.com>
Co-authored-by: Shanmughapriyan S <priyanshan03@gmail.com>
Co-authored-by: Alexey Pyltsyn <lex61rus@gmail.com>
2022-10-29 15:13:42 +02:00

158 lines
4.7 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {jest} from '@jest/globals';
import path from 'path';
import fs from 'fs-extra';
import {readOutputHTMLFile, generate} from '../emitUtils';
describe('readOutputHTMLFile', () => {
it('reads both files with trailing slash undefined', async () => {
await expect(
readOutputHTMLFile(
'/file',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).resolves.toBe('file\n');
await expect(
readOutputHTMLFile(
'/folder',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).resolves.toBe('folder\n');
await expect(
readOutputHTMLFile(
'/file/',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).resolves.toBe('file\n');
await expect(
readOutputHTMLFile(
'/folder/',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).resolves.toBe('folder\n');
});
it('reads only folder with trailing slash true', async () => {
await expect(
readOutputHTMLFile(
'/folder',
path.join(__dirname, '__fixtures__/build-snap'),
true,
).then(String),
).resolves.toBe('folder\n');
await expect(
readOutputHTMLFile(
'/folder/',
path.join(__dirname, '__fixtures__/build-snap'),
true,
).then(String),
).resolves.toBe('folder\n');
});
it('reads only file trailing slash false', async () => {
await expect(
readOutputHTMLFile(
'/file',
path.join(__dirname, '__fixtures__/build-snap'),
false,
).then(String),
).resolves.toBe('file\n');
await expect(
readOutputHTMLFile(
'/file/',
path.join(__dirname, '__fixtures__/build-snap'),
false,
).then(String),
).resolves.toBe('file\n');
});
it('reads file ending in .html', async () => {
await expect(
readOutputHTMLFile(
'/htmlFile.html',
path.join(__dirname, '__fixtures__/build-snap'),
false,
).then(String),
).resolves.toBe('htmlFile.html\n');
await expect(
readOutputHTMLFile(
'/htmlFile.html',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).resolves.toBe('htmlFile.html\n');
});
it('reads file ending in .html in folder containing .html', async () => {
await expect(
readOutputHTMLFile(
'/weird.html.folder/nestedHtmlFile.html',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).resolves.toBe('nestedHtmlFile.html\n');
await expect(
readOutputHTMLFile(
'/weird.html.folder/nestedHtmlFile.html',
path.join(__dirname, '__fixtures__/build-snap'),
undefined,
).then(String),
).resolves.toBe('nestedHtmlFile.html\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 for permalink /nonExistent."`,
);
});
});
describe('generate', () => {
const writeMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
const existsMock = jest.spyOn(fs, 'pathExists');
const readMock = jest.spyOn(fs, 'readFile');
it('works with no file and no cache', async () => {
existsMock.mockImplementationOnce(() => false);
await generate(__dirname, 'foo', 'bar');
expect(writeMock).toHaveBeenNthCalledWith(
1,
path.join(__dirname, 'foo'),
'bar',
);
});
it('works with existing cache', async () => {
await generate(__dirname, 'foo', 'bar');
expect(writeMock).toBeCalledTimes(1);
});
it('works with existing file but no cache', async () => {
existsMock.mockImplementationOnce(() => true);
// @ts-expect-error: seems the typedef doesn't understand overload
readMock.mockImplementationOnce(() => Promise.resolve('bar'));
await generate(__dirname, 'baz', 'bar');
expect(writeMock).toBeCalledTimes(1);
});
it('works when force skipping cache', async () => {
await generate(__dirname, 'foo', 'bar', true);
expect(writeMock).toHaveBeenNthCalledWith(
2,
path.join(__dirname, 'foo'),
'bar',
);
});
});