diff --git a/src/components/PageTreeViewItem.tsx b/src/components/PageTreeViewItem.tsx index 93c8de3..879ac7a 100644 --- a/src/components/PageTreeViewItem.tsx +++ b/src/components/PageTreeViewItem.tsx @@ -110,7 +110,7 @@ export const PageTreeViewItem = ({ onClick={onItemClick}> - {parentPath ? page.slug?.current : getRootPageSlug(page, config)} + {parentPath ? page.slug?.current : getRootPageSlug(page, config) ?? '/'} {!isDisabled && (isHovered || hasActionOpen) && ( config.documentInternationalization?.languageFieldName ?? 'language'; export const getRootPageSlug = (page: RawPageMetadata, config: PageTreeConfig) => { - if (!config.documentInternationalization) return '/'; + if (!config.documentInternationalization) return; const language = page[getLanguageFieldName(config)]; if (typeof language != 'string') { throw new Error(`Language field is not a string: ${language}`); } - return `${language}`; + return language; }; diff --git a/src/helpers/page-tree.test.ts b/src/helpers/page-tree.test.ts index 1f76a03..42625a3 100644 --- a/src/helpers/page-tree.test.ts +++ b/src/helpers/page-tree.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { PageTreeConfig, PageTreeItem, RawPageMetadata } from '../types'; +import { NestedPageTreeItem, PageTreeConfig, RawPageMetadata } from '../types'; import { findPageTreeItemById, flatMapPageTree, getAllPageMetadata, mapRawPageMetadatasToPageTree } from './page-tree'; const config: PageTreeConfig = { @@ -41,7 +41,7 @@ const rawChildContentPage: RawPageMetadata = { const rawPages: RawPageMetadata[] = [rawHomePage, rawParentContentPage, rawChildContentPage]; -const pageTree: PageTreeItem[] = [ +const pageTree: NestedPageTreeItem[] = [ { ...rawHomePage, isDraft: false, @@ -59,6 +59,7 @@ const pageTree: PageTreeItem[] = [ isDraft: false, isPublished: true, path: '/en/parent/child', + children: [], }, ], }, @@ -140,6 +141,7 @@ describe('Page tree helpers', () => { isDraft: true, isPublished: true, path: '/en', + children: [], }, ]); }); @@ -177,6 +179,7 @@ describe('Page tree helpers', () => { isDraft: false, isPublished: true, path: '/en/parent/child', + children: [], }); }); diff --git a/src/helpers/page-tree.ts b/src/helpers/page-tree.ts index 591a837..2066b92 100644 --- a/src/helpers/page-tree.ts +++ b/src/helpers/page-tree.ts @@ -90,7 +90,7 @@ const mapPageTreeItems = ( return getChildPages(parentId).map(page => { const pagePath = parentPath ? `${parentPath === '/' ? '' : parentPath}/${page.slug?.current}` - : getRootPageSlug(page, config); + : `/${getRootPageSlug(page, config) ?? ''}`; const children = orderBy(mapPageTreeItems(config, pagesWithPublishedState, page._id, pagePath), 'path'); return { diff --git a/vitest.config.ts b/vitest.config.ts index be452e5..de5c589 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -6,6 +6,7 @@ import { defineConfig, UserConfig } from 'vitest/config'; export default defineConfig({ plugins: [react()] as UserConfig['plugins'], test: { + include: ['src/**/*.test.ts'], environment: 'jsdom', }, resolve: {