113 lines
2.8 KiB
TypeScript
113 lines
2.8 KiB
TypeScript
|
|
import { fireEvent, render, screen } from '@testing-library/react';
|
||
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
|
|
||
|
|
import App from '../../App';
|
||
|
|
|
||
|
|
const { useAuthMock } = vi.hoisted(() => ({
|
||
|
|
useAuthMock: vi.fn(),
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../contexts/AuthContext', () => ({
|
||
|
|
useAuth: () => useAuthMock(),
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../contexts/LanguageContext', () => ({
|
||
|
|
useLanguage: () => ({
|
||
|
|
t: {
|
||
|
|
common: { loading: '加载中', processing: '处理中' },
|
||
|
|
alerts: {
|
||
|
|
taskTimeout: '超时',
|
||
|
|
networkError: '网络错误',
|
||
|
|
uploadFailed: '上传失败',
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../lib/uploadService', () => ({
|
||
|
|
uploadService: {
|
||
|
|
getTaskList: vi.fn().mockResolvedValue({ task_list: [], total: 0 }),
|
||
|
|
getTaskResult: vi.fn(),
|
||
|
|
calculateMD5: vi.fn(),
|
||
|
|
uploadFile: vi.fn(),
|
||
|
|
createRecognitionTask: vi.fn(),
|
||
|
|
},
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../components/Navbar', () => ({
|
||
|
|
default: () => <div>navbar</div>,
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../components/LeftSidebar', () => ({
|
||
|
|
default: ({
|
||
|
|
onUploadClick,
|
||
|
|
onRequireAuth,
|
||
|
|
canUploadAnonymously,
|
||
|
|
}: {
|
||
|
|
onUploadClick: () => void;
|
||
|
|
onRequireAuth: () => void;
|
||
|
|
canUploadAnonymously: boolean;
|
||
|
|
}) => (
|
||
|
|
<div>
|
||
|
|
<button onClick={onUploadClick}>open-upload</button>
|
||
|
|
<button onClick={onRequireAuth}>open-auth</button>
|
||
|
|
<span>{canUploadAnonymously ? 'guest-allowed' : 'guest-blocked'}</span>
|
||
|
|
</div>
|
||
|
|
),
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../components/FilePreview', () => ({
|
||
|
|
default: () => <div>preview</div>,
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../components/ResultPanel', () => ({
|
||
|
|
default: () => <div>result</div>,
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../components/UploadModal', () => ({
|
||
|
|
default: () => <div>upload-modal</div>,
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../components/UserGuide', () => ({
|
||
|
|
default: () => null,
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('../../components/AuthModal', () => ({
|
||
|
|
default: () => <div>auth-modal</div>,
|
||
|
|
}));
|
||
|
|
|
||
|
|
describe('App anonymous usage limit', () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
vi.clearAllMocks();
|
||
|
|
localStorage.clear();
|
||
|
|
localStorage.setItem('hasSeenGuide', 'true');
|
||
|
|
useAuthMock.mockReturnValue({
|
||
|
|
user: null,
|
||
|
|
initializing: false,
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
it('allows anonymous upload before the limit', () => {
|
||
|
|
localStorage.setItem('texpixel_guest_usage_count', '2');
|
||
|
|
|
||
|
|
render(<App />);
|
||
|
|
|
||
|
|
expect(screen.getByText('guest-allowed')).toBeInTheDocument();
|
||
|
|
fireEvent.click(screen.getByText('open-upload'));
|
||
|
|
|
||
|
|
expect(screen.getByText('upload-modal')).toBeInTheDocument();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('forces login after three anonymous uses', () => {
|
||
|
|
localStorage.setItem('texpixel_guest_usage_count', '3');
|
||
|
|
|
||
|
|
render(<App />);
|
||
|
|
|
||
|
|
expect(screen.getByText('guest-blocked')).toBeInTheDocument();
|
||
|
|
fireEvent.click(screen.getByText('open-upload'));
|
||
|
|
|
||
|
|
expect(screen.getByText('auth-modal')).toBeInTheDocument();
|
||
|
|
expect(screen.queryByText('upload-modal')).not.toBeInTheDocument();
|
||
|
|
});
|
||
|
|
});
|