feat(docs,theme-classic): docs breadcrumbs (#6517)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
Jody Heavener 2022-02-16 14:02:58 -04:00 committed by GitHub
parent 67918e35e2
commit 3629b5ab39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 341 additions and 1 deletions

View file

@ -16,11 +16,13 @@ import {
useDocsSidebar,
DocsSidebarProvider,
findSidebarCategory,
getBreadcrumbs,
} from '../docsUtils';
import type {
PropSidebar,
PropSidebarItem,
PropSidebarItemCategory,
PropSidebarItemLink,
PropVersionMetadata,
} from '@docusaurus/plugin-content-docs';
@ -39,6 +41,15 @@ function testCategory(
};
}
function testLink(data?: Partial<PropSidebarItemLink>): PropSidebarItemLink {
return {
type: 'link',
href: '/testLinkHref',
label: 'Link label',
...data,
};
}
function testVersion(data?: Partial<PropVersionMetadata>): PropVersionMetadata {
return {
version: 'versionName',
@ -330,4 +341,123 @@ describe('docsUtils', () => {
expect(isActiveSidebarItem(item, '/sub-sub-link-path/')).toEqual(true);
});
});
describe('getBreadcrumbs', () => {
test('should return empty for empty sidebar', () => {
expect(
getBreadcrumbs({
sidebar: [],
pathname: '/doesNotExist',
}),
).toEqual([]);
});
test('should return empty for sidebar but unknown pathname', () => {
const sidebar = [testCategory(), testLink()];
expect(
getBreadcrumbs({
sidebar,
pathname: '/doesNotExist',
}),
).toEqual([]);
});
test('should return first level category', () => {
const pathname = '/somePathName';
const sidebar = [testCategory({href: pathname}), testLink()];
expect(
getBreadcrumbs({
sidebar,
pathname,
}),
).toEqual([sidebar[0]]);
});
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(),
testCategory(),
categoryLevel1,
testLink(),
testCategory(),
];
expect(
getBreadcrumbs({
sidebar,
pathname,
}),
).toEqual([categoryLevel1, categoryLevel2, categoryLevel3]);
});
});
test('should return nested link', () => {
const pathname = '/somePathName';
const link = testLink({href: pathname});
const categoryLevel3 = testCategory({
items: [testLink(), link, testLink()],
});
const categoryLevel2 = testCategory({
items: [
testCategory(),
categoryLevel3,
testLink({href: pathname}),
testLink(),
],
});
const categoryLevel1 = testCategory({
items: [testLink(), categoryLevel2],
});
const sidebar = [
testLink(),
testCategory(),
categoryLevel1,
testLink(),
testCategory(),
];
expect(
getBreadcrumbs({
sidebar,
pathname,
}),
).toEqual([categoryLevel1, categoryLevel2, categoryLevel3, link]);
});
});