// @vitest-environment jsdom import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; import {act, cleanup, screen} from '@testing-library/react'; import Dashboard from './Dashboard.jsx'; import {fetchRoutes, json} from '../test/fetchRoutes.js'; import {renderWithProviders} from '../test/renderWithProviders.jsx'; vi.mock('react-i18next', () => import('../test/i18n-mock.js')); const empty = {content: [], total: 0, byStatus: {}}; const wait = (ms) => act(() => vi.advanceTimersByTimeAsync(ms)); const calls = (path) => fetch.mock.calls.filter(([url]) => new URL(url, 'http://x').pathname.endsWith(path)).length; beforeEach(() => { vi.useFakeTimers({shouldAdvanceTime: true}); localStorage.setItem('tbp_session', '1'); }); afterEach(() => { cleanup(); vi.useRealTimers(); localStorage.clear(); }); describe('Dashboard sync on a visit', () => { it('showsTheSyncingBadgeDuringTheVisitSyncAndReloadsBannerAndLibraryAfterIt', async () => { let syncStatus = {lastSyncStatus: 'OK', lastSyncedAt: '2026-10-03T10:00:00Z'}; let syncChanges = json(204, {}); fetchRoutes({ 'GET /api/library/summary': json(200, empty), 'GET /api/hints': json(200, {results: []}), 'GET /api/news': json(200, {results: []}), 'GET /api/library/sync-changes': () => syncChanges, 'GET /api/users/me': json(200, {email: 'a@b.de', displayName: 'Lobo', steamId: '76561198000000000'}), 'GET /api/library/games': json(200, empty), 'GET /api/library/stats': json(200, {trend: null}), 'GET /api/library/steam-profile': json(200, {personaName: 'Lobo', avatarUrl: null}), 'GET /api/library/activity': json(200, {totalMinutes: 0, days: [], recentGames: []}), 'GET /api/library/backlog': json(200, {games: []}), 'GET /api/library/games-count': json(200, {count: 1, hasSteamId: true, syncStarted: true}), 'GET /api/library/sync-status': () => json(200, syncStatus), 'GET /api/library/achievements/summary': json(200, {unlocked: 0, startedGames: 0, perfectGames: 0, averageCompletion: 0, pendingGames: 0, hiddenGames: 0, closest: []}), }); renderWithProviders(, {entries: ['/dashboard']}); expect(await screen.findByText(/DASHBOARD.HELLO/)).toBeTruthy(); expect(screen.queryByText('LIBRARY.SYNCING')).toBeNull(); syncStatus = {lastSyncStatus: 'RUNNING', lastSyncedAt: '2026-10-03T10:00:00Z'}; await wait(3000); expect(screen.getByText('LIBRARY.SYNCING')).toBeTruthy(); const gamesBefore = calls('/api/library/games'); const changesBefore = calls('/api/library/sync-changes'); syncStatus = {lastSyncStatus: 'OK', lastSyncedAt: '2026-10-04T08:00:00Z'}; syncChanges = json(200, {syncedAt: '2026-10-04T08:00:00Z', startedBy: 'LOGIN', seen: false, importedGames: 0, changes: [{kind: 'ADDED', appid: 1, name: 'New game', amount: 0}]}); await wait(3000); expect(screen.queryByText('LIBRARY.SYNCING')).toBeNull(); expect(await screen.findByText('SYNC_CHANGES.ADDED:1')).toBeTruthy(); expect(calls('/api/library/sync-changes')).toBeGreaterThan(changesBefore); expect(calls('/api/library/games')).toBeGreaterThan(gamesBefore); }); });