| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- import type { IPageHasId } from '@growi/core';
- import { OptionParser } from '@growi/core/dist/remark-plugins';
- import { mock } from 'vitest-mock-extended';
- import type { PageNode } from '../../interfaces/page-node';
- import { generatePageNodeTree } from './page-node';
- function omitPageData(pageNode: PageNode): Omit<PageNode, 'page'> {
- // Destructure to omit 'page', and recursively process children
- // biome-ignore lint/correctness/noUnusedVariables: ignore
- const { page, children, ...rest } = pageNode;
- return {
- ...rest,
- children: children.map((child) => omitPageData(child)),
- };
- }
- describe('generatePageNodeTree()', () => {
- it("returns when the rootPagePath is '/'", () => {
- // setup
- const pages: IPageHasId[] = ['/', '/Sandbox'].map((path) =>
- mock<IPageHasId>({ path }),
- );
- // when
- const result = generatePageNodeTree('/', pages);
- const resultWithoutPageData = result.map((pageNode) =>
- omitPageData(pageNode),
- );
- // then
- expect(resultWithoutPageData).toStrictEqual([
- {
- pagePath: '/Sandbox',
- children: [],
- },
- ]);
- });
- it('returns when the pages are not empty', () => {
- // setup
- const pages: IPageHasId[] = [
- '/Sandbox',
- '/Sandbox/level2',
- '/Sandbox/level2/level3-1',
- '/Sandbox/level2/level3-2',
- '/Sandbox/level2/level3-3',
- ].map((path) => mock<IPageHasId>({ path }));
- // when
- const result = generatePageNodeTree('/Sandbox', pages);
- const resultWithoutPageData = result.map((pageNode) =>
- omitPageData(pageNode),
- );
- // then
- expect(resultWithoutPageData).toStrictEqual([
- {
- pagePath: '/Sandbox/level2',
- children: [
- {
- pagePath: '/Sandbox/level2/level3-1',
- children: [],
- },
- {
- pagePath: '/Sandbox/level2/level3-2',
- children: [],
- },
- {
- pagePath: '/Sandbox/level2/level3-3',
- children: [],
- },
- ],
- },
- ]);
- });
- it('returns when the pages include some empty pages', () => {
- // setup
- const pages: IPageHasId[] = [
- '/',
- '/user/foo',
- '/user/bar',
- '/user/bar/memo/2023/06/01',
- '/user/bar/memo/2023/06/02/memo-test',
- ].map((path) => mock<IPageHasId>({ path }));
- // when
- const result = generatePageNodeTree('/', pages);
- const resultWithoutPageData = result.map((pageNode) =>
- omitPageData(pageNode),
- );
- // then
- expect(resultWithoutPageData).toStrictEqual([
- {
- pagePath: '/user',
- children: [
- {
- pagePath: '/user/foo',
- children: [],
- },
- {
- pagePath: '/user/bar',
- children: [
- {
- pagePath: '/user/bar/memo',
- children: [
- {
- pagePath: '/user/bar/memo/2023',
- children: [
- {
- pagePath: '/user/bar/memo/2023/06',
- children: [
- {
- pagePath: '/user/bar/memo/2023/06/01',
- children: [],
- },
- {
- pagePath: '/user/bar/memo/2023/06/02',
- children: [
- {
- pagePath: '/user/bar/memo/2023/06/02/memo-test',
- children: [],
- },
- ],
- },
- ],
- },
- ],
- },
- ],
- },
- ],
- },
- ],
- },
- ]);
- });
- it("returns with 'depth=1:2'", () => {
- // setup
- const pages: IPageHasId[] = [
- '/Sandbox',
- '/Sandbox/level2-1',
- '/Sandbox/level2-2',
- '/user',
- '/user/foo',
- '/user/bar',
- ].map((path) => mock<IPageHasId>({ path }));
- // when
- const depthRange = OptionParser.parseRange('1:2');
- const result = generatePageNodeTree('/', pages, depthRange);
- const resultWithoutPageData = result.map((pageNode) =>
- omitPageData(pageNode),
- );
- // then
- expect(resultWithoutPageData).toStrictEqual([
- {
- pagePath: '/Sandbox',
- children: [
- {
- pagePath: '/Sandbox/level2-1',
- children: [],
- },
- {
- pagePath: '/Sandbox/level2-2',
- children: [],
- },
- ],
- },
- {
- pagePath: '/user',
- children: [
- {
- pagePath: '/user/foo',
- children: [],
- },
- {
- pagePath: '/user/bar',
- children: [],
- },
- ],
- },
- ]);
- });
- it("returns with 'depth=2:3'", () => {
- // setup
- const pages: IPageHasId[] = [
- '/foo/level2',
- '/foo/level2',
- '/foo/level2/level3-1',
- '/foo/level2/level3-2',
- ].map((path) => mock<IPageHasId>({ path }));
- // when
- const depthRange = OptionParser.parseRange('2:3');
- const result = generatePageNodeTree('/', pages, depthRange);
- const resultWithoutPageData = result.map((pageNode) =>
- omitPageData(pageNode),
- );
- // then
- expect(resultWithoutPageData).toStrictEqual([
- {
- pagePath: '/foo/level2',
- children: [
- {
- pagePath: '/foo/level2/level3-1',
- children: [],
- },
- {
- pagePath: '/foo/level2/level3-2',
- children: [],
- },
- ],
- },
- ]);
- });
- });
|