veza/apps/web/src/services/uploadService.test.ts

66 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { uploadService } from './uploadService';
// Mock URL.createObjectURL
global.URL.createObjectURL = vi.fn(() => 'mock-url');
describe('uploadService', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('uploadFile', () => {
it('should upload a file with progress callback', async () => {
const file = new File(['test'], 'test.txt', { type: 'text/plain' });
const progressCallback = vi.fn();
// Use fake timers to speed up the test
vi.useFakeTimers();
const uploadPromise = uploadService.uploadFile(file, progressCallback);
// Fast-forward time to complete upload
await vi.runAllTimersAsync();
const result = await uploadPromise;
expect(result).toBeDefined();
expect(result).toHaveProperty('upload_id');
expect(result).toHaveProperty('status');
expect(result).toHaveProperty('url');
expect(result.status).toBe('completed');
expect(progressCallback).toHaveBeenCalled();
expect(global.URL.createObjectURL).toHaveBeenCalledWith(file);
vi.useRealTimers();
});
it('should upload without progress callback', async () => {
const file = new File(['test'], 'test.txt', { type: 'text/plain' });
// Use fake timers to speed up the test
vi.useFakeTimers();
const uploadPromise = uploadService.uploadFile(file);
// Fast-forward time to complete upload
await vi.runAllTimersAsync();
const result = await uploadPromise;
expect(result).toBeDefined();
expect(result).toHaveProperty('upload_id');
expect(result.status).toBe('completed');
vi.useRealTimers();
});
});
describe('getUploadStatus', () => {
it('should return upload status', async () => {
const status = await uploadService.getUploadStatus('upload-1');
expect(status).toBeDefined();
expect(status).toHaveProperty('status');
expect(status).toHaveProperty('progress');
expect(status.status).toBe('completed');
expect(status.progress).toBe(100);
});
});
});