|
| 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 | +}) |
0 commit comments