|
|
// upload.test.js
|
|
|
let globalStudent = null;
|
|
|
let globalnumber = 0;
|
|
|
const generatedStrings = [];
|
|
|
|
|
|
// 模拟 fetch 函数
|
|
|
global.fetch = jest.fn();
|
|
|
|
|
|
// 上传文件函数
|
|
|
async function uploadFile(file) {
|
|
|
// 检查是否选中了文件
|
|
|
if (!file) {
|
|
|
throw new Error('请先选择一个文件!');
|
|
|
}
|
|
|
|
|
|
const allowedTypes = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'];
|
|
|
if (!allowedTypes.includes(file.type)) {
|
|
|
throw new Error('请上传有效的 Excel 文件!');
|
|
|
}
|
|
|
|
|
|
const maxSize = 5 * 1024 * 1024; // 5MB
|
|
|
if (file.size > maxSize) {
|
|
|
throw new Error('文件大小不能超过 5MB!'); // 在这里抛出错误
|
|
|
}
|
|
|
|
|
|
const formData = new FormData();
|
|
|
formData.append('file', file);
|
|
|
|
|
|
const response = await fetch(`http://10.133.48.140:8000/${globalnumber}/upload`, {
|
|
|
method: 'POST',
|
|
|
body: formData
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|
|
|
throw new Error('文件上传失败。');
|
|
|
}
|
|
|
|
|
|
return await response.json();
|
|
|
}
|
|
|
|
|
|
|
|
|
// 获取随机学生函数
|
|
|
async function getRandomStudent() {
|
|
|
const response = await fetch(`http://10.133.48.140:8000/${globalnumber}/random-call`, {
|
|
|
method: 'POST',
|
|
|
});
|
|
|
const student = await response.json();
|
|
|
|
|
|
globalStudent = student;
|
|
|
|
|
|
if (!generatedStrings.includes(student.id)) {
|
|
|
generatedStrings.push(student.id);
|
|
|
}
|
|
|
|
|
|
return student;
|
|
|
}
|
|
|
|
|
|
// 创建房间函数
|
|
|
async function createRoom(roomNumber) {
|
|
|
if (roomNumber.trim() === '') {
|
|
|
throw new Error('房间号码不能为空!');
|
|
|
}
|
|
|
|
|
|
globalnumber = roomNumber;
|
|
|
|
|
|
const response = await fetch(`http://10.133.48.140:8000/create-class/${globalnumber}`, {
|
|
|
method: 'POST',
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|
|
|
throw new Error('创建房间失败。');
|
|
|
}
|
|
|
|
|
|
return await response.json();
|
|
|
}
|
|
|
|
|
|
// 测试用例
|
|
|
describe('文件上传功能', () => {
|
|
|
it('应该成功上传有效的文件', async () => {
|
|
|
const mockFile = new File(['content'], 'test.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', size: 1024 });
|
|
|
|
|
|
fetch.mockResolvedValueOnce({ ok: true, json: jest.fn().mockResolvedValue({ message: '上传成功' }) });
|
|
|
|
|
|
const result = await uploadFile(mockFile);
|
|
|
expect(result.message).toBe('上传成功');
|
|
|
});
|
|
|
|
|
|
it('应该抛出错误当没有文件时', async () => {
|
|
|
await expect(uploadFile(null)).rejects.toThrow('请先选择一个文件!');
|
|
|
});
|
|
|
|
|
|
it('应该抛出错误当文件类型无效时', async () => {
|
|
|
const mockFile = new File(['content'], 'test.txt', { type: 'text/plain', size: 1024 });
|
|
|
await expect(uploadFile(mockFile)).rejects.toThrow('请上传有效的 Excel 文件!');
|
|
|
});
|
|
|
|
|
|
|
|
|
it('应该成功获取随机学生', async () => {
|
|
|
fetch.mockResolvedValueOnce({ ok: true, json: jest.fn().mockResolvedValue({ id: '123', name: 'John Doe', points: 10 }) });
|
|
|
|
|
|
const student = await getRandomStudent();
|
|
|
expect(student.name).toBe('John Doe');
|
|
|
});
|
|
|
|
|
|
it('应该成功创建房间', async () => {
|
|
|
fetch.mockResolvedValueOnce({ ok: true, json: jest.fn().mockResolvedValue({ message: '房间创建成功' }) });
|
|
|
|
|
|
const result = await createRoom('101');
|
|
|
expect(result.message).toBe('房间创建成功');
|
|
|
});
|
|
|
|
|
|
it('应该抛出错误当房间番号为空时', async () => {
|
|
|
await expect(createRoom('')).rejects.toThrow('房间号码不能为空!');
|
|
|
});
|
|
|
}); |