Skip to content

Commit aec5e55

Browse files
Add tests and fix linting issues for UserGameList
Co-authored-by: kevinjosethomas <46242684+kevinjosethomas@users.noreply.github.com>
1 parent 06f0838 commit aec5e55

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React from 'react'
2+
import { render, screen, waitFor } from '@testing-library/react'
3+
import { UserGameList } from 'src/components/Profile/UserGameList'
4+
import * as api from 'src/api'
5+
6+
// Mock the API function
7+
jest.mock('src/api', () => ({
8+
getAnalysisGameList: jest.fn(),
9+
}))
10+
11+
// Mock framer-motion to avoid animation issues in tests
12+
jest.mock('framer-motion', () => ({
13+
motion: {
14+
div: ({ children, ...props }: React.PropsWithChildren<object>) => (
15+
<div {...props}>{children}</div>
16+
),
17+
},
18+
}))
19+
20+
const mockGetAnalysisGameList = api.getAnalysisGameList as jest.MockedFunction<
21+
typeof api.getAnalysisGameList
22+
>
23+
24+
describe('UserGameList', () => {
25+
const mockProps = {
26+
lichessId: 'testuser',
27+
userName: 'TestUser',
28+
}
29+
30+
beforeEach(() => {
31+
jest.clearAllMocks()
32+
mockGetAnalysisGameList.mockResolvedValue({
33+
games: [
34+
{
35+
game_id: 'game1',
36+
maia_name: 'maia_kdd_1500',
37+
result: '1-0',
38+
player_color: 'white',
39+
},
40+
],
41+
total_games: 1,
42+
total_pages: 1,
43+
})
44+
})
45+
46+
it('renders the component with user name in title', () => {
47+
render(<UserGameList {...mockProps} />)
48+
49+
expect(screen.getByText("TestUser's Games")).toBeInTheDocument()
50+
})
51+
52+
it('shows only Play and H&B tabs (no Custom or Lichess)', () => {
53+
render(<UserGameList {...mockProps} />)
54+
55+
expect(screen.getByText('Play')).toBeInTheDocument()
56+
expect(screen.getByText('H&B')).toBeInTheDocument()
57+
expect(screen.queryByText('Custom')).not.toBeInTheDocument()
58+
expect(screen.queryByText('Lichess')).not.toBeInTheDocument()
59+
})
60+
61+
it('calls API with correct parameters', async () => {
62+
render(<UserGameList {...mockProps} />)
63+
64+
await waitFor(() => {
65+
expect(mockGetAnalysisGameList).toHaveBeenCalledWith(
66+
'play',
67+
1,
68+
'testuser',
69+
)
70+
})
71+
})
72+
73+
it('displays games with proper labels showing user name', async () => {
74+
render(<UserGameList {...mockProps} />)
75+
76+
await waitFor(() => {
77+
expect(screen.getByText('TestUser vs. Maia 1500')).toBeInTheDocument()
78+
})
79+
})
80+
81+
it('shows loading spinner initially', () => {
82+
render(<UserGameList {...mockProps} />)
83+
84+
expect(screen.getByTestId('loading-spinner')).toBeInTheDocument()
85+
})
86+
})

src/components/Profile/UserGameList.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ export const UserGameList = ({ lichessId, userName }: UserGameListProps) => {
223223
<div className="red-scrollbar flex max-h-64 flex-col overflow-y-scroll md:max-h-96">
224224
{loading ? (
225225
<div className="flex h-full items-center justify-center py-8">
226-
<div className="h-8 w-8 animate-spin rounded-full border-b-2 border-white"></div>
226+
<div
227+
data-testid="loading-spinner"
228+
className="h-8 w-8 animate-spin rounded-full border-b-2 border-white"
229+
></div>
227230
</div>
228231
) : (
229232
<>

0 commit comments

Comments
 (0)