-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathuseMultiFileUpload.test.js
167 lines (133 loc) · 5.15 KB
/
useMultiFileUpload.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { renderHook, waitFor } from '@testing-library/react';
import { useMultiFileUpload } from './useMultiFileUpload';
describe('useMultiFileUpload Hook', () => {
// Mock file creation utility
const createMockFile = (name = 'test.txt', size = 1000, type = 'text/plain') =>
new File([new ArrayBuffer(size)], name, { type });
// Mock upload function
const mockOnUpload = jest.fn(() => Promise.resolve());
beforeEach(() => {
jest.clearAllMocks();
});
describe('addFiles', () => {
it('should add files to the queue', async () => {
const { result } = renderHook(() =>
useMultiFileUpload({
urlPath: '/upload',
onUpload: mockOnUpload,
}),
);
const mockFiles = [createMockFile('file1.txt'), createMockFile('file2.txt')];
await waitFor(() => result.current.addFiles(mockFiles));
expect(result.current.files).toHaveLength(2);
expect(result.current.files[0].status).toBe('queued');
});
});
describe('clearFiles', () => {
it('should clear all added files in the queue', async () => {
const { result } = renderHook(() =>
useMultiFileUpload({
urlPath: '/upload',
onUpload: mockOnUpload,
}),
);
const mockFiles = [createMockFile('file1.txt'), createMockFile('file2.txt')];
await waitFor(() => result.current.addFiles(mockFiles));
expect(result.current.files).toHaveLength(2);
expect(result.current.files[0].status).toBe('queued');
await waitFor(() => result.current.clearFiles());
expect(result.current.files).toHaveLength(0);
});
});
describe('removeFile', () => {
it('should remove a specific file from the queue', async () => {
const { result } = renderHook(() =>
useMultiFileUpload({
urlPath: '/upload',
onUpload: mockOnUpload,
}),
);
const mockFiles = [createMockFile('file1.txt'), createMockFile('file2.txt')];
await waitFor(() => {
result.current.addFiles(mockFiles);
});
const fileIdToRemove = result.current.files[0].id;
await waitFor(() => {
result.current.removeFile(fileIdToRemove);
});
expect(result.current.files).toHaveLength(1);
expect(result.current.files[0].data.name).toBe('file2.txt');
});
});
describe('startUploads', () => {
it('should start uploading queued files', async () => {
const mockOnUpload = jest.fn(() => Promise.resolve());
const { result } = renderHook(() =>
useMultiFileUpload({
onUpload: mockOnUpload,
}),
);
const mockFiles = [
createMockFile('file1.txt'),
createMockFile('file2.txt'),
createMockFile('file3.txt'),
];
await waitFor(() => result.current.addFiles(mockFiles));
await waitFor(() => result.current.startUploads());
await waitFor(() => {
const successful = result.current.files.filter((f) => f.status === 'success');
if (successful.length > 0) {
expect(successful).toHaveLength(mockFiles.length);
}
});
expect(mockOnUpload).toHaveBeenCalledTimes(mockFiles.length);
expect(result.current.isUploading).toEqual('complete');
expect(result.current.files).toHaveLength(mockFiles.length);
});
it('should handle error uploads', async () => {
const errorMessage = 'Roro';
const mockOnUpload = jest.fn(() => Promise.reject(new Error(errorMessage)));
const { result } = renderHook(() =>
useMultiFileUpload({
urlPath: '/upload',
onUpload: mockOnUpload,
}),
);
const mockFiles = [createMockFile('file1.txt')];
await waitFor(() => result.current.addFiles(mockFiles));
await waitFor(() => result.current.startUploads());
await waitFor(() =>
expect(result.current.files.filter((f) => f.status === 'error')).toHaveLength(
mockFiles.length,
),
);
const errored = result.current.files.filter((f) => f.status === 'error');
expect(mockOnUpload).toHaveBeenCalledTimes(mockFiles.length);
expect(result.current.files).toHaveLength(mockFiles.length);
errored.map((file) => expect(file.message).toEqual(errorMessage));
});
it('should handle error with default error message', async () => {
const mockOnUpload = jest.fn(() => Promise.reject());
const { result } = renderHook(() =>
useMultiFileUpload({
urlPath: '/upload',
onUpload: mockOnUpload,
}),
);
const mockFiles = [createMockFile('file1.txt')];
await waitFor(() => result.current.addFiles(mockFiles));
await waitFor(() => result.current.startUploads());
await waitFor(() =>
expect(result.current.files.filter((f) => f.status === 'error')).toHaveLength(
mockFiles.length,
),
);
const errored = result.current.files.filter((f) => f.status === 'error');
expect(mockOnUpload).toHaveBeenCalledTimes(mockFiles.length);
expect(result.current.files).toHaveLength(mockFiles.length);
errored.map((file) =>
expect(file.message).toEqual(`Unable to upload ${file.data.name}`),
);
});
});
});