mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-08 05:42:34 +02:00
test: enable a few jest eslint rules (#6900)
* test: enable a few jest eslint rules * more
This commit is contained in:
parent
1efc6c6091
commit
aa5a2d4c04
155 changed files with 3644 additions and 3478 deletions
|
@ -10,53 +10,57 @@ import {genChunkName, readOutputHTMLFile, generate} from '../emitUtils';
|
|||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
test('genChunkName', () => {
|
||||
const firstAssert: Record<string, string> = {
|
||||
'/docs/adding-blog': 'docs-adding-blog-062',
|
||||
'/docs/versioning': 'docs-versioning-8a8',
|
||||
'/': 'index',
|
||||
'/blog/2018/04/30/How-I-Converted-Profilo-To-Docusaurus':
|
||||
'blog-2018-04-30-how-i-converted-profilo-to-docusaurus-4f2',
|
||||
'/youtube': 'youtube-429',
|
||||
'/users/en/': 'users-en-f7a',
|
||||
'/blog': 'blog-c06',
|
||||
};
|
||||
Object.keys(firstAssert).forEach((str) => {
|
||||
expect(genChunkName(str)).toBe(firstAssert[str]);
|
||||
describe('genChunkName', () => {
|
||||
it('works', () => {
|
||||
const firstAssert: Record<string, string> = {
|
||||
'/docs/adding-blog': 'docs-adding-blog-062',
|
||||
'/docs/versioning': 'docs-versioning-8a8',
|
||||
'/': 'index',
|
||||
'/blog/2018/04/30/How-I-Converted-Profilo-To-Docusaurus':
|
||||
'blog-2018-04-30-how-i-converted-profilo-to-docusaurus-4f2',
|
||||
'/youtube': 'youtube-429',
|
||||
'/users/en/': 'users-en-f7a',
|
||||
'/blog': 'blog-c06',
|
||||
};
|
||||
Object.keys(firstAssert).forEach((str) => {
|
||||
expect(genChunkName(str)).toBe(firstAssert[str]);
|
||||
});
|
||||
});
|
||||
|
||||
// Don't allow different chunk name for same path.
|
||||
expect(genChunkName('path/is/similar', 'oldPrefix')).toEqual(
|
||||
genChunkName('path/is/similar', 'newPrefix'),
|
||||
);
|
||||
|
||||
// Even with same preferred name, still different chunk name for
|
||||
// different path
|
||||
const secondAssert: Record<string, string> = {
|
||||
'/blog/1': 'blog-85-f-089',
|
||||
'/blog/2': 'blog-353-489',
|
||||
};
|
||||
Object.keys(secondAssert).forEach((str) => {
|
||||
expect(genChunkName(str, undefined, 'blog')).toBe(secondAssert[str]);
|
||||
});
|
||||
|
||||
// Only generate short unique id
|
||||
const thirdAssert: Record<string, string> = {
|
||||
a: '0cc175b9',
|
||||
b: '92eb5ffe',
|
||||
c: '4a8a08f0',
|
||||
d: '8277e091',
|
||||
};
|
||||
Object.keys(thirdAssert).forEach((str) => {
|
||||
expect(genChunkName(str, undefined, undefined, true)).toBe(
|
||||
thirdAssert[str],
|
||||
it("doesn't allow different chunk name for same path", () => {
|
||||
expect(genChunkName('path/is/similar', 'oldPrefix')).toEqual(
|
||||
genChunkName('path/is/similar', 'newPrefix'),
|
||||
);
|
||||
});
|
||||
expect(genChunkName('d', undefined, undefined, true)).toBe('8277e091');
|
||||
|
||||
it('emits different chunk names for different paths even with same preferred name', () => {
|
||||
const secondAssert: Record<string, string> = {
|
||||
'/blog/1': 'blog-85-f-089',
|
||||
'/blog/2': 'blog-353-489',
|
||||
};
|
||||
Object.keys(secondAssert).forEach((str) => {
|
||||
expect(genChunkName(str, undefined, 'blog')).toBe(secondAssert[str]);
|
||||
});
|
||||
});
|
||||
|
||||
it('only generates short unique IDs', () => {
|
||||
const thirdAssert: Record<string, string> = {
|
||||
a: '0cc175b9',
|
||||
b: '92eb5ffe',
|
||||
c: '4a8a08f0',
|
||||
d: '8277e091',
|
||||
};
|
||||
Object.keys(thirdAssert).forEach((str) => {
|
||||
expect(genChunkName(str, undefined, undefined, true)).toBe(
|
||||
thirdAssert[str],
|
||||
);
|
||||
});
|
||||
expect(genChunkName('d', undefined, undefined, true)).toBe('8277e091');
|
||||
});
|
||||
});
|
||||
|
||||
describe('readOutputHTMLFile', () => {
|
||||
test('trailing slash undefined', async () => {
|
||||
it('trailing slash undefined', async () => {
|
||||
await expect(
|
||||
readOutputHTMLFile(
|
||||
'/file',
|
||||
|
@ -86,7 +90,7 @@ describe('readOutputHTMLFile', () => {
|
|||
).then(String),
|
||||
).resolves.toEqual('folder\n');
|
||||
});
|
||||
test('trailing slash true', async () => {
|
||||
it('trailing slash true', async () => {
|
||||
await expect(
|
||||
readOutputHTMLFile(
|
||||
'/folder',
|
||||
|
@ -102,7 +106,7 @@ describe('readOutputHTMLFile', () => {
|
|||
).then(String),
|
||||
).resolves.toEqual('folder\n');
|
||||
});
|
||||
test('trailing slash false', async () => {
|
||||
it('trailing slash false', async () => {
|
||||
await expect(
|
||||
readOutputHTMLFile(
|
||||
'/file',
|
||||
|
@ -120,36 +124,40 @@ describe('readOutputHTMLFile', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test('generate', async () => {
|
||||
describe('generate', () => {
|
||||
const writeMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
|
||||
const existsMock = jest.spyOn(fs, 'pathExists');
|
||||
const readMock = jest.spyOn(fs, 'readFile');
|
||||
|
||||
// First call: no file, no cache
|
||||
existsMock.mockImplementationOnce(() => false);
|
||||
await generate(__dirname, 'foo', 'bar');
|
||||
expect(writeMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
path.join(__dirname, 'foo'),
|
||||
'bar',
|
||||
);
|
||||
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',
|
||||
);
|
||||
});
|
||||
|
||||
// Second call: cache exists
|
||||
await generate(__dirname, 'foo', 'bar');
|
||||
expect(writeMock).toBeCalledTimes(1);
|
||||
it('works with existing cache', async () => {
|
||||
await generate(__dirname, 'foo', 'bar');
|
||||
expect(writeMock).toBeCalledTimes(1);
|
||||
});
|
||||
|
||||
// Generate another: file exists, cache doesn't
|
||||
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 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);
|
||||
});
|
||||
|
||||
// Generate again: force skip cache
|
||||
await generate(__dirname, 'foo', 'bar', true);
|
||||
expect(writeMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
path.join(__dirname, 'foo'),
|
||||
'bar',
|
||||
);
|
||||
it('works when force skipping cache', async () => {
|
||||
await generate(__dirname, 'foo', 'bar', true);
|
||||
expect(writeMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
path.join(__dirname, 'foo'),
|
||||
'bar',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue