Skip to content

Commit 47f694f

Browse files
committed
test: add unit tests for uploadFileUint8Array error handling
1 parent 512e4ec commit 47f694f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import axios, { AxiosError } from 'axios';
3+
import { uploadFileUint8Array } from './upload-utils';
4+
5+
vi.mock('axios');
6+
7+
describe('uploadFileUint8Array error handling', () => {
8+
let mockAxiosInstance: any;
9+
let mockProgressCallback: any;
10+
11+
beforeEach(() => {
12+
vi.clearAllMocks();
13+
mockAxiosInstance = vi.fn();
14+
mockProgressCallback = vi.fn();
15+
(axios.create as any) = vi.fn().mockReturnValue(mockAxiosInstance);
16+
(axios.isCancel as any) = vi.fn();
17+
});
18+
19+
it('should throw "Upload aborted" for cancelled requests', async () => {
20+
(axios.isCancel as any).mockReturnValue(true);
21+
mockAxiosInstance.mockRejectedValue(new Error('cancelled'));
22+
23+
await expect(
24+
uploadFileUint8Array(new Uint8Array([1]), 'https://test.com', { progressCallback: mockProgressCallback }),
25+
).rejects.toThrow('Upload aborted');
26+
});
27+
28+
it('should throw "Request has expired" for 403 errors', async () => {
29+
(axios.isCancel as any).mockReturnValue(false);
30+
mockAxiosInstance.mockRejectedValue({ response: { status: 403 } });
31+
32+
await expect(
33+
uploadFileUint8Array(new Uint8Array([1]), 'https://test.com', { progressCallback: mockProgressCallback }),
34+
).rejects.toThrow('Request has expired');
35+
});
36+
37+
it('should re-throw network errors', async () => {
38+
(axios.isCancel as any).mockReturnValue(false);
39+
const networkError = new AxiosError('Network Error');
40+
networkError.message = 'Network Error';
41+
mockAxiosInstance.mockRejectedValue(networkError);
42+
43+
await expect(
44+
uploadFileUint8Array(new Uint8Array([1]), 'https://test.com', { progressCallback: mockProgressCallback }),
45+
).rejects.toBe(networkError);
46+
});
47+
48+
it('should throw "Unknown error" for other errors', async () => {
49+
(axios.isCancel as any).mockReturnValue(false);
50+
mockAxiosInstance.mockRejectedValue(new Error('Other error'));
51+
52+
await expect(
53+
uploadFileUint8Array(new Uint8Array([1]), 'https://test.com', { progressCallback: mockProgressCallback }),
54+
).rejects.toThrow('Unknown error');
55+
});
56+
});

0 commit comments

Comments
 (0)