|
1 | | -import { describe, expect, test, vi } from 'vitest' |
| 1 | +import { afterEach, describe, expect, test, vi } from 'vitest' |
2 | 2 |
|
3 | 3 | import { resolveJourneyContext, resolveJourneyTracks } from '../lib/journey-path-resolver' |
| 4 | +import getLinkData from '@/journeys/lib/get-link-data' |
4 | 5 | import type { Page } from '@/types' |
5 | 6 |
|
6 | 7 | // Mock modules since we just want to test journey functions, not their dependencies or |
7 | 8 | // against real content files |
8 | 9 | vi.mock('@/journeys/lib/get-link-data', () => ({ |
9 | | - default: async (path: string) => [ |
10 | | - { |
11 | | - href: `/en/enterprise-cloud@latest${path}`, |
12 | | - title: `Mock Title for ${path}`, |
13 | | - }, |
14 | | - ], |
| 10 | + default: vi.fn(async (rawLinks: string | string[] | undefined) => { |
| 11 | + const path = Array.isArray(rawLinks) ? rawLinks[0] : rawLinks |
| 12 | + if (!path) return undefined |
| 13 | + return [ |
| 14 | + { |
| 15 | + href: `/en/enterprise-cloud@latest${path}`, |
| 16 | + title: `Mock Title for ${path}`, |
| 17 | + page: {} as unknown as Page, |
| 18 | + }, |
| 19 | + ] |
| 20 | + }), |
15 | 21 | })) |
16 | 22 |
|
| 23 | +const mockGetLinkData = vi.mocked(getLinkData) |
| 24 | + |
17 | 25 | vi.mock('@/content-render/index', () => ({ |
18 | 26 | renderContent: async (content: string) => content, |
19 | 27 | })) |
@@ -269,4 +277,156 @@ describe('journey-path-resolver', () => { |
269 | 277 | expect(result[0].description).toBeUndefined() |
270 | 278 | }) |
271 | 279 | }) |
| 280 | + |
| 281 | + describe('resolveJourneyContext with version-filtered guides', () => { |
| 282 | + afterEach(() => { |
| 283 | + // Restore the default implementation after each test in this block |
| 284 | + mockGetLinkData.mockImplementation(async (rawLinks: string | string[] | undefined) => { |
| 285 | + const path = Array.isArray(rawLinks) ? rawLinks[0] : rawLinks |
| 286 | + if (!path) return undefined |
| 287 | + return [ |
| 288 | + { |
| 289 | + href: `/en/enterprise-cloud@latest${path}`, |
| 290 | + title: `Mock Title for ${path}`, |
| 291 | + page: {} as unknown as Page, |
| 292 | + }, |
| 293 | + ] |
| 294 | + }) |
| 295 | + }) |
| 296 | + |
| 297 | + const mockContext = { |
| 298 | + currentProduct: 'github', |
| 299 | + currentLanguage: 'en', |
| 300 | + currentVersion: 'enterprise-cloud@latest', |
| 301 | + } |
| 302 | + |
| 303 | + // Track with 4 guides where the 2nd guide is unavailable in the current version: |
| 304 | + // raw indices: 0=setup, 1=unavailable, 2=config, 3=deploy |
| 305 | + // filtered: 0=setup, 1=config, 2=deploy |
| 306 | + const mockPages = { |
| 307 | + 'enterprise-onboarding/index': { |
| 308 | + layout: 'journey-landing', |
| 309 | + title: 'Enterprise onboarding', |
| 310 | + permalink: '/enterprise-onboarding', |
| 311 | + journeyTracks: [ |
| 312 | + { |
| 313 | + id: 'getting_started', |
| 314 | + title: 'Getting started', |
| 315 | + guides: [ |
| 316 | + { href: '/enterprise-onboarding/setup' }, |
| 317 | + { href: '/enterprise-onboarding/unavailable' }, |
| 318 | + { href: '/enterprise-onboarding/config' }, |
| 319 | + { href: '/enterprise-onboarding/deploy' }, |
| 320 | + ], |
| 321 | + }, |
| 322 | + ], |
| 323 | + }, |
| 324 | + } as unknown as Record<string, Page> |
| 325 | + |
| 326 | + test('resolveJourneyTracks filters out guides unavailable for the current version', async () => { |
| 327 | + mockGetLinkData.mockImplementation(async (rawLinks: string | string[] | undefined) => { |
| 328 | + const path = Array.isArray(rawLinks) ? rawLinks[0] : rawLinks |
| 329 | + if (path === '/enterprise-onboarding/config') return undefined |
| 330 | + if (!path) return undefined |
| 331 | + return [ |
| 332 | + { |
| 333 | + href: `/en/enterprise-cloud@latest${path}`, |
| 334 | + title: `Mock Title for ${path}`, |
| 335 | + page: {} as unknown as Page, |
| 336 | + }, |
| 337 | + ] |
| 338 | + }) |
| 339 | + |
| 340 | + const tracks = [ |
| 341 | + { |
| 342 | + id: 'getting_started', |
| 343 | + title: 'Getting started', |
| 344 | + guides: [ |
| 345 | + { href: '/enterprise-onboarding/setup' }, |
| 346 | + { href: '/enterprise-onboarding/config' }, |
| 347 | + ], |
| 348 | + }, |
| 349 | + ] |
| 350 | + const result = await resolveJourneyTracks(tracks, mockContext) |
| 351 | + |
| 352 | + // /enterprise-onboarding/config is unavailable, so only /enterprise-onboarding/setup remains |
| 353 | + expect(result[0].guides).toHaveLength(1) |
| 354 | + expect(result[0].guides[0].href).toBe( |
| 355 | + '/en/enterprise-cloud@latest/enterprise-onboarding/setup', |
| 356 | + ) |
| 357 | + }) |
| 358 | + |
| 359 | + test('numberOfGuides reflects only version-available guides', async () => { |
| 360 | + mockGetLinkData.mockImplementation(async (rawLinks: string | string[] | undefined) => { |
| 361 | + const path = Array.isArray(rawLinks) ? rawLinks[0] : rawLinks |
| 362 | + if (path === '/enterprise-onboarding/unavailable') return undefined |
| 363 | + if (!path) return undefined |
| 364 | + return [ |
| 365 | + { |
| 366 | + href: `/en/enterprise-cloud@latest${path}`, |
| 367 | + title: `Mock Title for ${path}`, |
| 368 | + page: {} as unknown as Page, |
| 369 | + }, |
| 370 | + ] |
| 371 | + }) |
| 372 | + |
| 373 | + const result = await resolveJourneyContext( |
| 374 | + '/enterprise-onboarding/config', |
| 375 | + mockPages, |
| 376 | + mockContext, |
| 377 | + ) |
| 378 | + |
| 379 | + expect(result?.numberOfGuides).toBe(3) // 3 available, not 4 raw |
| 380 | + }) |
| 381 | + |
| 382 | + test('currentGuideIndex reflects position in version-filtered list', async () => { |
| 383 | + mockGetLinkData.mockImplementation(async (rawLinks: string | string[] | undefined) => { |
| 384 | + const path = Array.isArray(rawLinks) ? rawLinks[0] : rawLinks |
| 385 | + if (path === '/enterprise-onboarding/unavailable') return undefined |
| 386 | + if (!path) return undefined |
| 387 | + return [ |
| 388 | + { |
| 389 | + href: `/en/enterprise-cloud@latest${path}`, |
| 390 | + title: `Mock Title for ${path}`, |
| 391 | + page: {} as unknown as Page, |
| 392 | + }, |
| 393 | + ] |
| 394 | + }) |
| 395 | + |
| 396 | + const result = await resolveJourneyContext( |
| 397 | + '/enterprise-onboarding/config', |
| 398 | + mockPages, |
| 399 | + mockContext, |
| 400 | + ) |
| 401 | + |
| 402 | + // config is at raw index 2, but filtered index 1 (setup=0, config=1, deploy=2) |
| 403 | + expect(result?.currentGuideIndex).toBe(1) |
| 404 | + }) |
| 405 | + |
| 406 | + test('prevGuide skips unavailable guides', async () => { |
| 407 | + mockGetLinkData.mockImplementation(async (rawLinks: string | string[] | undefined) => { |
| 408 | + const path = Array.isArray(rawLinks) ? rawLinks[0] : rawLinks |
| 409 | + if (path === '/enterprise-onboarding/unavailable') return undefined |
| 410 | + if (!path) return undefined |
| 411 | + return [ |
| 412 | + { |
| 413 | + href: `/en/enterprise-cloud@latest${path}`, |
| 414 | + title: `Mock Title for ${path}`, |
| 415 | + page: {} as unknown as Page, |
| 416 | + }, |
| 417 | + ] |
| 418 | + }) |
| 419 | + |
| 420 | + // config's predecessor in the raw list is "unavailable", but in filtered list it's "setup" |
| 421 | + const result = await resolveJourneyContext( |
| 422 | + '/enterprise-onboarding/config', |
| 423 | + mockPages, |
| 424 | + mockContext, |
| 425 | + ) |
| 426 | + |
| 427 | + expect(result?.prevGuide?.href).toBe( |
| 428 | + '/en/enterprise-cloud@latest/enterprise-onboarding/setup', |
| 429 | + ) |
| 430 | + }) |
| 431 | + }) |
272 | 432 | }) |
0 commit comments