-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChallengeView.test.tsx
More file actions
73 lines (63 loc) · 2.24 KB
/
Copy pathChallengeView.test.tsx
File metadata and controls
73 lines (63 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { describe, it, expect, vi } from 'vitest'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { ChallengeView } from './ChallengeView'
import { api } from '@/lib/api-client'
vi.mock('@/lib/api-client', () => ({
api: {
validateChallenge: vi.fn(),
},
}))
vi.mock('framer-motion', () => ({
motion: {
div: ({ children, ...props }: { children?: React.ReactNode; [key: string]: unknown }) => <div {...props}>{children}</div>,
},
AnimatePresence: ({ children }: { children?: React.ReactNode }) => <>{children}</>,
}))
describe('ChallengeView', () => {
it('validates bug hunt answers through backend API', async () => {
const challenge = {
id: 'challenge-1',
lesson_id: 'lesson-1',
challenge_type: 'bug_hunt' as const,
data: {
description: 'Find the bug',
code_snippet: 'const a = 1;\nconst b = a + 1;\nreturn b;',
bug_line: 2,
bug_description: 'Wrong increment',
hint: 'Check arithmetic',
},
completed: false,
used_hint: false,
}
const validationResult = {
correct: true,
correct_line: 2,
explanation: 'Wrong increment',
xp_earned: 75,
xp_gained: { amount: 75, reason: 'challenge_complete', bonus: 0 },
}
vi.mocked(api.validateChallenge).mockResolvedValueOnce(validationResult)
const onComplete = vi.fn()
render(
<ChallengeView
repoId="repo-1"
challenge={challenge}
onComplete={onComplete}
onClose={() => {}}
/>
)
fireEvent.click(screen.getByText('const b = a + 1;'))
fireEvent.click(screen.getByText('Submit Answer'))
await waitFor(() => {
expect(api.validateChallenge).toHaveBeenCalledWith(
'repo-1',
'bug_hunt',
challenge,
2,
false
)
})
expect(onComplete).toHaveBeenCalledWith(validationResult, false)
expect(screen.getByText('Correct! +75 XP')).toBeInTheDocument()
})
})