Skip to content

Commit 1c1387e

Browse files
fix: update tests for PlotOfTheDay API fields and useCodeFetch endpoint
- Add library_version/python_version to mock_impl in test_potd_with_db - Update useCodeFetch tests to match /specs/{id}/{lib}/code endpoint - Fix Footer test: stats link fires internal_link, not external_link Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 58fe471 commit 1c1387e

3 files changed

Lines changed: 24 additions & 28 deletions

File tree

app/src/components/Footer.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Footer', () => {
3535
render(<Footer onTrackEvent={onTrackEvent} />);
3636

3737
await user.click(screen.getByText('stats'));
38-
expect(onTrackEvent).toHaveBeenCalledWith('external_link', expect.objectContaining({ destination: 'stats' }));
38+
expect(onTrackEvent).toHaveBeenCalledWith('internal_link', expect.objectContaining({ destination: 'stats' }));
3939
});
4040

4141
it('renders without onTrackEvent', () => {

app/src/hooks/useCodeFetch.test.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ describe('useCodeFetch', () => {
1818
});
1919
}
2020

21-
const apiResponse = {
22-
implementations: [
23-
{ library_id: 'matplotlib', code: 'import matplotlib\nfig, ax = plt.subplots()' },
24-
{ library_id: 'seaborn', code: 'import seaborn as sns\nsns.heatmap(data)' },
25-
],
26-
};
21+
const mplCode = 'import matplotlib\nfig, ax = plt.subplots()';
22+
const snsCode = 'import seaborn as sns\nsns.heatmap(data)';
2723

2824
describe('getCode', () => {
2925
it('returns null for uncached entries', () => {
@@ -32,37 +28,35 @@ describe('useCodeFetch', () => {
3228
});
3329

3430
it('returns cached code after successful fetch', async () => {
35-
mockFetchResponse(apiResponse);
31+
mockFetchResponse({ code: mplCode });
3632
const { result } = renderHook(() => useCodeFetch());
3733

3834
await act(async () => {
3935
await result.current.fetchCode('scatter-basic', 'matplotlib');
4036
});
4137

42-
expect(result.current.getCode('scatter-basic', 'matplotlib')).toBe(
43-
'import matplotlib\nfig, ax = plt.subplots()'
44-
);
38+
expect(result.current.getCode('scatter-basic', 'matplotlib')).toBe(mplCode);
4539
});
4640
});
4741

4842
describe('fetchCode', () => {
4943
it('fetches code from API and returns it', async () => {
50-
mockFetchResponse(apiResponse);
44+
mockFetchResponse({ code: mplCode });
5145
const { result } = renderHook(() => useCodeFetch());
5246

5347
let code: string | null = null;
5448
await act(async () => {
5549
code = await result.current.fetchCode('scatter-basic', 'matplotlib');
5650
});
5751

58-
expect(code).toBe('import matplotlib\nfig, ax = plt.subplots()');
52+
expect(code).toBe(mplCode);
5953
expect(globalThis.fetch).toHaveBeenCalledWith(
60-
expect.stringContaining('/specs/scatter-basic')
54+
expect.stringContaining('/specs/scatter-basic/matplotlib/code')
6155
);
6256
});
6357

6458
it('returns cached result on second call without re-fetching', async () => {
65-
mockFetchResponse(apiResponse);
59+
mockFetchResponse({ code: mplCode });
6660
const { result } = renderHook(() => useCodeFetch());
6761

6862
await act(async () => {
@@ -74,12 +68,12 @@ describe('useCodeFetch', () => {
7468
code = await result.current.fetchCode('scatter-basic', 'matplotlib');
7569
});
7670

77-
expect(code).toBe('import matplotlib\nfig, ax = plt.subplots()');
71+
expect(code).toBe(mplCode);
7872
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
7973
});
8074

8175
it('deduplicates concurrent requests for the same key', async () => {
82-
mockFetchResponse(apiResponse);
76+
mockFetchResponse({ code: mplCode });
8377
const { result } = renderHook(() => useCodeFetch());
8478

8579
await act(async () => {
@@ -105,8 +99,8 @@ describe('useCodeFetch', () => {
10599
expect(code).toBeNull();
106100
});
107101

108-
it('returns null when implementation is not found for library', async () => {
109-
mockFetchResponse({ implementations: [{ library_id: 'plotly', code: 'plotly code' }] });
102+
it('returns null when code field is missing', async () => {
103+
mockFetchResponse({});
110104
const { result } = renderHook(() => useCodeFetch());
111105

112106
let code: string | null = 'initial';
@@ -135,7 +129,7 @@ describe('useCodeFetch', () => {
135129
});
136130

137131
it('manages isLoading state during fetch', async () => {
138-
mockFetchResponse(apiResponse);
132+
mockFetchResponse({ code: mplCode });
139133
const { result } = renderHook(() => useCodeFetch());
140134

141135
expect(result.current.isLoading).toBe(false);
@@ -148,21 +142,21 @@ describe('useCodeFetch', () => {
148142
});
149143

150144
it('fetches different libraries independently', async () => {
151-
mockFetchResponse(apiResponse);
152-
mockFetchResponse(apiResponse);
145+
mockFetchResponse({ code: mplCode });
146+
mockFetchResponse({ code: snsCode });
153147
const { result } = renderHook(() => useCodeFetch());
154148

155-
let mplCode: string | null = null;
156-
let snsCode: string | null = null;
149+
let mplResult: string | null = null;
150+
let snsResult: string | null = null;
157151
await act(async () => {
158-
mplCode = await result.current.fetchCode('scatter-basic', 'matplotlib');
152+
mplResult = await result.current.fetchCode('scatter-basic', 'matplotlib');
159153
});
160154
await act(async () => {
161-
snsCode = await result.current.fetchCode('scatter-basic', 'seaborn');
155+
snsResult = await result.current.fetchCode('scatter-basic', 'seaborn');
162156
});
163157

164-
expect(mplCode).toContain('matplotlib');
165-
expect(snsCode).toContain('seaborn');
158+
expect(mplResult).toContain('matplotlib');
159+
expect(snsResult).toContain('seaborn');
166160
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
167161
});
168162
});

tests/unit/api/test_routers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,8 @@ def test_potd_with_db(self, client: TestClient, mock_spec) -> None:
14271427
mock_impl = MagicMock()
14281428
mock_impl.code = "import matplotlib"
14291429
mock_impl.review_image_description = "A scatter plot"
1430+
mock_impl.library_version = "3.10.0"
1431+
mock_impl.python_version = "3.13.11"
14301432
mock_impl_repo = MagicMock()
14311433
mock_impl_repo.get_by_spec_and_library = AsyncMock(return_value=mock_impl)
14321434

0 commit comments

Comments
 (0)