|
| 1 | +import { describe, expect, test, vi } from 'vitest' |
| 2 | +import { getLinkData } from '@/article-api/lib/get-link-data' |
| 3 | +import type { Context, Page, Permalink } from '@/types' |
| 4 | + |
| 5 | +// Helper to create a minimal mock page |
| 6 | +function createMockPage(options: { |
| 7 | + title?: string |
| 8 | + intro?: string |
| 9 | + permalinks?: Partial<Permalink>[] |
| 10 | +}): Page { |
| 11 | + const page = { |
| 12 | + title: options.title || 'Test Title', |
| 13 | + intro: options.intro, |
| 14 | + permalinks: (options.permalinks || []) as Permalink[], |
| 15 | + renderTitle: vi.fn().mockResolvedValue(options.title || 'Test Title'), |
| 16 | + renderProp: vi.fn().mockResolvedValue(options.intro || ''), |
| 17 | + } |
| 18 | + return page as unknown as Page |
| 19 | +} |
| 20 | + |
| 21 | +// Helper to create a minimal context |
| 22 | +function createContext(currentVersion = 'free-pro-team@latest'): Context { |
| 23 | + return { currentVersion } as unknown as Context |
| 24 | +} |
| 25 | + |
| 26 | +describe('getLinkData', () => { |
| 27 | + describe('when page is not found', () => { |
| 28 | + test('returns href as both href and title when page not resolved', async () => { |
| 29 | + const resolvePath = vi.fn().mockReturnValue(undefined) |
| 30 | + const context = createContext() |
| 31 | + |
| 32 | + const result = await getLinkData( |
| 33 | + '/en/missing-page', |
| 34 | + 'en', |
| 35 | + '/en/current', |
| 36 | + context, |
| 37 | + resolvePath, |
| 38 | + ) |
| 39 | + |
| 40 | + expect(result).toEqual({ |
| 41 | + href: '/en/missing-page', |
| 42 | + title: '/en/missing-page', |
| 43 | + }) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + describe('when page is found', () => { |
| 48 | + test('returns rendered title from page', async () => { |
| 49 | + const page = createMockPage({ title: 'My Page Title' }) |
| 50 | + const resolvePath = vi.fn().mockReturnValue(page) |
| 51 | + const context = createContext() |
| 52 | + |
| 53 | + const result = await getLinkData('/en/some-page', 'en', '/en/current', context, resolvePath) |
| 54 | + |
| 55 | + expect(result.title).toBe('My Page Title') |
| 56 | + expect(page.renderTitle).toHaveBeenCalledWith(context, { unwrap: true }) |
| 57 | + }) |
| 58 | + |
| 59 | + test('returns rendered intro when page has intro', async () => { |
| 60 | + const page = createMockPage({ |
| 61 | + title: 'Page', |
| 62 | + intro: 'This is the intro text', |
| 63 | + }) |
| 64 | + const resolvePath = vi.fn().mockReturnValue(page) |
| 65 | + const context = createContext() |
| 66 | + |
| 67 | + const result = await getLinkData('/en/some-page', 'en', '/en/current', context, resolvePath) |
| 68 | + |
| 69 | + expect(result.intro).toBe('This is the intro text') |
| 70 | + expect(page.renderProp).toHaveBeenCalledWith('intro', context, { textOnly: true }) |
| 71 | + }) |
| 72 | + |
| 73 | + test('returns empty intro when page has no intro', async () => { |
| 74 | + const page = createMockPage({ title: 'Page', intro: undefined }) |
| 75 | + const resolvePath = vi.fn().mockReturnValue(page) |
| 76 | + const context = createContext() |
| 77 | + |
| 78 | + const result = await getLinkData('/en/some-page', 'en', '/en/current', context, resolvePath) |
| 79 | + |
| 80 | + expect(result.intro).toBe('') |
| 81 | + expect(page.renderProp).not.toHaveBeenCalled() |
| 82 | + }) |
| 83 | + |
| 84 | + test('uses permalink href when matching permalink found', async () => { |
| 85 | + const page = createMockPage({ |
| 86 | + title: 'Page', |
| 87 | + permalinks: [ |
| 88 | + { languageCode: 'en', pageVersion: 'free-pro-team@latest', href: '/en/resolved-path' }, |
| 89 | + ], |
| 90 | + }) |
| 91 | + const resolvePath = vi.fn().mockReturnValue(page) |
| 92 | + const context = createContext('free-pro-team@latest') |
| 93 | + |
| 94 | + const result = await getLinkData( |
| 95 | + '/en/original-href', |
| 96 | + 'en', |
| 97 | + '/en/current', |
| 98 | + context, |
| 99 | + resolvePath, |
| 100 | + ) |
| 101 | + |
| 102 | + expect(result.href).toBe('/en/resolved-path') |
| 103 | + }) |
| 104 | + |
| 105 | + test('falls back to original href when no matching permalink', async () => { |
| 106 | + const page = createMockPage({ |
| 107 | + title: 'Page', |
| 108 | + permalinks: [{ languageCode: 'ja', pageVersion: 'free-pro-team@latest', href: '/ja/page' }], |
| 109 | + }) |
| 110 | + const resolvePath = vi.fn().mockReturnValue(page) |
| 111 | + const context = createContext('free-pro-team@latest') |
| 112 | + |
| 113 | + const result = await getLinkData( |
| 114 | + '/en/original-href', |
| 115 | + 'en', |
| 116 | + '/en/current', |
| 117 | + context, |
| 118 | + resolvePath, |
| 119 | + ) |
| 120 | + |
| 121 | + expect(result.href).toBe('/en/original-href') |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + describe('resolvePath function usage', () => { |
| 126 | + test('passes correct arguments to resolvePath', async () => { |
| 127 | + const page = createMockPage({ title: 'Page' }) |
| 128 | + const resolvePath = vi.fn().mockReturnValue(page) |
| 129 | + const context = createContext() |
| 130 | + |
| 131 | + await getLinkData('/en/target', 'en', '/en/current', context, resolvePath) |
| 132 | + |
| 133 | + expect(resolvePath).toHaveBeenCalledWith('/en/target', 'en', '/en/current', context) |
| 134 | + }) |
| 135 | + }) |
| 136 | +}) |
0 commit comments