|
| 1 | +import { act } from 'react'; |
| 2 | +import { createRoot } from 'react-dom/client'; |
| 3 | +import type { Root } from 'react-dom/client'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { QuestionMetadata } from '@/components/error-detail/question-metadata'; |
| 6 | +import type { ErrorItemDetail } from '@/components/error-detail/types'; |
| 7 | +import { LanguageProvider } from '@/contexts/LanguageContext'; |
| 8 | +import { apiClient } from '@/lib/api-client'; |
| 9 | + |
| 10 | +vi.mock('@/lib/api-client', () => ({ apiClient: { put: vi.fn() } })); |
| 11 | +vi.mock('@/components/notebook-selector', () => ({ NotebookSelector: () => null })); |
| 12 | +vi.mock('@/lib/feedback', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 13 | + |
| 14 | +let container: HTMLDivElement; |
| 15 | +let root: Root; |
| 16 | +const onChanged = vi.fn(); |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); |
| 20 | + container = document.createElement('div'); |
| 21 | + document.body.appendChild(container); |
| 22 | + root = createRoot(container); |
| 23 | +}); |
| 24 | + |
| 25 | +afterEach(async () => { |
| 26 | + await act(async () => root.unmount()); |
| 27 | + container.remove(); |
| 28 | + vi.unstubAllGlobals(); |
| 29 | +}); |
| 30 | + |
| 31 | +async function render(gradeSemester = '初一,上期') { |
| 32 | + const item: ErrorItemDetail = { id: 'e1', questionText: '题目', answerText: '', analysis: '', knowledgePoints: '[]', tags: [], masteryLevel: 0, originalImageUrl: '', userNotes: null, gradeSemester }; |
| 33 | + await act(async () => root.render(<LanguageProvider><QuestionMetadata item={item} onChanged={onChanged} /></LanguageProvider>)); |
| 34 | +} |
| 35 | + |
| 36 | +async function click(label: string) { |
| 37 | + await act(async () => [...container.querySelectorAll('button')].find((button) => button.textContent === label)!.click()); |
| 38 | +} |
| 39 | + |
| 40 | +describe('题目年级编辑入口', () => { |
| 41 | + it('独立入口可修改未归类题目的年级并保存', async () => { |
| 42 | + await render(); |
| 43 | + await click('编辑年级/学期'); |
| 44 | + const select = container.querySelector<HTMLSelectElement>('#detail-grade-semester')!; |
| 45 | + expect(select.value).toBe('初一,上期'); |
| 46 | + await act(async () => { |
| 47 | + select.value = '高二下'; |
| 48 | + select.dispatchEvent(new Event('change', { bubbles: true })); |
| 49 | + }); |
| 50 | + await click('保存'); |
| 51 | + expect(apiClient.put).toHaveBeenCalledWith('/api/error-items/e1', { gradeSemester: '高二下', paperLevel: 'a' }); |
| 52 | + expect(onChanged).toHaveBeenCalledOnce(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('保留旧的自定义年级,取消时不保存', async () => { |
| 56 | + await render('大学拓展课程'); |
| 57 | + await click('编辑年级/学期'); |
| 58 | + expect(container.querySelector<HTMLSelectElement>('#detail-grade-semester')!.value).toBe('大学拓展课程'); |
| 59 | + await click('取消'); |
| 60 | + expect(apiClient.put).not.toHaveBeenCalled(); |
| 61 | + expect(container.textContent).toContain('大学拓展课程'); |
| 62 | + }); |
| 63 | +}); |
0 commit comments