|
| 1 | +import { Articles, CMS } from '@o2s/configs.integrations'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { Utils } from '@o2s/utils.api-harmonization'; |
| 5 | + |
| 6 | +import { mapArticleList } from './article-list.mapper'; |
| 7 | + |
| 8 | +vi.mock('@o2s/utils.api-harmonization', () => ({ |
| 9 | + Utils: { |
| 10 | + Date: { |
| 11 | + formatDateRelative: vi.fn(), |
| 12 | + }, |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +describe('article-list.mapper', () => { |
| 17 | + const createMockCms = (overrides = {}): CMS.Model.ArticleListBlock.ArticleListBlock => ({ |
| 18 | + id: 'block-123', |
| 19 | + title: 'Articles', |
| 20 | + description: 'All articles', |
| 21 | + labels: { |
| 22 | + today: 'Today', |
| 23 | + yesterday: 'Yesterday', |
| 24 | + seeAllArticles: 'See All Articles', |
| 25 | + }, |
| 26 | + meta: undefined, |
| 27 | + ...overrides, |
| 28 | + }); |
| 29 | + |
| 30 | + const createMockCategory = (overrides = {}): Articles.Model.Category => ({ |
| 31 | + id: 'category-001', |
| 32 | + title: 'Test Category', |
| 33 | + description: 'Test Description', |
| 34 | + icon: 'icon-category', |
| 35 | + slug: '/category/test', |
| 36 | + createdAt: '2024-01-15T10:00:00Z', |
| 37 | + updatedAt: '2024-01-20T14:30:00Z', |
| 38 | + ...overrides, |
| 39 | + }); |
| 40 | + |
| 41 | + const createMockArticle = (overrides = {}): Omit<Articles.Model.Article, 'sections'> => ({ |
| 42 | + id: 'article-001', |
| 43 | + slug: '/article/test', |
| 44 | + title: 'Test Article', |
| 45 | + lead: 'Test Lead', |
| 46 | + tags: ['test'], |
| 47 | + createdAt: '2024-01-15T10:00:00Z', |
| 48 | + updatedAt: '2024-01-20T14:30:00Z', |
| 49 | + ...overrides, |
| 50 | + }); |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + vi.clearAllMocks(); |
| 54 | + vi.mocked(Utils.Date.formatDateRelative).mockReturnValue('formatted-date'); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('date formatting', () => { |
| 58 | + it('should format createdAt and updatedAt for each article', () => { |
| 59 | + const articles = { |
| 60 | + total: 2, |
| 61 | + data: [ |
| 62 | + createMockArticle({ |
| 63 | + id: 'article-1', |
| 64 | + createdAt: '2024-01-15T10:00:00Z', |
| 65 | + updatedAt: '2024-01-20T14:30:00Z', |
| 66 | + }), |
| 67 | + createMockArticle({ |
| 68 | + id: 'article-2', |
| 69 | + createdAt: '2024-01-16T10:00:00Z', |
| 70 | + updatedAt: '2024-01-21T14:30:00Z', |
| 71 | + }), |
| 72 | + ], |
| 73 | + }; |
| 74 | + vi.mocked(Utils.Date.formatDateRelative) |
| 75 | + .mockReturnValueOnce('Jan 15, 2024') |
| 76 | + .mockReturnValueOnce('Jan 20, 2024') |
| 77 | + .mockReturnValueOnce('Jan 16, 2024') |
| 78 | + .mockReturnValueOnce('Jan 21, 2024'); |
| 79 | + |
| 80 | + const result = mapArticleList(createMockCms(), articles, undefined, 'en'); |
| 81 | + |
| 82 | + expect(result.items.data).toHaveLength(2); |
| 83 | + expect(result.items.data[0]?.createdAt).toBe('Jan 15, 2024'); |
| 84 | + expect(result.items.data[0]?.updatedAt).toBe('Jan 20, 2024'); |
| 85 | + expect(result.items.data[1]?.createdAt).toBe('Jan 16, 2024'); |
| 86 | + expect(result.items.data[1]?.updatedAt).toBe('Jan 21, 2024'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should pass correct arguments to formatDateRelative', () => { |
| 90 | + const articles = { |
| 91 | + total: 1, |
| 92 | + data: [createMockArticle({ createdAt: '2024-01-15T10:00:00Z', updatedAt: '2024-01-20T14:30:00Z' })], |
| 93 | + }; |
| 94 | + const cms = createMockCms({ |
| 95 | + labels: { |
| 96 | + today: 'Dzisiaj', |
| 97 | + yesterday: 'Wczoraj', |
| 98 | + seeAllArticles: 'Zobacz wszystkie', |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + mapArticleList(cms, articles, undefined, 'pl'); |
| 103 | + |
| 104 | + expect(Utils.Date.formatDateRelative).toHaveBeenCalledWith( |
| 105 | + '2024-01-15T10:00:00Z', |
| 106 | + 'pl', |
| 107 | + 'Dzisiaj', |
| 108 | + 'Wczoraj', |
| 109 | + ); |
| 110 | + expect(Utils.Date.formatDateRelative).toHaveBeenCalledWith( |
| 111 | + '2024-01-20T14:30:00Z', |
| 112 | + 'pl', |
| 113 | + 'Dzisiaj', |
| 114 | + 'Wczoraj', |
| 115 | + ); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('conditional category link', () => { |
| 120 | + it('should include categoryLink when category is provided', () => { |
| 121 | + const articles = { |
| 122 | + total: 0, |
| 123 | + data: [], |
| 124 | + }; |
| 125 | + const category = createMockCategory({ slug: '/category/test-category' }); |
| 126 | + |
| 127 | + const result = mapArticleList(createMockCms(), articles, category, 'en'); |
| 128 | + |
| 129 | + expect(result.categoryLink).toBeDefined(); |
| 130 | + expect(result.categoryLink?.url).toBe('/category/test-category'); |
| 131 | + expect(result.categoryLink?.label).toBe('See All Articles'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should set categoryLink to undefined when category is not provided', () => { |
| 135 | + const articles = { |
| 136 | + total: 0, |
| 137 | + data: [], |
| 138 | + }; |
| 139 | + |
| 140 | + const result = mapArticleList(createMockCms(), articles, undefined, 'en'); |
| 141 | + |
| 142 | + expect(result.categoryLink).toBeUndefined(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should use correct label from CMS for categoryLink', () => { |
| 146 | + const articles = { |
| 147 | + total: 0, |
| 148 | + data: [], |
| 149 | + }; |
| 150 | + const category = createMockCategory({ slug: '/category/test' }); |
| 151 | + const cms = createMockCms({ |
| 152 | + labels: { |
| 153 | + today: 'Today', |
| 154 | + yesterday: 'Yesterday', |
| 155 | + seeAllArticles: 'View All', |
| 156 | + }, |
| 157 | + }); |
| 158 | + |
| 159 | + const result = mapArticleList(cms, articles, category, 'en'); |
| 160 | + |
| 161 | + expect(result.categoryLink?.label).toBe('View All'); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('mapArticleList', () => { |
| 166 | + it('should set correct __typename and id from cms', () => { |
| 167 | + const articles = { |
| 168 | + total: 0, |
| 169 | + data: [], |
| 170 | + }; |
| 171 | + |
| 172 | + const result = mapArticleList(createMockCms(), articles, undefined, 'en'); |
| 173 | + |
| 174 | + expect(result.__typename).toBe('ArticleListBlock'); |
| 175 | + expect(result.id).toBe('block-123'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should preserve articles total', () => { |
| 179 | + const articles = { |
| 180 | + total: 5, |
| 181 | + data: Array(5) |
| 182 | + .fill(null) |
| 183 | + .map((_, i) => createMockArticle({ id: `article-${i}` })), |
| 184 | + }; |
| 185 | + |
| 186 | + const result = mapArticleList(createMockCms(), articles, undefined, 'en'); |
| 187 | + |
| 188 | + expect(result.items.total).toBe(5); |
| 189 | + expect(result.items.data).toHaveLength(5); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should preserve all article fields except sections', () => { |
| 193 | + const articles = { |
| 194 | + total: 1, |
| 195 | + data: [ |
| 196 | + createMockArticle({ |
| 197 | + id: 'article-001', |
| 198 | + slug: '/article/test', |
| 199 | + title: 'Test Article', |
| 200 | + lead: 'Test Lead', |
| 201 | + tags: ['test', 'vitest'], |
| 202 | + }), |
| 203 | + ], |
| 204 | + }; |
| 205 | + |
| 206 | + const result = mapArticleList(createMockCms(), articles, undefined, 'en'); |
| 207 | + |
| 208 | + expect(result.items.data[0]?.id).toBe('article-001'); |
| 209 | + expect(result.items.data[0]?.slug).toBe('/article/test'); |
| 210 | + expect(result.items.data[0]?.title).toBe('Test Article'); |
| 211 | + expect(result.items.data[0]?.lead).toBe('Test Lead'); |
| 212 | + expect(result.items.data[0]?.tags).toEqual(['test', 'vitest']); |
| 213 | + }); |
| 214 | + }); |
| 215 | +}); |
0 commit comments