|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 3 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import HistoricalTrendView from './HistoricalTrendView'; |
| 5 | +import type { ActivityData } from '@/types/dashboard'; |
| 6 | + |
| 7 | +// Stub DOM observers missing in JSDOM |
| 8 | +global.ResizeObserver = class ResizeObserver { |
| 9 | + observe() {} |
| 10 | + unobserve() {} |
| 11 | + disconnect() {} |
| 12 | +}; |
| 13 | + |
| 14 | +global.IntersectionObserver = class IntersectionObserver { |
| 15 | + readonly root: Element | Document | null = null; |
| 16 | + readonly rootMargin: string = ''; |
| 17 | + readonly thresholds: ReadonlyArray<number> = []; |
| 18 | + observe() {} |
| 19 | + unobserve() {} |
| 20 | + disconnect() {} |
| 21 | + takeRecords() { |
| 22 | + return []; |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +const defaultPeriod = { kind: 'year' as const, year: '2023', from: '', to: '', label: '2023' }; |
| 27 | + |
| 28 | +// Global cache stub |
| 29 | +const globalCache: Record<string, ActivityData[]> = {}; |
| 30 | + |
| 31 | +// Mock Database stub |
| 32 | +class MockDatabase { |
| 33 | + async fetchActivity(_username: string): Promise<ActivityData[]> { |
| 34 | + // To be mocked per test |
| 35 | + return []; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const mockDb = new MockDatabase(); |
| 40 | + |
| 41 | +// Async Wrapper to simulate the Service Layer Mocking & Local Cache |
| 42 | +function AsyncTrendWrapper({ username }: { username: string }) { |
| 43 | + const [data, setData] = useState<ActivityData[] | null>(null); |
| 44 | + const [loading, setLoading] = useState(true); |
| 45 | + const [error, setError] = useState<string | null>(null); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + let isMounted = true; |
| 49 | + const fetchData = async () => { |
| 50 | + setLoading(true); |
| 51 | + setError(null); |
| 52 | + |
| 53 | + // Local cache layer query |
| 54 | + if (globalCache[username]) { |
| 55 | + if (isMounted) { |
| 56 | + setData(globalCache[username]); |
| 57 | + setLoading(false); |
| 58 | + } |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Database retrieval |
| 63 | + try { |
| 64 | + const response = await mockDb.fetchActivity(username); |
| 65 | + // Complete cache sync |
| 66 | + globalCache[username] = response; |
| 67 | + if (isMounted) { |
| 68 | + setData(response); |
| 69 | + } |
| 70 | + } catch (e: unknown) { |
| 71 | + if (isMounted) { |
| 72 | + setError(e instanceof Error ? e.message : 'Error occurred'); |
| 73 | + } |
| 74 | + } finally { |
| 75 | + if (isMounted) { |
| 76 | + setLoading(false); |
| 77 | + } |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + fetchData(); |
| 82 | + return () => { |
| 83 | + isMounted = false; |
| 84 | + }; |
| 85 | + }, [username]); |
| 86 | + |
| 87 | + if (loading) return <div data-testid="pending-overlay">Loading Activity Data...</div>; |
| 88 | + if (error) return <div data-testid="fallback-error">{error}</div>; |
| 89 | + if (!data) return null; |
| 90 | + |
| 91 | + return <HistoricalTrendView username={username} activity={data} period={defaultPeriod} />; |
| 92 | +} |
| 93 | + |
| 94 | +// Mock next/navigation |
| 95 | +vi.mock('next/navigation', () => ({ |
| 96 | + useRouter: () => ({ |
| 97 | + push: vi.fn(), |
| 98 | + }), |
| 99 | +})); |
| 100 | + |
| 101 | +describe('HistoricalTrendView - Asynchronous Service Layer Mocking & Local Cache Stubs', () => { |
| 102 | + let fetchSpy: ReturnType<typeof vi.spyOn>; |
| 103 | + |
| 104 | + beforeEach(() => { |
| 105 | + // Clear cache |
| 106 | + for (const key in globalCache) delete globalCache[key]; |
| 107 | + fetchSpy = vi.spyOn(mockDb, 'fetchActivity'); |
| 108 | + }); |
| 109 | + |
| 110 | + afterEach(() => { |
| 111 | + vi.restoreAllMocks(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('1. Mock standard asynchronous imports and databases using stubs', async () => { |
| 115 | + const mockData = [{ date: '2023-01-01', count: 5, intensity: 1 as const }]; |
| 116 | + fetchSpy.mockResolvedValue(mockData); |
| 117 | + |
| 118 | + render(<AsyncTrendWrapper username="user1" />); |
| 119 | + |
| 120 | + await waitFor(() => { |
| 121 | + expect(screen.queryByTestId('pending-overlay')).not.toBeInTheDocument(); |
| 122 | + }); |
| 123 | + |
| 124 | + expect(fetchSpy).toHaveBeenCalledWith('user1'); |
| 125 | + expect(screen.getByText('2023 · 1 days')).toBeInTheDocument(); // Component rendered correctly |
| 126 | + }); |
| 127 | + |
| 128 | + it('2. Test service loading paths to ensure pending state overlays render', () => { |
| 129 | + // Never resolve to keep loading forever |
| 130 | + fetchSpy.mockImplementation(() => new Promise(() => {})); |
| 131 | + |
| 132 | + render(<AsyncTrendWrapper username="user2" />); |
| 133 | + |
| 134 | + // Assert pending state immediately |
| 135 | + expect(screen.getByTestId('pending-overlay')).toBeInTheDocument(); |
| 136 | + expect(screen.getByText('Loading Activity Data...')).toBeInTheDocument(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('3. Assert local cache layers are queried before triggering database retrievals', async () => { |
| 140 | + const cachedData = [{ date: '2023-02-01', count: 10, intensity: 2 as const }]; |
| 141 | + globalCache['cachedUser'] = cachedData; |
| 142 | + |
| 143 | + render(<AsyncTrendWrapper username="cachedUser" />); |
| 144 | + |
| 145 | + // Wait for the component to settle |
| 146 | + await waitFor(() => { |
| 147 | + expect(screen.getByText('2023 · 1 days')).toBeInTheDocument(); |
| 148 | + }); |
| 149 | + |
| 150 | + // Database retrieval should NOT be triggered |
| 151 | + expect(fetchSpy).not.toHaveBeenCalled(); |
| 152 | + // Verify it rendered the cached data (active days = 1, streaks = 1) |
| 153 | + expect(screen.getAllByText('1', { selector: 'p.text-3xl' })).toHaveLength(3); |
| 154 | + }); |
| 155 | + |
| 156 | + it('4. Verify correct fallback procedures during fake endpoint timeout blocks', async () => { |
| 157 | + const timeoutError = new Error('Database connection timed out'); |
| 158 | + fetchSpy.mockRejectedValue(timeoutError); |
| 159 | + |
| 160 | + render(<AsyncTrendWrapper username="user-timeout" />); |
| 161 | + |
| 162 | + // Initially loading |
| 163 | + expect(screen.getByTestId('pending-overlay')).toBeInTheDocument(); |
| 164 | + |
| 165 | + // Fallback UI replaces loading overlay |
| 166 | + await waitFor(() => { |
| 167 | + expect(screen.getByTestId('fallback-error')).toBeInTheDocument(); |
| 168 | + }); |
| 169 | + |
| 170 | + expect(screen.queryByTestId('pending-overlay')).not.toBeInTheDocument(); |
| 171 | + expect(screen.getByText('Database connection timed out')).toBeInTheDocument(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('5. Assert complete cache sync is written on success callbacks', async () => { |
| 175 | + const freshData = [{ date: '2023-03-01', count: 20, intensity: 3 as const }]; |
| 176 | + fetchSpy.mockResolvedValue(freshData); |
| 177 | + |
| 178 | + // Initial cache is empty |
| 179 | + expect(globalCache['syncUser']).toBeUndefined(); |
| 180 | + |
| 181 | + render(<AsyncTrendWrapper username="syncUser" />); |
| 182 | + |
| 183 | + await waitFor(() => { |
| 184 | + expect(screen.queryByTestId('pending-overlay')).not.toBeInTheDocument(); |
| 185 | + }); |
| 186 | + |
| 187 | + // Cache sync was written successfully |
| 188 | + expect(globalCache['syncUser']).toEqual(freshData); |
| 189 | + }); |
| 190 | +}); |
0 commit comments