From 80fe374f74365de6e17dbdcd387b6463388b6821 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 26 Oct 2022 02:15:20 -0400 Subject: [PATCH] Fix tests --- .../__snapshots__/index.test.ts.snap | 373 +++++++++--------- .../src/__tests__/index.test.ts | 63 ++- .../src/docs.ts | 3 +- 3 files changed, 229 insertions(+), 210 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index 5d26371f8d..432946d60c 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -44,118 +44,6 @@ Available document ids are: `; exports[`simple website content 1`] = ` -{ - "description": "Images", - "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, - "frontMatter": { - "id": "baz", - "pagination_label": "baz pagination_label", - "slug": "bazSlug.html", - "tags": [ - "tag 1", - "tag-1", - { - "label": "tag 2", - "permalink": "tag2-custom-permalink", - }, - ], - "title": "baz", - }, - "id": "foo/baz", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, - "permalink": "/docs/foo/bazSlug.html", - "sidebarPosition": undefined, - "slug": "/foo/bazSlug.html", - "source": "@site/docs/foo/baz.md", - "sourceDirName": "foo", - "tags": [ - { - "label": "tag 1", - "permalink": "/docs/tags/tag-1", - }, - { - "label": "tag 2", - "permalink": "/docs/tags/tag2-custom-permalink", - }, - ], - "title": "baz", - "unversionedId": "foo/baz", - "version": "current", -} -`; - -exports[`simple website content 2`] = ` -{ - "description": "Hi, Endilie here :)", - "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, - "frontMatter": { - "id": "hello", - "sidebar_label": "Hello sidebar_label", - "slug": "/", - "tags": [ - "tag-1", - "tag 3", - ], - "title": "Hello, World !", - }, - "id": "hello", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, - "permalink": "/docs/", - "sidebarPosition": undefined, - "slug": "/", - "source": "@site/docs/hello.md", - "sourceDirName": ".", - "tags": [ - { - "label": "tag-1", - "permalink": "/docs/tags/tag-1", - }, - { - "label": "tag 3", - "permalink": "/docs/tags/tag-3", - }, - ], - "title": "Hello, World !", - "unversionedId": "hello", - "version": "current", -} -`; - -exports[`simple website content 3`] = ` -{ - "description": "This is custom description", - "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, - "frontMatter": { - "description": "This is custom description", - "id": "bar", - "pagination_next": null, - "pagination_prev": null, - "title": "Bar", - }, - "id": "foo/bar", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, - "permalink": "/docs/foo/bar", - "sidebarPosition": undefined, - "slug": "/foo/bar", - "source": "@site/docs/foo/bar.md", - "sourceDirName": "foo", - "tags": [], - "title": "Bar", - "unversionedId": "foo/bar", - "version": "current", -} -`; - -exports[`simple website content 4`] = ` { "docs": [ { @@ -243,6 +131,118 @@ exports[`simple website content 4`] = ` } `; +exports[`simple website content 2`] = ` +{ + "description": "Images", + "draft": false, + "frontMatter": { + "id": "baz", + "pagination_label": "baz pagination_label", + "slug": "bazSlug.html", + "tags": [ + "tag 1", + "tag-1", + { + "label": "tag 2", + "permalink": "tag2-custom-permalink", + }, + ], + "title": "baz", + }, + "id": "foo/baz", + "next": { + "permalink": "/docs/category/slugs", + "title": "Slugs", + }, + "permalink": "/docs/foo/bazSlug.html", + "previous": { + "permalink": "/docs/foo/bar", + "title": "Bar", + }, + "sidebar": "docs", + "slug": "/foo/bazSlug.html", + "source": "@site/docs/foo/baz.md", + "sourceDirName": "foo", + "tags": [ + { + "label": "tag 1", + "permalink": "/docs/tags/tag-1", + }, + { + "label": "tag 2", + "permalink": "/docs/tags/tag2-custom-permalink", + }, + ], + "title": "baz", + "unversionedId": "foo/baz", + "version": "current", +} +`; + +exports[`simple website content 3`] = ` +{ + "description": "Hi, Endilie here :)", + "draft": false, + "frontMatter": { + "id": "hello", + "sidebar_label": "Hello sidebar_label", + "slug": "/", + "tags": [ + "tag-1", + "tag 3", + ], + "title": "Hello, World !", + }, + "id": "hello", + "permalink": "/docs/", + "previous": { + "permalink": "/docs/headingAsTitle", + "title": "My heading as title", + }, + "sidebar": "docs", + "slug": "/", + "source": "@site/docs/hello.md", + "sourceDirName": ".", + "tags": [ + { + "label": "tag-1", + "permalink": "/docs/tags/tag-1", + }, + { + "label": "tag 3", + "permalink": "/docs/tags/tag-3", + }, + ], + "title": "Hello, World !", + "unversionedId": "hello", + "version": "current", +} +`; + +exports[`simple website content 4`] = ` +{ + "description": "This is custom description", + "draft": false, + "frontMatter": { + "description": "This is custom description", + "id": "bar", + "pagination_next": null, + "pagination_prev": null, + "title": "Bar", + }, + "id": "foo/bar", + "permalink": "/docs/foo/bar", + "sidebar": "docs", + "slug": "/foo/bar", + "source": "@site/docs/foo/bar.md", + "sourceDirName": "foo", + "tags": [], + "title": "Bar", + "unversionedId": "foo/bar", + "version": "current", +} +`; + exports[`simple website content 5`] = ` { "pluginName": { @@ -2345,16 +2345,12 @@ exports[`versioned website (community) content 1`] = ` { "description": "Team current version (translated)", "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, "frontMatter": { "title": "Team title translated", }, "id": "team", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, "permalink": "/community/next/team", - "sidebarPosition": undefined, + "sidebar": "community", "slug": "/team", "source": "@site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md", "sourceDirName": ".", @@ -2367,23 +2363,21 @@ exports[`versioned website (community) content 1`] = ` exports[`versioned website (community) content 2`] = ` { - "description": "Team 1.0.0", + "description": "Team current version (translated)", "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, - "frontMatter": {}, - "id": "version-1.0.0/team", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, - "permalink": "/community/team", - "sidebarPosition": undefined, + "frontMatter": { + "title": "Team title translated", + }, + "id": "team", + "permalink": "/community/next/team", + "sidebar": "community", "slug": "/team", - "source": "@site/community_versioned_docs/version-1.0.0/team.md", + "source": "@site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md", "sourceDirName": ".", "tags": [], - "title": "team", + "title": "Team title translated", "unversionedId": "team", - "version": "1.0.0", + "version": "current", } `; @@ -2644,8 +2638,6 @@ exports[`versioned website content 1`] = ` { "description": "This is next version of bar.", "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, "frontMatter": { "slug": "barSlug", "tags": [ @@ -2658,10 +2650,12 @@ exports[`versioned website content 1`] = ` ], }, "id": "foo/bar", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, + "next": { + "permalink": "/docs/next/", + "title": "hello", + }, "permalink": "/docs/next/foo/barSlug", - "sidebarPosition": undefined, + "sidebar": "docs", "slug": "/foo/barSlug", "source": "@site/docs/foo/bar.md", "sourceDirName": "foo", @@ -2686,41 +2680,19 @@ exports[`versioned website content 1`] = ` `; exports[`versioned website content 2`] = ` -{ - "description": "Bar 1.0.1 !", - "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, - "frontMatter": {}, - "id": "version-1.0.1/foo/bar", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, - "permalink": "/docs/foo/bar", - "sidebarPosition": undefined, - "slug": "/foo/bar", - "source": "@site/versioned_docs/version-1.0.1/foo/bar.md", - "sourceDirName": "foo", - "tags": [], - "title": "bar", - "unversionedId": "foo/bar", - "version": "1.0.1", -} -`; - -exports[`versioned website content 3`] = ` { "description": "Hello next !", "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, "frontMatter": { "slug": "/", }, "id": "hello", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, "permalink": "/docs/next/", - "sidebarPosition": undefined, + "previous": { + "permalink": "/docs/next/foo/barSlug", + "title": "bar", + }, + "sidebar": "docs", "slug": "/", "source": "@site/docs/hello.md", "sourceDirName": ".", @@ -2731,27 +2703,72 @@ exports[`versioned website content 3`] = ` } `; +exports[`versioned website content 3`] = ` +{ + "description": "This is next version of bar.", + "draft": false, + "frontMatter": { + "slug": "barSlug", + "tags": [ + "barTag 1", + "barTag-2", + { + "label": "barTag 3", + "permalink": "barTag-3-permalink", + }, + ], + }, + "id": "foo/bar", + "next": { + "permalink": "/docs/next/", + "title": "hello", + }, + "permalink": "/docs/next/foo/barSlug", + "sidebar": "docs", + "slug": "/foo/barSlug", + "source": "@site/docs/foo/bar.md", + "sourceDirName": "foo", + "tags": [ + { + "label": "barTag 1", + "permalink": "/docs/next/tags/bar-tag-1", + }, + { + "label": "barTag-2", + "permalink": "/docs/next/tags/bar-tag-2", + }, + { + "label": "barTag 3", + "permalink": "/docs/next/tags/barTag-3-permalink", + }, + ], + "title": "bar", + "unversionedId": "foo/bar", + "version": "current", +} +`; + exports[`versioned website content 4`] = ` { - "description": "Hello 1.0.1 !", + "description": "Hello next !", "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, "frontMatter": { "slug": "/", }, - "id": "version-1.0.1/hello", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, - "permalink": "/docs/", - "sidebarPosition": undefined, + "id": "hello", + "permalink": "/docs/next/", + "previous": { + "permalink": "/docs/next/foo/barSlug", + "title": "bar", + }, + "sidebar": "docs", "slug": "/", - "source": "@site/versioned_docs/version-1.0.1/hello.md", + "source": "@site/docs/hello.md", "sourceDirName": ".", "tags": [], "title": "hello", "unversionedId": "hello", - "version": "1.0.1", + "version": "current", } `; @@ -2759,14 +2776,18 @@ exports[`versioned website content 5`] = ` { "description": "Baz 1.0.0 ! This will be deleted in next subsequent versions.", "draft": false, - "editUrl": undefined, - "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "version-1.0.0/foo/baz", - "lastUpdatedAt": undefined, - "lastUpdatedBy": undefined, + "next": { + "permalink": "/docs/1.0.0/", + "title": "hello", + }, "permalink": "/docs/1.0.0/foo/baz", - "sidebarPosition": undefined, + "previous": { + "permalink": "/docs/1.0.0/foo/barSlug", + "title": "bar", + }, + "sidebar": "version-1.0.0/docs", "slug": "/foo/baz", "source": "@site/versioned_docs/version-1.0.0/foo/baz.md", "sourceDirName": "foo", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts index 7598112f13..94bac84b4e 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts @@ -82,19 +82,19 @@ const createFakeActions = (contentDir: string) => { }, }; - // Query by prefix, because files have a hash at the end so it's not + // Query by substring, because files have a hash at the end so it's not // convenient to query by full filename - function getCreatedDataByPrefix(prefix: string) { + function searchCreatedData(search: string) { const entry = Object.entries(dataContainer).find(([key]) => - key.startsWith(prefix), + key.includes(search), ); if (!entry) { - throw new Error(`No created entry found for prefix "${prefix}". + throw new Error(`No created entry found for substring "${search}". Entries created: - ${Object.keys(dataContainer).join('\n- ')} `); } - return JSON.parse(entry[1] as string) as PropSidebars; + return JSON.parse(entry[1] as string) as unknown; } // Extra fns useful for tests! @@ -102,16 +102,23 @@ Entries created: getGlobalData: () => globalDataContainer, getRouteConfigs: () => routeConfigs, - checkVersionMetadataPropCreated: (version: LoadedVersion | undefined) => { + checkVersionMetadataPropCreated: ( + version: LoadedVersion | undefined, + docIds: string[], + ) => { if (!version) { throw new Error('Version not found'); } - const versionMetadataProp = getCreatedDataByPrefix( + const versionMetadataProp = searchCreatedData( `version-${_.kebabCase(version.versionName)}-metadata-prop`, - ); + ) as PropSidebars; expect(versionMetadataProp.docsSidebars).toEqual( toSidebarsProp(version as FullVersion), ); + docIds.forEach((id) => { + const docMetadataProp = searchCreatedData(_.kebabCase(id)); + expect(docMetadataProp).toMatchSnapshot(); + }); }, expectSnapshot: () => { @@ -145,7 +152,11 @@ describe('sidebar', () => { }, }), ); - await expect(plugin.loadContent!()).rejects.toThrowErrorMatchingSnapshot(); + const content = await plugin.loadContent!(); + const {actions} = createFakeActions(siteDir); + await expect( + plugin.contentLoaded!({content, actions, allContent: {}}), + ).rejects.toThrowErrorMatchingSnapshot(); }); it('site with wrong sidebar file path', async () => { @@ -330,12 +341,6 @@ describe('simple website', () => { expect(content.loadedVersions).toHaveLength(1); const [currentVersion] = content.loadedVersions; - expect(findDocById(currentVersion, 'foo/baz')).toMatchSnapshot(); - - expect(findDocById(currentVersion, 'hello')).toMatchSnapshot(); - - expect(getDocById(currentVersion, 'foo/bar')).toMatchSnapshot(); - expect(currentVersion!.sidebars).toMatchSnapshot(); const {actions, utils} = createFakeActions(pluginContentDir); @@ -346,7 +351,11 @@ describe('simple website', () => { allContent: {}, }); - utils.checkVersionMetadataPropCreated(currentVersion); + utils.checkVersionMetadataPropCreated(currentVersion, [ + 'foo/baz', + 'hello', + 'foo/bar', + ]); utils.expectSnapshot(); @@ -452,13 +461,6 @@ describe('versioned website', () => { expect(findDocById(version101, 'foo/baz')).toBeUndefined(); expect(findDocById(versionWithSlugs, 'foo/baz')).toBeUndefined(); - expect(getDocById(currentVersion, 'foo/bar')).toMatchSnapshot(); - expect(getDocById(version101, 'foo/bar')).toMatchSnapshot(); - - expect(getDocById(currentVersion, 'hello')).toMatchSnapshot(); - expect(getDocById(version101, 'hello')).toMatchSnapshot(); - expect(getDocById(version100, 'foo/baz')).toMatchSnapshot(); - expect(currentVersion!.sidebars).toMatchSnapshot( 'current version sidebars', ); @@ -475,10 +477,10 @@ describe('versioned website', () => { allContent: {}, }); - utils.checkVersionMetadataPropCreated(currentVersion); - utils.checkVersionMetadataPropCreated(version101); - utils.checkVersionMetadataPropCreated(version100); - utils.checkVersionMetadataPropCreated(versionWithSlugs); + utils.checkVersionMetadataPropCreated(currentVersion, ['foo/bar', 'hello']); + utils.checkVersionMetadataPropCreated(version101, ['foo/bar', 'hello']); + utils.checkVersionMetadataPropCreated(version100, ['foo/baz']); + utils.checkVersionMetadataPropCreated(versionWithSlugs, []); utils.expectSnapshot(); }); @@ -565,9 +567,6 @@ describe('versioned website (community)', () => { expect(content.loadedVersions).toHaveLength(2); const [currentVersion, version100] = content.loadedVersions; - expect(getDocById(currentVersion, 'team')).toMatchSnapshot(); - expect(getDocById(version100, 'team')).toMatchSnapshot(); - expect(currentVersion!.sidebars).toMatchSnapshot( 'current version sidebars', ); @@ -580,8 +579,8 @@ describe('versioned website (community)', () => { allContent: {}, }); - utils.checkVersionMetadataPropCreated(currentVersion); - utils.checkVersionMetadataPropCreated(version100); + utils.checkVersionMetadataPropCreated(currentVersion, ['team']); + utils.checkVersionMetadataPropCreated(version100, ['team']); utils.expectSnapshot(); }); diff --git a/packages/docusaurus-plugin-content-docs/src/docs.ts b/packages/docusaurus-plugin-content-docs/src/docs.ts index 7d13e40eb8..ff46666ad8 100644 --- a/packages/docusaurus-plugin-content-docs/src/docs.ts +++ b/packages/docusaurus-plugin-content-docs/src/docs.ts @@ -36,7 +36,6 @@ import type { LastUpdateData, VersionMetadata, DocFrontMatter, - LoadedVersion, FileChange, } from '@docusaurus/plugin-content-docs'; import type {LoadContext} from '@docusaurus/types'; @@ -335,7 +334,7 @@ export function addDocNavigation( docsBase: DocMetadataBase[], sidebarsUtils: SidebarsUtils, sidebarFilePath: string, -): LoadedVersion['docs'] { +): DocMetadata[] { const docsById = createDocsByIdIndex(docsBase); sidebarsUtils.checkSidebarsDocIds(