|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { render, screen, waitFor } from '../test-utils'; |
| 3 | +import { PlotOfTheDay } from './PlotOfTheDay'; |
| 4 | + |
| 5 | +// Mock sessionStorage |
| 6 | +const sessionStorageMock: Record<string, string> = {}; |
| 7 | +const sessionStorageStub = { |
| 8 | + getItem: vi.fn((key: string) => sessionStorageMock[key] ?? null), |
| 9 | + setItem: vi.fn((key: string, value: string) => { sessionStorageMock[key] = value; }), |
| 10 | + removeItem: vi.fn((key: string) => { delete sessionStorageMock[key]; }), |
| 11 | + clear: vi.fn(() => { Object.keys(sessionStorageMock).forEach(k => delete sessionStorageMock[k]); }), |
| 12 | + get length() { return Object.keys(sessionStorageMock).length; }, |
| 13 | + key: vi.fn(() => null), |
| 14 | +}; |
| 15 | + |
| 16 | +const mockData = { |
| 17 | + spec_id: 'scatter-basic', |
| 18 | + spec_title: 'Basic Scatter Plot', |
| 19 | + description: 'A scatter plot', |
| 20 | + library_id: 'matplotlib', |
| 21 | + library_name: 'Matplotlib', |
| 22 | + quality_score: 9, |
| 23 | + preview_url: 'https://cdn.example.com/plots/scatter-basic/matplotlib/plot.png', |
| 24 | + image_description: 'Shows data points with clear labels', |
| 25 | + library_version: '3.8.0', |
| 26 | + python_version: '3.12', |
| 27 | + date: '2026-04-11', |
| 28 | +}; |
| 29 | + |
| 30 | +describe('PlotOfTheDay', () => { |
| 31 | + let fetchMock: ReturnType<typeof vi.fn>; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + fetchMock = vi.fn(); |
| 35 | + vi.stubGlobal('fetch', fetchMock); |
| 36 | + // Reset sessionStorage mock state |
| 37 | + Object.keys(sessionStorageMock).forEach(k => delete sessionStorageMock[k]); |
| 38 | + vi.stubGlobal('sessionStorage', sessionStorageStub); |
| 39 | + sessionStorageStub.getItem.mockClear(); |
| 40 | + sessionStorageStub.setItem.mockClear(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('renders a placeholder while loading', () => { |
| 48 | + // Never resolve the fetch, so component stays in loading state |
| 49 | + fetchMock.mockReturnValue(new Promise(() => {})); |
| 50 | + |
| 51 | + const { container } = render(<PlotOfTheDay />); |
| 52 | + |
| 53 | + // The loading state renders a Box with minHeight for CLS prevention |
| 54 | + const placeholder = container.firstChild as HTMLElement; |
| 55 | + expect(placeholder).toBeInTheDocument(); |
| 56 | + // Should not show any text content yet |
| 57 | + expect(screen.queryByText('plot of the day')).not.toBeInTheDocument(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('shows the card after successful fetch', async () => { |
| 61 | + fetchMock.mockResolvedValueOnce({ |
| 62 | + ok: true, |
| 63 | + json: () => Promise.resolve(mockData), |
| 64 | + }); |
| 65 | + |
| 66 | + render(<PlotOfTheDay />); |
| 67 | + |
| 68 | + await waitFor(() => { |
| 69 | + expect(screen.getByText('plot of the day')).toBeInTheDocument(); |
| 70 | + }); |
| 71 | + |
| 72 | + expect(screen.getByText('Basic Scatter Plot')).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('shows image description when present', async () => { |
| 76 | + fetchMock.mockResolvedValueOnce({ |
| 77 | + ok: true, |
| 78 | + json: () => Promise.resolve(mockData), |
| 79 | + }); |
| 80 | + |
| 81 | + render(<PlotOfTheDay />); |
| 82 | + |
| 83 | + await waitFor(() => { |
| 84 | + expect(screen.getByText(/Shows data points with clear labels/)).toBeInTheDocument(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('shows library version and python version in bottom bar', async () => { |
| 89 | + fetchMock.mockResolvedValueOnce({ |
| 90 | + ok: true, |
| 91 | + json: () => Promise.resolve(mockData), |
| 92 | + }); |
| 93 | + |
| 94 | + render(<PlotOfTheDay />); |
| 95 | + |
| 96 | + await waitFor(() => { |
| 97 | + expect(screen.getByText(/Matplotlib 3\.8\.0/)).toBeInTheDocument(); |
| 98 | + expect(screen.getByText(/Python 3\.12/)).toBeInTheDocument(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns null immediately when dismissed via sessionStorage', () => { |
| 103 | + sessionStorageMock['potd_dismissed'] = 'true'; |
| 104 | + |
| 105 | + const { container } = render(<PlotOfTheDay />); |
| 106 | + |
| 107 | + // Dismissed state returns null — no DOM output |
| 108 | + expect(container.firstChild).toBeNull(); |
| 109 | + // Should not have fetched |
| 110 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns null after API error', async () => { |
| 114 | + fetchMock.mockResolvedValueOnce({ ok: false }); |
| 115 | + |
| 116 | + const { container } = render(<PlotOfTheDay />); |
| 117 | + |
| 118 | + await waitFor(() => { |
| 119 | + // After loading finishes with error, data is null so component returns null |
| 120 | + expect(container.firstChild).toBeNull(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('dismisses on close button click', async () => { |
| 125 | + fetchMock.mockResolvedValueOnce({ |
| 126 | + ok: true, |
| 127 | + json: () => Promise.resolve(mockData), |
| 128 | + }); |
| 129 | + |
| 130 | + const { userEvent } = await import('../test-utils'); |
| 131 | + const user = userEvent.setup(); |
| 132 | + |
| 133 | + const { container } = render(<PlotOfTheDay />); |
| 134 | + |
| 135 | + await waitFor(() => { |
| 136 | + expect(screen.getByText('plot of the day')).toBeInTheDocument(); |
| 137 | + }); |
| 138 | + |
| 139 | + const dismissButton = screen.getByLabelText('Dismiss plot of the day'); |
| 140 | + await user.click(dismissButton); |
| 141 | + |
| 142 | + // After dismiss, component should return null |
| 143 | + expect(container.firstChild).toBeNull(); |
| 144 | + expect(sessionStorageStub.setItem).toHaveBeenCalledWith('potd_dismissed', 'true'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('hides library version when it is "unknown"', async () => { |
| 148 | + const dataWithUnknownVersion = { ...mockData, library_version: 'unknown' }; |
| 149 | + fetchMock.mockResolvedValueOnce({ |
| 150 | + ok: true, |
| 151 | + json: () => Promise.resolve(dataWithUnknownVersion), |
| 152 | + }); |
| 153 | + |
| 154 | + render(<PlotOfTheDay />); |
| 155 | + |
| 156 | + await waitFor(() => { |
| 157 | + expect(screen.getByText('plot of the day')).toBeInTheDocument(); |
| 158 | + }); |
| 159 | + |
| 160 | + // Should show "Matplotlib" without " unknown" appended |
| 161 | + // The bottom bar text node should contain just "Matplotlib" followed by python version |
| 162 | + const bottomText = screen.getByText(/Matplotlib/); |
| 163 | + expect(bottomText.textContent).not.toContain('unknown'); |
| 164 | + }); |
| 165 | +}); |
0 commit comments