mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 21:03:47 +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
|
@ -0,0 +1,123 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`parseLines does not parse content with metastring 1`] = `
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines does not parse content with metastring 2`] = `
|
||||
Object {
|
||||
"code": "// highlight-next-line
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines does not parse content with metastring 3`] = `
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines does not parse content with no language 1`] = `
|
||||
Object {
|
||||
"code": "// highlight-next-line
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines removes lines correctly 1`] = `
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines removes lines correctly 2`] = `
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines removes lines correctly 3`] = `
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbbbb
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
1,
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines respects language 1`] = `
|
||||
Object {
|
||||
"code": "# highlight-next-line
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines respects language 2`] = `
|
||||
Object {
|
||||
"code": "/* highlight-next-line */
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines respects language 3`] = `
|
||||
Object {
|
||||
"code": "// highlight-next-line
|
||||
aaaa
|
||||
/* highlight-next-line */
|
||||
bbbbb
|
||||
ccccc
|
||||
<!-- highlight-next-line -->
|
||||
dddd",
|
||||
"highlightLines": Array [
|
||||
4,
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`parseLines respects language 4`] = `
|
||||
Object {
|
||||
"code": "aaaa
|
||||
bbbbb
|
||||
ccccc
|
||||
dddd",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
],
|
||||
}
|
||||
`;
|
|
@ -12,45 +12,45 @@ import {
|
|||
} from '../codeBlockUtils';
|
||||
|
||||
describe('parseCodeBlockTitle', () => {
|
||||
test('should parse double quote delimited title', () => {
|
||||
it('parses double quote delimited title', () => {
|
||||
expect(parseCodeBlockTitle(`title="index.js"`)).toEqual(`index.js`);
|
||||
});
|
||||
|
||||
test('should parse single quote delimited title', () => {
|
||||
it('parses single quote delimited title', () => {
|
||||
expect(parseCodeBlockTitle(`title='index.js'`)).toEqual(`index.js`);
|
||||
});
|
||||
|
||||
test('should not parse mismatched quote delimiters', () => {
|
||||
it('does not parse mismatched quote delimiters', () => {
|
||||
expect(parseCodeBlockTitle(`title="index.js'`)).toEqual(``);
|
||||
});
|
||||
|
||||
test('should parse undefined metastring', () => {
|
||||
it('parses undefined metastring', () => {
|
||||
expect(parseCodeBlockTitle(undefined)).toEqual(``);
|
||||
});
|
||||
|
||||
test('should parse metastring with no title specified', () => {
|
||||
it('parses metastring with no title specified', () => {
|
||||
expect(parseCodeBlockTitle(`{1,2-3}`)).toEqual(``);
|
||||
});
|
||||
|
||||
test('should parse with multiple metadata title first', () => {
|
||||
it('parses with multiple metadata title first', () => {
|
||||
expect(parseCodeBlockTitle(`title="index.js" label="JavaScript"`)).toEqual(
|
||||
`index.js`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should parse with multiple metadata title last', () => {
|
||||
it('parses with multiple metadata title last', () => {
|
||||
expect(parseCodeBlockTitle(`label="JavaScript" title="index.js"`)).toEqual(
|
||||
`index.js`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should parse double quotes when delimited by single quotes', () => {
|
||||
it('parses double quotes when delimited by single quotes', () => {
|
||||
expect(parseCodeBlockTitle(`title='console.log("Hello, World!")'`)).toEqual(
|
||||
`console.log("Hello, World!")`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should parse single quotes when delimited by double quotes', () => {
|
||||
it('parses single quotes when delimited by double quotes', () => {
|
||||
expect(parseCodeBlockTitle(`title="console.log('Hello, World!')"`)).toEqual(
|
||||
`console.log('Hello, World!')`,
|
||||
);
|
||||
|
@ -58,7 +58,7 @@ describe('parseCodeBlockTitle', () => {
|
|||
});
|
||||
|
||||
describe('parseLanguage', () => {
|
||||
test('behaves correctly', () => {
|
||||
it('works', () => {
|
||||
expect(parseLanguage('language-foo xxx yyy')).toEqual('foo');
|
||||
expect(parseLanguage('xxxxx language-foo yyy')).toEqual('foo');
|
||||
expect(parseLanguage('xx-language-foo yyyy')).toBeUndefined();
|
||||
|
@ -67,16 +67,8 @@ describe('parseLanguage', () => {
|
|||
});
|
||||
|
||||
describe('parseLines', () => {
|
||||
test('does not parse content with metastring', () => {
|
||||
expect(parseLines('aaaaa\nbbbbb', '{1}', 'js')).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`);
|
||||
it('does not parse content with metastring', () => {
|
||||
expect(parseLines('aaaaa\nbbbbb', '{1}', 'js')).toMatchSnapshot();
|
||||
expect(
|
||||
parseLines(
|
||||
`// highlight-next-line
|
||||
|
@ -85,33 +77,16 @@ bbbbb`,
|
|||
'{1}',
|
||||
'js',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "// highlight-next-line
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
parseLines(
|
||||
`aaaaa
|
||||
bbbbb`,
|
||||
'{1}',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
test('does not parse content with no language', () => {
|
||||
it('does not parse content with no language', () => {
|
||||
expect(
|
||||
parseLines(
|
||||
`// highlight-next-line
|
||||
|
@ -120,16 +95,9 @@ bbbbb`,
|
|||
'',
|
||||
undefined,
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "// highlight-next-line
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
test('removes lines correctly', () => {
|
||||
it('removes lines correctly', () => {
|
||||
expect(
|
||||
parseLines(
|
||||
`// highlight-next-line
|
||||
|
@ -138,15 +106,7 @@ bbbbb`,
|
|||
'',
|
||||
'js',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
parseLines(
|
||||
`// highlight-start
|
||||
|
@ -156,15 +116,7 @@ bbbbb`,
|
|||
'',
|
||||
'js',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
parseLines(
|
||||
`// highlight-start
|
||||
|
@ -177,21 +129,9 @@ bbbbb`,
|
|||
'',
|
||||
'js',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "aaaaa
|
||||
bbbbbbb
|
||||
bbbbb",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
1,
|
||||
],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
test('respects language', () => {
|
||||
it('respects language', () => {
|
||||
expect(
|
||||
parseLines(
|
||||
`# highlight-next-line
|
||||
|
@ -200,14 +140,7 @@ bbbbb`,
|
|||
'',
|
||||
'js',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "# highlight-next-line
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
parseLines(
|
||||
`/* highlight-next-line */
|
||||
|
@ -216,14 +149,7 @@ bbbbb`,
|
|||
'',
|
||||
'py',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "/* highlight-next-line */
|
||||
aaaaa
|
||||
bbbbb",
|
||||
"highlightLines": Array [],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
parseLines(
|
||||
`// highlight-next-line
|
||||
|
@ -237,20 +163,7 @@ dddd`,
|
|||
'',
|
||||
'py',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "// highlight-next-line
|
||||
aaaa
|
||||
/* highlight-next-line */
|
||||
bbbbb
|
||||
ccccc
|
||||
<!-- highlight-next-line -->
|
||||
dddd",
|
||||
"highlightLines": Array [
|
||||
4,
|
||||
],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
parseLines(
|
||||
`// highlight-next-line
|
||||
|
@ -264,19 +177,6 @@ dddd`,
|
|||
'',
|
||||
'',
|
||||
),
|
||||
).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"code": "aaaa
|
||||
bbbbb
|
||||
ccccc
|
||||
dddd",
|
||||
"highlightLines": Array [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
],
|
||||
}
|
||||
`);
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -65,365 +65,353 @@ function testVersion(data?: Partial<PropVersionMetadata>): PropVersionMetadata {
|
|||
};
|
||||
}
|
||||
|
||||
describe('docsUtils', () => {
|
||||
describe('useDocsVersion', () => {
|
||||
test('should throw if context provider is missing', () => {
|
||||
expect(
|
||||
() => renderHook(() => useDocsVersion()).result.current,
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Hook useDocsVersion is called outside the <DocsVersionProvider>. "`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should read value from context provider', () => {
|
||||
const version = testVersion();
|
||||
const {result} = renderHook(() => useDocsVersion(), {
|
||||
wrapper: ({children}) => (
|
||||
<DocsVersionProvider version={version}>
|
||||
{children}
|
||||
</DocsVersionProvider>
|
||||
),
|
||||
});
|
||||
expect(result.current).toBe(version);
|
||||
});
|
||||
describe('useDocsVersion', () => {
|
||||
it('throws if context provider is missing', () => {
|
||||
expect(
|
||||
() => renderHook(() => useDocsVersion()).result.current,
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Hook useDocsVersion is called outside the <DocsVersionProvider>. "`,
|
||||
);
|
||||
});
|
||||
|
||||
describe('useDocsSidebar', () => {
|
||||
test('should throw if context provider is missing', () => {
|
||||
expect(
|
||||
() => renderHook(() => useDocsSidebar()).result.current,
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Hook useDocsSidebar is called outside the <DocsSidebarProvider>. "`,
|
||||
);
|
||||
it('reads value from context provider', () => {
|
||||
const version = testVersion();
|
||||
const {result} = renderHook(() => useDocsVersion(), {
|
||||
wrapper: ({children}) => (
|
||||
<DocsVersionProvider version={version}>{children}</DocsVersionProvider>
|
||||
),
|
||||
});
|
||||
expect(result.current).toBe(version);
|
||||
});
|
||||
});
|
||||
|
||||
test('should read value from context provider', () => {
|
||||
const sidebar: PropSidebar = [];
|
||||
const {result} = renderHook(() => useDocsSidebar(), {
|
||||
wrapper: ({children}) => (
|
||||
<DocsSidebarProvider sidebar={sidebar}>
|
||||
{children}
|
||||
</DocsSidebarProvider>
|
||||
),
|
||||
});
|
||||
expect(result.current).toBe(sidebar);
|
||||
});
|
||||
describe('useDocsSidebar', () => {
|
||||
it('throws if context provider is missing', () => {
|
||||
expect(
|
||||
() => renderHook(() => useDocsSidebar()).result.current,
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Hook useDocsSidebar is called outside the <DocsSidebarProvider>. "`,
|
||||
);
|
||||
});
|
||||
|
||||
describe('useDocById', () => {
|
||||
const version = testVersion({
|
||||
docs: {
|
||||
doc1: {
|
||||
id: 'doc1',
|
||||
title: 'Doc 1',
|
||||
description: 'desc1',
|
||||
sidebar: 'sidebar1',
|
||||
},
|
||||
doc2: {
|
||||
id: 'doc2',
|
||||
title: 'Doc 2',
|
||||
description: 'desc2',
|
||||
sidebar: 'sidebar2',
|
||||
},
|
||||
it('reads value from context provider', () => {
|
||||
const sidebar: PropSidebar = [];
|
||||
const {result} = renderHook(() => useDocsSidebar(), {
|
||||
wrapper: ({children}) => (
|
||||
<DocsSidebarProvider sidebar={sidebar}>{children}</DocsSidebarProvider>
|
||||
),
|
||||
});
|
||||
expect(result.current).toBe(sidebar);
|
||||
});
|
||||
});
|
||||
|
||||
describe('useDocById', () => {
|
||||
const version = testVersion({
|
||||
docs: {
|
||||
doc1: {
|
||||
id: 'doc1',
|
||||
title: 'Doc 1',
|
||||
description: 'desc1',
|
||||
sidebar: 'sidebar1',
|
||||
},
|
||||
});
|
||||
|
||||
function callHook(docId: string | undefined) {
|
||||
const {result} = renderHook(() => useDocById(docId), {
|
||||
wrapper: ({children}) => (
|
||||
<DocsVersionProvider version={version}>
|
||||
{children}
|
||||
</DocsVersionProvider>
|
||||
),
|
||||
});
|
||||
return result.current;
|
||||
}
|
||||
|
||||
test('should accept undefined', () => {
|
||||
expect(callHook(undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
test('should find doc1', () => {
|
||||
expect(callHook('doc1')).toMatchObject({id: 'doc1'});
|
||||
});
|
||||
test('should find doc2', () => {
|
||||
expect(callHook('doc2')).toMatchObject({id: 'doc2'});
|
||||
});
|
||||
|
||||
test('should throw for doc3', () => {
|
||||
expect(() => callHook('doc3')).toThrowErrorMatchingInlineSnapshot(
|
||||
`"no version doc found by id=doc3"`,
|
||||
);
|
||||
});
|
||||
doc2: {
|
||||
id: 'doc2',
|
||||
title: 'Doc 2',
|
||||
description: 'desc2',
|
||||
sidebar: 'sidebar2',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
describe('findSidebarCategory', () => {
|
||||
test('should be able to return undefined', () => {
|
||||
expect(findSidebarCategory([], () => false)).toBeUndefined();
|
||||
expect(
|
||||
findSidebarCategory([testCategory(), testCategory()], () => false),
|
||||
).toBeUndefined();
|
||||
function callHook(docId: string | undefined) {
|
||||
const {result} = renderHook(() => useDocById(docId), {
|
||||
wrapper: ({children}) => (
|
||||
<DocsVersionProvider version={version}>{children}</DocsVersionProvider>
|
||||
),
|
||||
});
|
||||
return result.current;
|
||||
}
|
||||
|
||||
test('should return first element matching predicate', () => {
|
||||
const first = testCategory();
|
||||
const second = testCategory();
|
||||
const third = testCategory();
|
||||
const sidebar = [first, second, third];
|
||||
expect(findSidebarCategory(sidebar, () => true)).toEqual(first);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === first)).toEqual(
|
||||
first,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === second)).toEqual(
|
||||
second,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === third)).toEqual(
|
||||
third,
|
||||
);
|
||||
});
|
||||
|
||||
test('should be able to search in sub items', () => {
|
||||
const subsub1 = testCategory();
|
||||
const subsub2 = testCategory();
|
||||
const sub1 = testCategory({
|
||||
items: [subsub1, subsub2],
|
||||
});
|
||||
const sub2 = testCategory();
|
||||
const parent = testCategory({
|
||||
items: [sub1, sub2],
|
||||
});
|
||||
const sidebar = [parent];
|
||||
|
||||
expect(findSidebarCategory(sidebar, () => true)).toEqual(parent);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === sub1)).toEqual(
|
||||
sub1,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === sub2)).toEqual(
|
||||
sub2,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === subsub1)).toEqual(
|
||||
subsub1,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === subsub2)).toEqual(
|
||||
subsub2,
|
||||
);
|
||||
});
|
||||
it('accepts undefined', () => {
|
||||
expect(callHook(undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
describe('findFirstCategoryLink', () => {
|
||||
test('category without link nor child', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: undefined,
|
||||
}),
|
||||
),
|
||||
).toEqual(undefined);
|
||||
});
|
||||
|
||||
test('category with link', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: '/itemPath',
|
||||
}),
|
||||
),
|
||||
).toEqual('/itemPath');
|
||||
});
|
||||
|
||||
test('category with deeply nested category link', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [
|
||||
{type: 'html', value: '<p>test1</p>'},
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [
|
||||
{type: 'html', value: '<p>test2</p>'},
|
||||
testCategory({
|
||||
href: '/itemPath',
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
),
|
||||
).toEqual('/itemPath');
|
||||
});
|
||||
|
||||
test('category with deeply nested link', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [{type: 'link', href: '/itemPath', label: 'Label'}],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
),
|
||||
).toEqual('/itemPath');
|
||||
});
|
||||
it('finds doc1', () => {
|
||||
expect(callHook('doc1')).toMatchObject({id: 'doc1'});
|
||||
});
|
||||
it('finds doc2', () => {
|
||||
expect(callHook('doc2')).toMatchObject({id: 'doc2'});
|
||||
});
|
||||
|
||||
describe('isActiveSidebarItem', () => {
|
||||
test('with link href', () => {
|
||||
const item: PropSidebarItem = {
|
||||
type: 'link',
|
||||
href: '/itemPath',
|
||||
label: 'Label',
|
||||
};
|
||||
it('throws for doc3', () => {
|
||||
expect(() => callHook('doc3')).toThrowErrorMatchingInlineSnapshot(
|
||||
`"no version doc found by id=doc3"`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
expect(isActiveSidebarItem(item, '/unexistingPath')).toEqual(false);
|
||||
|
||||
expect(isActiveSidebarItem(item, '/itemPath')).toEqual(true);
|
||||
|
||||
// Ensure it's not trailing slash sensitive:
|
||||
expect(isActiveSidebarItem(item, '/itemPath/')).toEqual(true);
|
||||
expect(
|
||||
isActiveSidebarItem({...item, href: '/itemPath/'}, '/itemPath'),
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
test('with category href', () => {
|
||||
const item: PropSidebarItem = testCategory({
|
||||
href: '/itemPath',
|
||||
});
|
||||
|
||||
expect(isActiveSidebarItem(item, '/unexistingPath')).toEqual(false);
|
||||
|
||||
expect(isActiveSidebarItem(item, '/itemPath')).toEqual(true);
|
||||
|
||||
// Ensure it's not trailing slash sensitive:
|
||||
expect(isActiveSidebarItem(item, '/itemPath/')).toEqual(true);
|
||||
expect(
|
||||
isActiveSidebarItem({...item, href: '/itemPath/'}, '/itemPath'),
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
test('with category nested items', () => {
|
||||
const item: PropSidebarItem = testCategory({
|
||||
href: '/category-path',
|
||||
items: [
|
||||
{
|
||||
type: 'link',
|
||||
href: '/sub-link-path',
|
||||
label: 'Label',
|
||||
},
|
||||
testCategory({
|
||||
href: '/sub-category-path',
|
||||
items: [
|
||||
{
|
||||
type: 'link',
|
||||
href: '/sub-sub-link-path',
|
||||
label: 'Label',
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
expect(isActiveSidebarItem(item, '/unexistingPath')).toEqual(false);
|
||||
|
||||
expect(isActiveSidebarItem(item, '/category-path')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-link-path')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-category-path')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-sub-link-path')).toEqual(true);
|
||||
|
||||
// Ensure it's not trailing slash sensitive:
|
||||
expect(isActiveSidebarItem(item, '/category-path/')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-link-path/')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-category-path/')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-sub-link-path/')).toEqual(true);
|
||||
});
|
||||
describe('findSidebarCategory', () => {
|
||||
it('os able to return undefined', () => {
|
||||
expect(findSidebarCategory([], () => false)).toBeUndefined();
|
||||
expect(
|
||||
findSidebarCategory([testCategory(), testCategory()], () => false),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
describe('getBreadcrumbs', () => {
|
||||
test('should return empty for empty sidebar', () => {
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar: [],
|
||||
pathname: '/doesNotExist',
|
||||
it('returns first element matching predicate', () => {
|
||||
const first = testCategory();
|
||||
const second = testCategory();
|
||||
const third = testCategory();
|
||||
const sidebar = [first, second, third];
|
||||
expect(findSidebarCategory(sidebar, () => true)).toEqual(first);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === first)).toEqual(
|
||||
first,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === second)).toEqual(
|
||||
second,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === third)).toEqual(
|
||||
third,
|
||||
);
|
||||
});
|
||||
|
||||
it('is able to search in sub items', () => {
|
||||
const subsub1 = testCategory();
|
||||
const subsub2 = testCategory();
|
||||
const sub1 = testCategory({
|
||||
items: [subsub1, subsub2],
|
||||
});
|
||||
const sub2 = testCategory();
|
||||
const parent = testCategory({
|
||||
items: [sub1, sub2],
|
||||
});
|
||||
const sidebar = [parent];
|
||||
|
||||
expect(findSidebarCategory(sidebar, () => true)).toEqual(parent);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === sub1)).toEqual(sub1);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === sub2)).toEqual(sub2);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === subsub1)).toEqual(
|
||||
subsub1,
|
||||
);
|
||||
expect(findSidebarCategory(sidebar, (item) => item === subsub2)).toEqual(
|
||||
subsub2,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findFirstCategoryLink', () => {
|
||||
it('works with category without link nor child', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: undefined,
|
||||
}),
|
||||
).toEqual([]);
|
||||
),
|
||||
).toEqual(undefined);
|
||||
});
|
||||
|
||||
it('works with category with link', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: '/itemPath',
|
||||
}),
|
||||
),
|
||||
).toEqual('/itemPath');
|
||||
});
|
||||
|
||||
it('works with category with deeply nested category link', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [
|
||||
{type: 'html', value: '<p>test1</p>'},
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [
|
||||
{type: 'html', value: '<p>test2</p>'},
|
||||
testCategory({
|
||||
href: '/itemPath',
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
),
|
||||
).toEqual('/itemPath');
|
||||
});
|
||||
|
||||
it('works with category with deeply nested link', () => {
|
||||
expect(
|
||||
findFirstCategoryLink(
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [
|
||||
testCategory({
|
||||
href: undefined,
|
||||
items: [{type: 'link', href: '/itemPath', label: 'Label'}],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
),
|
||||
).toEqual('/itemPath');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isActiveSidebarItem', () => {
|
||||
it('works with link href', () => {
|
||||
const item: PropSidebarItem = {
|
||||
type: 'link',
|
||||
href: '/itemPath',
|
||||
label: 'Label',
|
||||
};
|
||||
|
||||
expect(isActiveSidebarItem(item, '/unexistingPath')).toEqual(false);
|
||||
|
||||
expect(isActiveSidebarItem(item, '/itemPath')).toEqual(true);
|
||||
|
||||
// Ensure it's not trailing slash sensitive:
|
||||
expect(isActiveSidebarItem(item, '/itemPath/')).toEqual(true);
|
||||
expect(
|
||||
isActiveSidebarItem({...item, href: '/itemPath/'}, '/itemPath'),
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('works with category href', () => {
|
||||
const item: PropSidebarItem = testCategory({
|
||||
href: '/itemPath',
|
||||
});
|
||||
|
||||
test('should return empty for sidebar but unknown pathname', () => {
|
||||
const sidebar = [testCategory(), testLink()];
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname: '/doesNotExist',
|
||||
expect(isActiveSidebarItem(item, '/unexistingPath')).toEqual(false);
|
||||
|
||||
expect(isActiveSidebarItem(item, '/itemPath')).toEqual(true);
|
||||
|
||||
// Ensure it's not trailing slash sensitive:
|
||||
expect(isActiveSidebarItem(item, '/itemPath/')).toEqual(true);
|
||||
expect(
|
||||
isActiveSidebarItem({...item, href: '/itemPath/'}, '/itemPath'),
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('works with category nested items', () => {
|
||||
const item: PropSidebarItem = testCategory({
|
||||
href: '/category-path',
|
||||
items: [
|
||||
{
|
||||
type: 'link',
|
||||
href: '/sub-link-path',
|
||||
label: 'Label',
|
||||
},
|
||||
testCategory({
|
||||
href: '/sub-category-path',
|
||||
items: [
|
||||
{
|
||||
type: 'link',
|
||||
href: '/sub-sub-link-path',
|
||||
label: 'Label',
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toEqual([]);
|
||||
],
|
||||
});
|
||||
|
||||
test('should return first level category', () => {
|
||||
const pathname = '/somePathName';
|
||||
const sidebar = [testCategory({href: pathname}), testLink()];
|
||||
expect(isActiveSidebarItem(item, '/unexistingPath')).toEqual(false);
|
||||
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname,
|
||||
}),
|
||||
).toEqual([sidebar[0]]);
|
||||
expect(isActiveSidebarItem(item, '/category-path')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-link-path')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-category-path')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-sub-link-path')).toEqual(true);
|
||||
|
||||
// Ensure it's not trailing slash sensitive:
|
||||
expect(isActiveSidebarItem(item, '/category-path/')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-link-path/')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-category-path/')).toEqual(true);
|
||||
expect(isActiveSidebarItem(item, '/sub-sub-link-path/')).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getBreadcrumbs', () => {
|
||||
it('returns empty for empty sidebar', () => {
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar: [],
|
||||
pathname: '/doesNotExist',
|
||||
}),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns empty for sidebar but unknown pathname', () => {
|
||||
const sidebar = [testCategory(), testLink()];
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname: '/doesNotExist',
|
||||
}),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns first level category', () => {
|
||||
const pathname = '/somePathName';
|
||||
const sidebar = [testCategory({href: pathname}), testLink()];
|
||||
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname,
|
||||
}),
|
||||
).toEqual([sidebar[0]]);
|
||||
});
|
||||
|
||||
it('returns first level link', () => {
|
||||
const pathname = '/somePathName';
|
||||
const sidebar = [testCategory(), testLink({href: pathname})];
|
||||
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname,
|
||||
}),
|
||||
).toEqual([sidebar[1]]);
|
||||
});
|
||||
|
||||
it('returns nested category', () => {
|
||||
const pathname = '/somePathName';
|
||||
|
||||
const categoryLevel3 = testCategory({
|
||||
href: pathname,
|
||||
});
|
||||
|
||||
test('should return first level link', () => {
|
||||
const pathname = '/somePathName';
|
||||
const sidebar = [testCategory(), testLink({href: pathname})];
|
||||
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname,
|
||||
}),
|
||||
).toEqual([sidebar[1]]);
|
||||
});
|
||||
|
||||
test('should return nested category', () => {
|
||||
const pathname = '/somePathName';
|
||||
|
||||
const categoryLevel3 = testCategory({
|
||||
href: pathname,
|
||||
});
|
||||
|
||||
const categoryLevel2 = testCategory({
|
||||
items: [
|
||||
testCategory(),
|
||||
categoryLevel3,
|
||||
testLink({href: pathname}),
|
||||
testLink(),
|
||||
],
|
||||
});
|
||||
|
||||
const categoryLevel1 = testCategory({
|
||||
items: [testLink(), categoryLevel2],
|
||||
});
|
||||
|
||||
const sidebar = [
|
||||
testLink(),
|
||||
const categoryLevel2 = testCategory({
|
||||
items: [
|
||||
testCategory(),
|
||||
categoryLevel1,
|
||||
categoryLevel3,
|
||||
testLink({href: pathname}),
|
||||
testLink(),
|
||||
testCategory(),
|
||||
];
|
||||
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname,
|
||||
}),
|
||||
).toEqual([categoryLevel1, categoryLevel2, categoryLevel3]);
|
||||
],
|
||||
});
|
||||
|
||||
const categoryLevel1 = testCategory({
|
||||
items: [testLink(), categoryLevel2],
|
||||
});
|
||||
|
||||
const sidebar = [
|
||||
testLink(),
|
||||
testCategory(),
|
||||
categoryLevel1,
|
||||
testLink(),
|
||||
testCategory(),
|
||||
];
|
||||
|
||||
expect(
|
||||
getBreadcrumbs({
|
||||
sidebar,
|
||||
pathname,
|
||||
}),
|
||||
).toEqual([categoryLevel1, categoryLevel2, categoryLevel3]);
|
||||
});
|
||||
|
||||
test('should return nested link', () => {
|
||||
it('returns nested link', () => {
|
||||
const pathname = '/somePathName';
|
||||
|
||||
const link = testLink({href: pathname});
|
||||
|
|
|
@ -8,13 +8,13 @@
|
|||
import {uniq, duplicates} from '../jsUtils';
|
||||
|
||||
describe('duplicates', () => {
|
||||
test('gets duplicate values', () => {
|
||||
it('gets duplicate values', () => {
|
||||
expect(duplicates(['a', 'b', 'c', 'd'])).toEqual([]);
|
||||
expect(duplicates(['a', 'b', 'b', 'b'])).toEqual(['b', 'b']);
|
||||
expect(duplicates(['c', 'b', 'b', 'c'])).toEqual(['b', 'c']);
|
||||
expect(duplicates([{a: 1}, {a: 1}, {a: 1}])).toEqual([]);
|
||||
});
|
||||
test('accepts custom comparator', () => {
|
||||
it('accepts custom comparator', () => {
|
||||
expect(duplicates([{a: 1}, {a: 1}, {a: 1}], (a, b) => a.a === b.a)).toEqual(
|
||||
[{a: 1}, {a: 1}],
|
||||
);
|
||||
|
@ -28,7 +28,7 @@ describe('duplicates', () => {
|
|||
});
|
||||
|
||||
describe('uniq', () => {
|
||||
test('remove duplicate primitives', () => {
|
||||
it('remove duplicate primitives', () => {
|
||||
expect(uniq(['A', 'B', 'C', 'B', 'A', 'D'])).toEqual(['A', 'B', 'C', 'D']);
|
||||
expect(uniq([3, 3, 5, 1, 6, 3, 5])).toEqual([3, 5, 1, 6]);
|
||||
expect(uniq([null, undefined, 3, null, 4, 3])).toEqual([
|
||||
|
@ -39,7 +39,7 @@ describe('uniq', () => {
|
|||
]);
|
||||
});
|
||||
|
||||
test('remove duplicate objects/arrays by identity', () => {
|
||||
it('remove duplicate objects/arrays by identity', () => {
|
||||
const obj1 = {};
|
||||
const obj2 = {};
|
||||
const obj3 = {};
|
||||
|
|
|
@ -8,32 +8,32 @@
|
|||
import {isSamePath} from '../pathUtils';
|
||||
|
||||
describe('isSamePath', () => {
|
||||
test('should be true for compared path without trailing slash', () => {
|
||||
it('returns true for compared path without trailing slash', () => {
|
||||
expect(isSamePath('/docs', '/docs')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should be true for compared path with trailing slash', () => {
|
||||
it('returns true for compared path with trailing slash', () => {
|
||||
expect(isSamePath('/docs', '/docs/')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should be true for compared path with different case', () => {
|
||||
it('returns true for compared path with different case', () => {
|
||||
expect(isSamePath('/doCS', '/DOcs')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should be true for compared path with different case + trailing slash', () => {
|
||||
it('returns true for compared path with different case + trailing slash', () => {
|
||||
expect(isSamePath('/doCS', '/DOcs/')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should be false for compared path with double trailing slash', () => {
|
||||
it('returns false for compared path with double trailing slash', () => {
|
||||
expect(isSamePath('/docs', '/docs//')).toBeFalsy();
|
||||
});
|
||||
|
||||
test('should be true for twice undefined/null', () => {
|
||||
it('returns true for twice undefined/null', () => {
|
||||
expect(isSamePath(undefined, undefined)).toBeTruthy();
|
||||
expect(isSamePath(undefined, undefined)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should be false when one undefined', () => {
|
||||
it('returns false when one undefined', () => {
|
||||
expect(isSamePath('/docs', undefined)).toBeFalsy();
|
||||
expect(isSamePath(undefined, '/docs')).toBeFalsy();
|
||||
});
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import {isRegexpStringMatch} from '../regexpUtils';
|
||||
|
||||
describe('isRegexpStringMatch', () => {
|
||||
test('behaves correctly', () => {
|
||||
it('works', () => {
|
||||
expect(isRegexpStringMatch(undefined, 'foo')).toEqual(false);
|
||||
expect(isRegexpStringMatch('bar', undefined)).toEqual(false);
|
||||
expect(isRegexpStringMatch('foo', 'bar')).toEqual(false);
|
||||
|
|
|
@ -8,17 +8,17 @@
|
|||
import {type Route} from '@generated/routes';
|
||||
import {findHomePageRoute} from '../routesUtils';
|
||||
|
||||
describe('routesUtils findHomePageRoute', () => {
|
||||
describe('findHomePageRoute', () => {
|
||||
const homePage: Route = {
|
||||
path: '/',
|
||||
exact: true,
|
||||
};
|
||||
|
||||
test('should return undefined for no routes', () => {
|
||||
it('returns undefined for no routes', () => {
|
||||
expect(findHomePageRoute({baseUrl: '/', routes: []})).toEqual(undefined);
|
||||
});
|
||||
|
||||
test('should return undefined for no homepage', () => {
|
||||
it('returns undefined for no homepage', () => {
|
||||
expect(
|
||||
findHomePageRoute({
|
||||
baseUrl: '/',
|
||||
|
@ -40,7 +40,7 @@ describe('routesUtils findHomePageRoute', () => {
|
|||
).toEqual(undefined);
|
||||
});
|
||||
|
||||
test('should find top-level homepage', () => {
|
||||
it('finds top-level homepage', () => {
|
||||
expect(
|
||||
findHomePageRoute({
|
||||
baseUrl: '/',
|
||||
|
@ -56,7 +56,7 @@ describe('routesUtils findHomePageRoute', () => {
|
|||
).toEqual(homePage);
|
||||
});
|
||||
|
||||
test('should find nested homepage', () => {
|
||||
it('finds nested homepage', () => {
|
||||
expect(
|
||||
findHomePageRoute({
|
||||
baseUrl: '/',
|
||||
|
@ -80,7 +80,7 @@ describe('routesUtils findHomePageRoute', () => {
|
|||
).toEqual(homePage);
|
||||
});
|
||||
|
||||
test('should find nested homepage with baseUrl', () => {
|
||||
it('finds nested homepage with baseUrl', () => {
|
||||
const baseUrl = '/baseUrl/';
|
||||
const baseUrlHomePage = {...homePage, path: baseUrl};
|
||||
expect(
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import {docVersionSearchTag} from '../searchUtils';
|
||||
|
||||
describe('docVersionSearchTag', () => {
|
||||
test('behaves correctly', () => {
|
||||
it('works', () => {
|
||||
expect(docVersionSearchTag('foo', 'bar')).toEqual('docs-foo-bar');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -13,7 +13,7 @@ describe('listTagsByLetters', () => {
|
|||
type Tag = Param[number];
|
||||
type Result = ReturnType<typeof listTagsByLetters>;
|
||||
|
||||
test('Should create letters list', () => {
|
||||
it('creates letters list', () => {
|
||||
const tag1: Tag = {
|
||||
name: 'tag1',
|
||||
permalink: '/tag1',
|
||||
|
|
|
@ -10,7 +10,7 @@ import {renderHook} from '@testing-library/react-hooks';
|
|||
import {useFilteredAndTreeifiedTOC} from '../tocUtils';
|
||||
|
||||
describe('useFilteredAndTreeifiedTOC', () => {
|
||||
test('filter a toc with all heading levels', () => {
|
||||
it('filters a toc with all heading levels', () => {
|
||||
const toc: TOCItem[] = [
|
||||
{
|
||||
id: 'alpha',
|
||||
|
@ -137,7 +137,7 @@ describe('useFilteredAndTreeifiedTOC', () => {
|
|||
// It's not 100% clear exactly how the TOC should behave under weird heading
|
||||
// levels provided by the user. Adding a test so that behavior stays the same
|
||||
// over time
|
||||
test('filter invalid heading levels (but possible) TOC', () => {
|
||||
it('filters invalid heading levels (but possible) TOC', () => {
|
||||
const toc: TOCItem[] = [
|
||||
{
|
||||
id: 'charlie',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue