|
| 1 | +/** |
| 2 | + * @ai-context: GoalInput 回归测试——"跳过"与空输入点"开始"都必须启动计时 |
| 3 | + * (提交空目标),只有真正取消(Modal 关闭按钮)才不启动。 |
| 4 | + * @ai-context: Regression test — "skip" and empty-input "start" must both |
| 5 | + * launch the timer with an empty goal; only an explicit close cancels. |
| 6 | + */ |
| 7 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 8 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 9 | +import GoalInput from './GoalInput'; |
| 10 | + |
| 11 | +// GoalMemory 读取 Dexie,测试中隔离掉真实数据库 |
| 12 | +vi.mock('@/lib/storage', () => ({ |
| 13 | + db: { |
| 14 | + pomodoroGoals: { |
| 15 | + orderBy: () => ({ |
| 16 | + reverse: () => ({ |
| 17 | + limit: () => ({ toArray: () => Promise.resolve([]) }), |
| 18 | + }), |
| 19 | + }), |
| 20 | + }, |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +// Modal 播放开关音效,测试中无需真实音频 |
| 25 | +vi.mock('@/lib/audio/SoundPlayer', () => ({ |
| 26 | + soundPlayer: { play: vi.fn() }, |
| 27 | +})); |
| 28 | + |
| 29 | +describe('GoalInput - 空目标番茄钟', () => { |
| 30 | + const onSubmit = vi.fn(); |
| 31 | + const onClose = vi.fn(); |
| 32 | + |
| 33 | + function renderGoalInput() { |
| 34 | + return render( |
| 35 | + <GoalInput |
| 36 | + open |
| 37 | + onClose={onClose} |
| 38 | + onSubmit={onSubmit} |
| 39 | + rememberGoal={false} |
| 40 | + onRememberChange={vi.fn()} |
| 41 | + />, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + onSubmit.mockClear(); |
| 47 | + onClose.mockClear(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('点击"跳过"应提交空目标以启动计时,而非仅关闭弹窗', () => { |
| 51 | + renderGoalInput(); |
| 52 | + |
| 53 | + fireEvent.click(screen.getByRole('button', { name: '跳过' })); |
| 54 | + |
| 55 | + // 核心断言:跳过 = 无目标但开始计时 |
| 56 | + expect(onSubmit).toHaveBeenCalledWith(''); |
| 57 | + }); |
| 58 | + |
| 59 | + it('输入为空时点击"开始"也应启动计时', () => { |
| 60 | + renderGoalInput(); |
| 61 | + |
| 62 | + fireEvent.click(screen.getByRole('button', { name: '开始' })); |
| 63 | + |
| 64 | + expect(onSubmit).toHaveBeenCalledWith(''); |
| 65 | + }); |
| 66 | + |
| 67 | + it('输入为空时按 Enter 也应启动计时', () => { |
| 68 | + renderGoalInput(); |
| 69 | + |
| 70 | + fireEvent.keyDown(screen.getByPlaceholderText('这个番茄要做什么?'), { key: 'Enter' }); |
| 71 | + |
| 72 | + expect(onSubmit).toHaveBeenCalledWith(''); |
| 73 | + }); |
| 74 | + |
| 75 | + it('有目标时点击"开始"提交去空格后的目标', () => { |
| 76 | + renderGoalInput(); |
| 77 | + |
| 78 | + fireEvent.change(screen.getByPlaceholderText('这个番茄要做什么?'), { |
| 79 | + target: { value: ' 背单词 ' }, |
| 80 | + }); |
| 81 | + fireEvent.click(screen.getByRole('button', { name: '开始' })); |
| 82 | + |
| 83 | + expect(onSubmit).toHaveBeenCalledWith('背单词'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('点击"取消"不提交、不启动计时', () => { |
| 87 | + renderGoalInput(); |
| 88 | + |
| 89 | + fireEvent.click(screen.getByRole('button', { name: '取消' })); |
| 90 | + |
| 91 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 92 | + expect(onClose).toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('点击关闭按钮也是取消:不提交、不启动计时', () => { |
| 96 | + renderGoalInput(); |
| 97 | + |
| 98 | + fireEvent.click(screen.getByRole('button', { name: '关闭' })); |
| 99 | + |
| 100 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 101 | + expect(onClose).toHaveBeenCalled(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments