|
1 | 1 | import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'; |
2 | | -import { getLatestVersion } from './dashboard'; |
| 2 | +import { getLatestVersion, fetchRecentNews, parseNewsMarkdown } from './dashboard'; |
3 | 3 |
|
4 | 4 | vi.mock('masonry-layout', () => ({ |
5 | 5 | default: vi.fn(), |
@@ -65,3 +65,160 @@ describe('dashboard getLatestVersion', () => { |
65 | 65 | expect(alert?.textContent).not.toBeNull(); |
66 | 66 | }); |
67 | 67 | }); |
| 68 | + |
| 69 | +describe('parseNewsMarkdown', () => { |
| 70 | + it('converts markdown links with absolute URLs', () => { |
| 71 | + const result = parseNewsMarkdown('Check [our site](https://example.com) now'); |
| 72 | + expect(result).toContain('<a href="https://example.com" target="_blank" rel="noopener noreferrer">our site</a>'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('resolves relative URLs against phpmyfaq.de base', () => { |
| 76 | + const result = parseNewsMarkdown('See [downloads](/download)'); |
| 77 | + expect(result).toContain( |
| 78 | + '<a href="https://www.phpmyfaq.de/download" target="_blank" rel="noopener noreferrer">downloads</a>' |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('resolves relative URLs without leading slash', () => { |
| 83 | + const result = parseNewsMarkdown('See [news](news/latest)'); |
| 84 | + expect(result).toContain( |
| 85 | + '<a href="https://www.phpmyfaq.de/news/latest" target="_blank" rel="noopener noreferrer">news</a>' |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('converts bold markdown', () => { |
| 90 | + const result = parseNewsMarkdown('This is **important** text'); |
| 91 | + expect(result).toContain('<strong>important</strong>'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('converts italic markdown', () => { |
| 95 | + const result = parseNewsMarkdown('This is *italic* text'); |
| 96 | + expect(result).toContain('<em>italic</em>'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('escapes HTML entities to prevent XSS', () => { |
| 100 | + const result = parseNewsMarkdown('<script>alert("xss")</script>'); |
| 101 | + expect(result).not.toContain('<script>'); |
| 102 | + expect(result).toContain('<script>'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns plain text when no markdown is present', () => { |
| 106 | + const result = parseNewsMarkdown('Just plain text'); |
| 107 | + expect(result).toBe('Just plain text'); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('fetchRecentNews', () => { |
| 112 | + beforeEach(() => { |
| 113 | + document.body.innerHTML = ` |
| 114 | + <div id="pmf-news-loader" class="d-none"></div> |
| 115 | + <div id="pmf-recent-news"></div> |
| 116 | + `; |
| 117 | + }); |
| 118 | + |
| 119 | + afterEach(() => { |
| 120 | + document.body.innerHTML = ''; |
| 121 | + vi.unstubAllGlobals(); |
| 122 | + vi.clearAllMocks(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('does nothing when container element is missing', async () => { |
| 126 | + document.body.innerHTML = ''; |
| 127 | + const fetchMock = vi.fn(); |
| 128 | + vi.stubGlobal('fetch', fetchMock); |
| 129 | + |
| 130 | + await fetchRecentNews(); |
| 131 | + |
| 132 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('renders news items on successful response', async () => { |
| 136 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 137 | + ok: true, |
| 138 | + json: vi.fn().mockResolvedValue({ |
| 139 | + news: [ |
| 140 | + { date: '2026-03-10', content: 'phpMyFAQ **4.2** released' }, |
| 141 | + { date: '2026-03-01', content: 'Check [downloads](/download)' }, |
| 142 | + ], |
| 143 | + }), |
| 144 | + }); |
| 145 | + vi.stubGlobal('fetch', fetchMock); |
| 146 | + |
| 147 | + await fetchRecentNews(); |
| 148 | + |
| 149 | + const container = document.getElementById('pmf-recent-news') as HTMLDivElement; |
| 150 | + const items = container.querySelectorAll('li'); |
| 151 | + expect(items.length).toBe(2); |
| 152 | + expect(items[0].querySelector('small')?.textContent).toBe('2026-03-10'); |
| 153 | + expect(items[0].querySelector('span')?.innerHTML).toContain('<strong>4.2</strong>'); |
| 154 | + expect(items[1].querySelector('span')?.innerHTML).toContain('href="https://www.phpmyfaq.de/download"'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('limits displayed news to 5 items', async () => { |
| 158 | + const news = Array.from({ length: 8 }, (_, i) => ({ |
| 159 | + date: `2026-03-${String(i + 1).padStart(2, '0')}`, |
| 160 | + content: `News item ${i + 1}`, |
| 161 | + })); |
| 162 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 163 | + ok: true, |
| 164 | + json: vi.fn().mockResolvedValue({ news }), |
| 165 | + }); |
| 166 | + vi.stubGlobal('fetch', fetchMock); |
| 167 | + |
| 168 | + await fetchRecentNews(); |
| 169 | + |
| 170 | + const container = document.getElementById('pmf-recent-news') as HTMLDivElement; |
| 171 | + expect(container.querySelectorAll('li').length).toBe(5); |
| 172 | + }); |
| 173 | + |
| 174 | + it('shows fallback message when news array is empty', async () => { |
| 175 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 176 | + ok: true, |
| 177 | + json: vi.fn().mockResolvedValue({ news: [] }), |
| 178 | + }); |
| 179 | + vi.stubGlobal('fetch', fetchMock); |
| 180 | + |
| 181 | + await fetchRecentNews(); |
| 182 | + |
| 183 | + const container = document.getElementById('pmf-recent-news') as HTMLDivElement; |
| 184 | + expect(container.textContent).toContain('No recent news available.'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('shows error message when response is not ok', async () => { |
| 188 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 189 | + ok: false, |
| 190 | + json: vi.fn().mockResolvedValue({ error: 'disabled' }), |
| 191 | + }); |
| 192 | + vi.stubGlobal('fetch', fetchMock); |
| 193 | + |
| 194 | + await fetchRecentNews(); |
| 195 | + |
| 196 | + const container = document.getElementById('pmf-recent-news') as HTMLDivElement; |
| 197 | + expect(container.textContent).toContain('Could not load news.'); |
| 198 | + }); |
| 199 | + |
| 200 | + it('shows error message on fetch failure', async () => { |
| 201 | + const fetchMock = vi.fn().mockRejectedValue(new Error('Network error')); |
| 202 | + vi.stubGlobal('fetch', fetchMock); |
| 203 | + |
| 204 | + await fetchRecentNews(); |
| 205 | + |
| 206 | + const container = document.getElementById('pmf-recent-news') as HTMLDivElement; |
| 207 | + expect(container.textContent).toContain('Could not load news.'); |
| 208 | + const loader = document.getElementById('pmf-news-loader') as HTMLDivElement; |
| 209 | + expect(loader.classList.contains('d-none')).toBe(true); |
| 210 | + }); |
| 211 | + |
| 212 | + it('hides loader after successful fetch', async () => { |
| 213 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 214 | + ok: true, |
| 215 | + json: vi.fn().mockResolvedValue({ news: [{ date: '2026-03-10', content: 'Test' }] }), |
| 216 | + }); |
| 217 | + vi.stubGlobal('fetch', fetchMock); |
| 218 | + |
| 219 | + await fetchRecentNews(); |
| 220 | + |
| 221 | + const loader = document.getElementById('pmf-news-loader') as HTMLDivElement; |
| 222 | + expect(loader.classList.contains('d-none')).toBe(true); |
| 223 | + }); |
| 224 | +}); |
0 commit comments