|
10 | 10 |
|
11 | 11 | import {
|
12 | 12 | convertChildrenArrayToObject,
|
| 13 | + extractTechnologyProps, |
| 14 | + flattenNavigationIndex, |
13 | 15 | flattenNestedData,
|
14 | 16 | getAllChildren,
|
15 | 17 | getChildren,
|
@@ -317,3 +319,84 @@ describe('index data', () => {
|
317 | 319 | expect(childNodes).toEqual([root0Child0, root0Child1]);
|
318 | 320 | });
|
319 | 321 | });
|
| 322 | + |
| 323 | +describe('when multiple top-level children are provided', () => { |
| 324 | + const a = { |
| 325 | + type: 'overview', |
| 326 | + title: 'a', |
| 327 | + path: '/tutorials/a', |
| 328 | + children: [ |
| 329 | + { |
| 330 | + type: 'project', |
| 331 | + title: 'a1', |
| 332 | + path: '/tutorials/a/a1', |
| 333 | + }, |
| 334 | + ], |
| 335 | + }; |
| 336 | + const b = { |
| 337 | + type: 'module', |
| 338 | + title: 'a', |
| 339 | + path: '/documentation/b', |
| 340 | + children: [ |
| 341 | + { |
| 342 | + type: 'article', |
| 343 | + title: 'b1', |
| 344 | + path: '/documentation/b/b1', |
| 345 | + }, |
| 346 | + ], |
| 347 | + }; |
| 348 | + const c = { |
| 349 | + type: 'other', |
| 350 | + title: 'c', |
| 351 | + path: '/documentation/c', |
| 352 | + children: [ |
| 353 | + { |
| 354 | + type: 'article', |
| 355 | + title: 'c1', |
| 356 | + path: '/documentation/c/c1', |
| 357 | + }, |
| 358 | + ], |
| 359 | + }; |
| 360 | + |
| 361 | + describe('flattenNavigationIndex', () => { |
| 362 | + it('prefers modules', () => { |
| 363 | + // use first root node if only one is provided |
| 364 | + let flattenedIndex = flattenNavigationIndex({ swift: [a] }); |
| 365 | + expect(flattenedIndex.swift.length).toBe(1); |
| 366 | + expect(flattenedIndex.swift[0].title).toBe(a.children[0].title); |
| 367 | + flattenedIndex = flattenNavigationIndex({ swift: [b] }); |
| 368 | + expect(flattenedIndex.swift.length).toBe(1); |
| 369 | + expect(flattenedIndex.swift[0].title).toBe(b.children[0].title); |
| 370 | + |
| 371 | + // prefer "module" root when multiple top-level nodes are provided |
| 372 | + flattenedIndex = flattenNavigationIndex({ swift: [a, b] }); |
| 373 | + expect(flattenedIndex.swift.length).toBe(1); |
| 374 | + expect(flattenedIndex.swift[0].title).toBe(b.children[0].title); |
| 375 | + |
| 376 | + // fallback to first root node when multiple top-level nodes are provided |
| 377 | + // and none of them is a "module" |
| 378 | + flattenedIndex = flattenNavigationIndex({ swift: [c, a] }); |
| 379 | + expect(flattenedIndex.swift.length).toBe(1); |
| 380 | + expect(flattenedIndex.swift[0].title).toBe(c.children[0].title); |
| 381 | + }); |
| 382 | + }); |
| 383 | + |
| 384 | + describe('extractTechnologyProps', () => { |
| 385 | + it('prefers modules', () => { |
| 386 | + // use first root node if only one is provided |
| 387 | + let props = extractTechnologyProps({ swift: [a] }); |
| 388 | + expect(props.swift.technology).toBe(a.title); |
| 389 | + props = extractTechnologyProps({ swift: [b] }); |
| 390 | + expect(props.swift.technology).toBe(b.title); |
| 391 | + |
| 392 | + // prefer "module" root when multiple top-level nodes are provided |
| 393 | + props = extractTechnologyProps({ swift: [a, b] }); |
| 394 | + expect(props.swift.technology).toBe(b.title); |
| 395 | + |
| 396 | + // fallback to first root node when multiple top-level nodes are provided |
| 397 | + // and none of them is a "module" |
| 398 | + props = extractTechnologyProps({ swift: [c, a] }); |
| 399 | + expect(props.swift.technology).toBe(c.title); |
| 400 | + }); |
| 401 | + }); |
| 402 | +}); |
0 commit comments