page-node.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import type { IPageHasId } from '@growi/core';
  2. import { OptionParser } from '@growi/core/dist/remark-plugins';
  3. import { mock } from 'vitest-mock-extended';
  4. import type { PageNode } from '../../interfaces/page-node';
  5. import { generatePageNodeTree } from './page-node';
  6. function omitPageData(pageNode: PageNode): Omit<PageNode, 'page'> {
  7. // Destructure to omit 'page', and recursively process children
  8. // biome-ignore lint/correctness/noUnusedVariables: ignore
  9. const { page, children, ...rest } = pageNode;
  10. return {
  11. ...rest,
  12. children: children.map((child) => omitPageData(child)),
  13. };
  14. }
  15. describe('generatePageNodeTree()', () => {
  16. it("returns when the rootPagePath is '/'", () => {
  17. // setup
  18. const pages: IPageHasId[] = ['/', '/Sandbox'].map((path) =>
  19. mock<IPageHasId>({ path }),
  20. );
  21. // when
  22. const result = generatePageNodeTree('/', pages);
  23. const resultWithoutPageData = result.map((pageNode) =>
  24. omitPageData(pageNode),
  25. );
  26. // then
  27. expect(resultWithoutPageData).toStrictEqual([
  28. {
  29. pagePath: '/Sandbox',
  30. children: [],
  31. },
  32. ]);
  33. });
  34. it('returns when the pages are not empty', () => {
  35. // setup
  36. const pages: IPageHasId[] = [
  37. '/Sandbox',
  38. '/Sandbox/level2',
  39. '/Sandbox/level2/level3-1',
  40. '/Sandbox/level2/level3-2',
  41. '/Sandbox/level2/level3-3',
  42. ].map((path) => mock<IPageHasId>({ path }));
  43. // when
  44. const result = generatePageNodeTree('/Sandbox', pages);
  45. const resultWithoutPageData = result.map((pageNode) =>
  46. omitPageData(pageNode),
  47. );
  48. // then
  49. expect(resultWithoutPageData).toStrictEqual([
  50. {
  51. pagePath: '/Sandbox/level2',
  52. children: [
  53. {
  54. pagePath: '/Sandbox/level2/level3-1',
  55. children: [],
  56. },
  57. {
  58. pagePath: '/Sandbox/level2/level3-2',
  59. children: [],
  60. },
  61. {
  62. pagePath: '/Sandbox/level2/level3-3',
  63. children: [],
  64. },
  65. ],
  66. },
  67. ]);
  68. });
  69. it('returns when the pages include some empty pages', () => {
  70. // setup
  71. const pages: IPageHasId[] = [
  72. '/',
  73. '/user/foo',
  74. '/user/bar',
  75. '/user/bar/memo/2023/06/01',
  76. '/user/bar/memo/2023/06/02/memo-test',
  77. ].map((path) => mock<IPageHasId>({ path }));
  78. // when
  79. const result = generatePageNodeTree('/', pages);
  80. const resultWithoutPageData = result.map((pageNode) =>
  81. omitPageData(pageNode),
  82. );
  83. // then
  84. expect(resultWithoutPageData).toStrictEqual([
  85. {
  86. pagePath: '/user',
  87. children: [
  88. {
  89. pagePath: '/user/foo',
  90. children: [],
  91. },
  92. {
  93. pagePath: '/user/bar',
  94. children: [
  95. {
  96. pagePath: '/user/bar/memo',
  97. children: [
  98. {
  99. pagePath: '/user/bar/memo/2023',
  100. children: [
  101. {
  102. pagePath: '/user/bar/memo/2023/06',
  103. children: [
  104. {
  105. pagePath: '/user/bar/memo/2023/06/01',
  106. children: [],
  107. },
  108. {
  109. pagePath: '/user/bar/memo/2023/06/02',
  110. children: [
  111. {
  112. pagePath: '/user/bar/memo/2023/06/02/memo-test',
  113. children: [],
  114. },
  115. ],
  116. },
  117. ],
  118. },
  119. ],
  120. },
  121. ],
  122. },
  123. ],
  124. },
  125. ],
  126. },
  127. ]);
  128. });
  129. it("returns with 'depth=1:2'", () => {
  130. // setup
  131. const pages: IPageHasId[] = [
  132. '/Sandbox',
  133. '/Sandbox/level2-1',
  134. '/Sandbox/level2-2',
  135. '/user',
  136. '/user/foo',
  137. '/user/bar',
  138. ].map((path) => mock<IPageHasId>({ path }));
  139. // when
  140. const depthRange = OptionParser.parseRange('1:2');
  141. const result = generatePageNodeTree('/', pages, depthRange);
  142. const resultWithoutPageData = result.map((pageNode) =>
  143. omitPageData(pageNode),
  144. );
  145. // then
  146. expect(resultWithoutPageData).toStrictEqual([
  147. {
  148. pagePath: '/Sandbox',
  149. children: [
  150. {
  151. pagePath: '/Sandbox/level2-1',
  152. children: [],
  153. },
  154. {
  155. pagePath: '/Sandbox/level2-2',
  156. children: [],
  157. },
  158. ],
  159. },
  160. {
  161. pagePath: '/user',
  162. children: [
  163. {
  164. pagePath: '/user/foo',
  165. children: [],
  166. },
  167. {
  168. pagePath: '/user/bar',
  169. children: [],
  170. },
  171. ],
  172. },
  173. ]);
  174. });
  175. it("returns with 'depth=2:3'", () => {
  176. // setup
  177. const pages: IPageHasId[] = [
  178. '/foo/level2',
  179. '/foo/level2',
  180. '/foo/level2/level3-1',
  181. '/foo/level2/level3-2',
  182. ].map((path) => mock<IPageHasId>({ path }));
  183. // when
  184. const depthRange = OptionParser.parseRange('2:3');
  185. const result = generatePageNodeTree('/', pages, depthRange);
  186. const resultWithoutPageData = result.map((pageNode) =>
  187. omitPageData(pageNode),
  188. );
  189. // then
  190. expect(resultWithoutPageData).toStrictEqual([
  191. {
  192. pagePath: '/foo/level2',
  193. children: [
  194. {
  195. pagePath: '/foo/level2/level3-1',
  196. children: [],
  197. },
  198. {
  199. pagePath: '/foo/level2/level3-2',
  200. children: [],
  201. },
  202. ],
  203. },
  204. ]);
  205. });
  206. });