|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +// Mock data to simulate tocItems and spotlight configurations |
| 4 | +const mockTocItems = [ |
| 5 | + { |
| 6 | + title: 'Test Debug Article', |
| 7 | + intro: 'A test article for debugging functionality.', |
| 8 | + fullPath: '/en/category/debugging-errors/test-debug-article', |
| 9 | + }, |
| 10 | + { |
| 11 | + title: 'Test Refactor Article', |
| 12 | + intro: 'A test article for refactoring functionality.', |
| 13 | + fullPath: '/en/category/refactoring-code/test-refactor-article', |
| 14 | + }, |
| 15 | + { |
| 16 | + title: 'Test Unit Article', |
| 17 | + intro: 'A test article for unit testing functionality.', |
| 18 | + fullPath: '/en/category/testing-code/test-unit-article', |
| 19 | + }, |
| 20 | +] |
| 21 | + |
| 22 | +// Helper function to simulate the spotlight processing logic from CategoryLanding |
| 23 | +function processSpotlight(spotlight, tocItems) { |
| 24 | + const findArticleData = (articlePath) => { |
| 25 | + const cleanPath = articlePath.startsWith('/') ? articlePath.slice(1) : articlePath |
| 26 | + return tocItems.find( |
| 27 | + (item) => |
| 28 | + item.fullPath?.endsWith(cleanPath) || |
| 29 | + item.fullPath?.includes(cleanPath.split('/').pop() || ''), |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + return ( |
| 34 | + spotlight?.map((spotlightItem) => { |
| 35 | + const articleData = findArticleData(spotlightItem.article) |
| 36 | + return { |
| 37 | + article: spotlightItem.article, |
| 38 | + title: articleData?.title || 'Unknown Article', |
| 39 | + description: articleData?.intro || '', |
| 40 | + url: articleData?.fullPath || spotlightItem.article, |
| 41 | + image: spotlightItem.image, |
| 42 | + } |
| 43 | + }) || [] |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +describe('spotlight processing logic', () => { |
| 48 | + test('processes spotlight object items correctly', () => { |
| 49 | + const spotlight = [ |
| 50 | + { |
| 51 | + article: '/debugging-errors/test-debug-article', |
| 52 | + image: '/assets/images/test-debugging.png', |
| 53 | + }, |
| 54 | + { |
| 55 | + article: '/refactoring-code/test-refactor-article', |
| 56 | + image: '/assets/images/test-refactoring.png', |
| 57 | + }, |
| 58 | + ] |
| 59 | + |
| 60 | + const result = processSpotlight(spotlight, mockTocItems) |
| 61 | + |
| 62 | + expect(result).toHaveLength(2) |
| 63 | + expect(result[0]).toEqual({ |
| 64 | + article: '/debugging-errors/test-debug-article', |
| 65 | + title: 'Test Debug Article', |
| 66 | + description: 'A test article for debugging functionality.', |
| 67 | + url: '/en/category/debugging-errors/test-debug-article', |
| 68 | + image: '/assets/images/test-debugging.png', |
| 69 | + }) |
| 70 | + expect(result[1]).toEqual({ |
| 71 | + article: '/refactoring-code/test-refactor-article', |
| 72 | + title: 'Test Refactor Article', |
| 73 | + description: 'A test article for refactoring functionality.', |
| 74 | + url: '/en/category/refactoring-code/test-refactor-article', |
| 75 | + image: '/assets/images/test-refactoring.png', |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + test('processes multiple spotlight items with different images', () => { |
| 80 | + const spotlight = [ |
| 81 | + { |
| 82 | + article: '/debugging-errors/test-debug-article', |
| 83 | + image: '/assets/images/debugging.png', |
| 84 | + }, |
| 85 | + { |
| 86 | + article: '/refactoring-code/test-refactor-article', |
| 87 | + image: '/assets/images/refactoring.png', |
| 88 | + }, |
| 89 | + { |
| 90 | + article: '/testing-code/test-unit-article', |
| 91 | + image: '/assets/images/testing.png', |
| 92 | + }, |
| 93 | + ] |
| 94 | + |
| 95 | + const result = processSpotlight(spotlight, mockTocItems) |
| 96 | + |
| 97 | + expect(result).toHaveLength(3) |
| 98 | + expect(result[0].image).toBe('/assets/images/debugging.png') |
| 99 | + expect(result[1].image).toBe('/assets/images/refactoring.png') |
| 100 | + expect(result[2].image).toBe('/assets/images/testing.png') |
| 101 | + expect(result[2].title).toBe('Test Unit Article') |
| 102 | + }) |
| 103 | + |
| 104 | + test('finds articles by filename when full path does not match', () => { |
| 105 | + const spotlight = [ |
| 106 | + { |
| 107 | + article: 'test-debug-article', |
| 108 | + image: '/assets/images/debug.png', |
| 109 | + }, |
| 110 | + ] |
| 111 | + const result = processSpotlight(spotlight, mockTocItems) |
| 112 | + |
| 113 | + expect(result[0].title).toBe('Test Debug Article') |
| 114 | + expect(result[0].url).toBe('/en/category/debugging-errors/test-debug-article') |
| 115 | + expect(result[0].image).toBe('/assets/images/debug.png') |
| 116 | + }) |
| 117 | + |
| 118 | + test('handles articles not found in tocItems', () => { |
| 119 | + const spotlight = [ |
| 120 | + { |
| 121 | + article: '/completely/nonexistent/path', |
| 122 | + image: '/assets/images/missing1.png', |
| 123 | + }, |
| 124 | + { |
| 125 | + article: '/another/totally-missing-article', |
| 126 | + image: '/assets/images/missing2.png', |
| 127 | + }, |
| 128 | + ] |
| 129 | + |
| 130 | + const result = processSpotlight(spotlight, mockTocItems) |
| 131 | + |
| 132 | + expect(result).toHaveLength(2) |
| 133 | + expect(result[0]).toEqual({ |
| 134 | + article: '/completely/nonexistent/path', |
| 135 | + title: 'Unknown Article', |
| 136 | + description: '', |
| 137 | + url: '/completely/nonexistent/path', |
| 138 | + image: '/assets/images/missing1.png', |
| 139 | + }) |
| 140 | + expect(result[1]).toEqual({ |
| 141 | + article: '/another/totally-missing-article', |
| 142 | + title: 'Unknown Article', |
| 143 | + description: '', |
| 144 | + url: '/another/totally-missing-article', |
| 145 | + image: '/assets/images/missing2.png', |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + test('handles empty spotlight array', () => { |
| 150 | + const spotlight = [] |
| 151 | + const result = processSpotlight(spotlight, mockTocItems) |
| 152 | + expect(result).toEqual([]) |
| 153 | + }) |
| 154 | + |
| 155 | + test('handles undefined spotlight', () => { |
| 156 | + const result = processSpotlight(undefined, mockTocItems) |
| 157 | + expect(result).toEqual([]) |
| 158 | + }) |
| 159 | +}) |
0 commit comments