Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 1 | // Personal.test.jsx
|
| 2 | import React from 'react';
|
| 3 | import { render, screen, waitFor, act } from '@testing-library/react';
|
| 4 | import { MemoryRouter, useLocation, useNavigate } from 'react-router-dom';
|
| 5 | import Personal from './Personal';
|
| 6 | import { getUserInfo, getDownloadQuota, getDownloadProgress } from '../../api/personal';
|
| 7 |
|
| 8 | // Mock API 调用
|
| 9 | jest.mock('../../api/personal', () => ({
|
| 10 | getUserInfo: jest.fn(),
|
| 11 | getDownloadQuota: jest.fn(),
|
| 12 | getDownloadProgress: jest.fn()
|
| 13 | }));
|
| 14 |
|
| 15 | // Mock react-router-dom hooks
|
| 16 | jest.mock('react-router-dom', () => ({
|
| 17 | ...jest.requireActual('react-router-dom'),
|
| 18 | useNavigate: jest.fn(),
|
| 19 | useLocation: jest.fn()
|
| 20 | }));
|
| 21 |
|
| 22 | describe('Personal Component', () => {
|
| 23 | const mockNavigate = jest.fn();
|
| 24 | const mockLocation = {
|
| 25 | pathname: '/personal',
|
| 26 | state: null
|
| 27 | };
|
| 28 |
|
| 29 | beforeEach(() => {
|
| 30 | useNavigate.mockReturnValue(mockNavigate);
|
| 31 | useLocation.mockReturnValue(mockLocation);
|
| 32 |
|
| 33 | // 重置所有 mock
|
| 34 | jest.clearAllMocks();
|
| 35 |
|
| 36 | // 设置默认 mock 返回值
|
| 37 | getUserInfo.mockResolvedValue({
|
| 38 | username: 'testuser',
|
| 39 | registTime: '2023-01-01',
|
| 40 | level: 2,
|
| 41 | magicPoints: 1000,
|
| 42 | upload: 1024 * 1024 * 5, // 5MB
|
| 43 | download: 1024 * 1024 * 2, // 2MB
|
| 44 | shareRate: 2.5
|
| 45 | });
|
| 46 |
|
| 47 | getDownloadQuota.mockResolvedValue({
|
| 48 | total: 1024 * 1024 * 10, // 10MB
|
| 49 | used: 1024 * 1024 * 3, // 3MB
|
| 50 | remaining: 1024 * 1024 * 7 // 7MB
|
| 51 | });
|
| 52 |
|
| 53 | getDownloadProgress.mockResolvedValue({
|
| 54 | 'task1': 0.25,
|
| 55 | 'task2': 0.75
|
| 56 | });
|
| 57 | });
|
| 58 |
|
| 59 | it('应该正确加载并显示用户数据', async () => {
|
| 60 | render(
|
| 61 | <MemoryRouter>
|
| 62 | <Personal />
|
| 63 | </MemoryRouter>
|
| 64 | );
|
| 65 |
|
| 66 | // 初始加载状态
|
| 67 | expect(screen.getByText('加载中...')).toBeInTheDocument();
|
| 68 |
|
| 69 | // 等待数据加载完成
|
| 70 | await waitFor(() => {
|
| 71 | expect(screen.getByText('testuser')).toBeInTheDocument();
|
| 72 | expect(screen.getByText(/加入时间: 2023-01-01/)).toBeInTheDocument();
|
| 73 | expect(screen.getByText(/会员等级: Lv.2/)).toBeInTheDocument();
|
| 74 | expect(screen.getByText('1000')).toBeInTheDocument(); // 保种积分
|
| 75 | expect(screen.getByText('5.00 MB')).toBeInTheDocument(); // 上传量
|
| 76 | expect(screen.getByText('2.00 MB')).toBeInTheDocument(); // 下载量
|
| 77 | expect(screen.getByText('2.5')).toBeInTheDocument(); // 分享率
|
| 78 | });
|
| 79 | });
|
| 80 |
|
| 81 | it('应该显示下载额度信息', async () => {
|
| 82 | render(
|
| 83 | <MemoryRouter>
|
| 84 | <Personal />
|
| 85 | </MemoryRouter>
|
| 86 | );
|
| 87 |
|
| 88 | await waitFor(() => {
|
| 89 | expect(screen.getByText(/3.00 MB 已使用/)).toBeInTheDocument();
|
| 90 | expect(screen.getByText(/7.00 MB 剩余/)).toBeInTheDocument();
|
| 91 | expect(screen.getByText(/总额度: 10.00 MB/)).toBeInTheDocument();
|
| 92 | });
|
| 93 | });
|
| 94 |
|
| 95 | it('应该显示下载进度', async () => {
|
| 96 | render(
|
| 97 | <MemoryRouter>
|
| 98 | <Personal />
|
| 99 | </MemoryRouter>
|
| 100 | );
|
| 101 |
|
| 102 | await waitFor(() => {
|
| 103 | expect(screen.getByText('当前下载进度')).toBeInTheDocument();
|
| 104 | expect(screen.getByText(/任务: task1/)).toBeInTheDocument();
|
| 105 | expect(screen.getByText('25%')).toBeInTheDocument();
|
| 106 | expect(screen.getByText(/任务: task2/)).toBeInTheDocument();
|
| 107 | expect(screen.getByText('75%')).toBeInTheDocument();
|
| 108 | });
|
| 109 | });
|
| 110 |
|
| 111 | it('应该显示功能卡片并处理点击', async () => {
|
| 112 | render(
|
| 113 | <MemoryRouter>
|
| 114 | <Personal />
|
| 115 | </MemoryRouter>
|
| 116 | );
|
| 117 |
|
| 118 | await waitFor(() => {
|
| 119 | const exchangeCard = screen.getByText('兑换区');
|
| 120 | expect(exchangeCard).toBeInTheDocument();
|
| 121 |
|
| 122 | // 模拟点击功能卡片
|
| 123 | act(() => {
|
| 124 | exchangeCard.closest('.action-card').click();
|
| 125 | });
|
| 126 |
|
| 127 | expect(mockNavigate).toHaveBeenCalledWith('/personal/Exchange');
|
| 128 | });
|
| 129 | });
|
| 130 |
|
| 131 | it('应该处理返回按钮点击', async () => {
|
| 132 | render(
|
| 133 | <MemoryRouter>
|
| 134 | <Personal />
|
| 135 | </MemoryRouter>
|
| 136 | );
|
| 137 |
|
| 138 | await waitFor(() => {
|
| 139 | const backButton = screen.getByText(/返回/);
|
| 140 | act(() => {
|
| 141 | backButton.click();
|
| 142 | });
|
| 143 |
|
| 144 | expect(mockNavigate).toHaveBeenCalledWith(-1);
|
| 145 | });
|
| 146 | });
|
| 147 |
|
| 148 | it('应该处理从子页面返回的情况', async () => {
|
| 149 | useLocation.mockReturnValue({
|
| 150 | pathname: '/personal',
|
| 151 | state: { fromSubpage: true, dashboardTab: 'uploads' }
|
| 152 | });
|
| 153 |
|
| 154 | render(
|
| 155 | <MemoryRouter>
|
| 156 | <Personal />
|
| 157 | </MemoryRouter>
|
| 158 | );
|
| 159 |
|
| 160 | await waitFor(() => {
|
| 161 | const backButton = screen.getByText(/返回/);
|
| 162 | act(() => {
|
| 163 | backButton.click();
|
| 164 | });
|
| 165 |
|
| 166 | expect(mockNavigate).toHaveBeenCalledWith('/dashboard/uploads', { replace: true });
|
| 167 | });
|
| 168 | });
|
| 169 |
|
| 170 | it('应该显示错误信息当API调用失败', async () => {
|
| 171 | getUserInfo.mockRejectedValue(new Error('获取用户信息失败'));
|
| 172 |
|
| 173 | render(
|
| 174 | <MemoryRouter>
|
| 175 | <Personal />
|
| 176 | </MemoryRouter>
|
| 177 | );
|
| 178 |
|
| 179 | await waitFor(() => {
|
| 180 | expect(screen.getByText(/错误: 获取用户信息失败/)).toBeInTheDocument();
|
| 181 | });
|
| 182 | });
|
| 183 |
|
| 184 |
|
| 185 | it('应该定期更新下载进度', async () => {
|
| 186 | jest.useFakeTimers();
|
| 187 |
|
| 188 | render(
|
| 189 | <MemoryRouter>
|
| 190 | <Personal />
|
| 191 | </MemoryRouter>
|
| 192 | );
|
| 193 |
|
| 194 | await waitFor(() => {
|
| 195 | expect(getDownloadProgress).toHaveBeenCalledTimes(1);
|
| 196 | });
|
| 197 |
|
| 198 | // 快进时间
|
| 199 | act(() => {
|
| 200 | jest.advanceTimersByTime(10000);
|
| 201 | });
|
| 202 |
|
| 203 | await waitFor(() => {
|
| 204 | expect(getDownloadProgress).toHaveBeenCalledTimes(2);
|
| 205 | });
|
| 206 |
|
| 207 | jest.useRealTimers();
|
| 208 | });
|
| 209 | }); |