1 import { act, renderHook } from '@testing-library/react-hooks';
3 import { TransferState } from '../../../components/TransferManager/transfer';
4 import { mockGlobalFile, testFile } from '../../../utils/test/file';
5 import type { UploadFileList } from '../interface';
6 import type { FileUpload, FolderUpload, UploadQueue } from './interface';
7 import useUploadQueue, { addItemToQueue } from './useUploadQueue';
9 function createEmptyQueue(): UploadQueue {
18 describe('useUploadQueue::add', () => {
19 const mockLogCallback = jest.fn();
23 fileUploads: FileUpload[];
24 folderUploads: FolderUpload[];
25 add: (shareId: string, parentId: string, fileList: UploadFileList) => void;
31 const { result } = renderHook(() => useUploadQueue(mockLogCallback));
35 it('creates new upload queue', () => {
37 hook.current.add('shareId', 'parentId', [{ path: [], folder: 'folder' }]);
38 hook.current.add('shareId2', 'parentId2', [{ path: [], folder: 'folder' }]);
40 expect(hook.current.folderUploads).toMatchObject([
49 parentId: 'parentId2',
54 it('merges upload queue', () => {
56 hook.current.add('shareId', 'parentId', [{ path: [], folder: 'folder' }]);
57 hook.current.add('shareId', 'parentId', [{ path: [], folder: 'folder2' }]);
59 expect(hook.current.folderUploads).toMatchObject([
73 it('throws error when adding file with empty name', () => {
75 addItemToQueue(mockLogCallback, 'shareId', createEmptyQueue(), { path: [], file: testFile('') });
76 }).toThrow('File or folder is missing a name');
79 it('throws error when adding folder with empty name', () => {
81 addItemToQueue(mockLogCallback, 'shareId', createEmptyQueue(), { path: [], folder: '' });
82 }).toThrow('File or folder is missing a name');
85 it('throws error when adding file to non-existing folder', () => {
87 addItemToQueue(mockLogCallback, 'shareId', createEmptyQueue(), {
89 file: testFile('a.txt'),
91 }).toThrow('Wrong file or folder structure');
94 it('throws error when adding the same file again', () => {
95 const queue = createEmptyQueue();
96 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], file: testFile('a.txt') });
98 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], file: testFile('a.txt') });
99 }).toThrow('File or folder "a.txt" is already uploading');
100 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
101 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], file: testFile('a.txt') });
103 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], file: testFile('a.txt') });
104 }).toThrow('File or folder "a.txt" is already uploading');
107 it('throws error when adding the same folder again', () => {
108 const queue = createEmptyQueue();
109 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
111 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
112 }).toThrow('File or folder "folder" is already uploading');
113 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], folder: 'subfolder' });
115 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], folder: 'subfolder' });
116 }).toThrow('File or folder "subfolder" is already uploading');
119 it('throws error when adding the same folder again with unfinished childs', () => {
120 const queue = createEmptyQueue();
121 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
122 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], file: testFile('a.txt') });
124 queue.folders[0].state = TransferState.Done;
126 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
127 }).toThrow('File or folder "folder" is already uploading');
129 queue.folders[0].files[0].state = TransferState.Done;
130 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
133 it('adds files to the latest folder', () => {
134 const queue = createEmptyQueue();
135 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
136 queue.folders[0].state = TransferState.Done;
138 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
139 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], file: testFile('b.txt') });
140 expect(queue.folders[0].files.length).toBe(0);
141 expect(queue.folders[1].files.length).toBe(1);
142 expect(queue.folders[1].files[0].meta.filename).toBe('b.txt');
145 it('adds files to already prepared filter with pending state', () => {
146 const queue = createEmptyQueue();
147 addItemToQueue(mockLogCallback, 'shareId', queue, { path: [], folder: 'folder' });
149 // The first file, before folder is done, is set to init state.
150 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], file: testFile('a.txt') });
151 expect(queue.folders[0].files[0]).toMatchObject({
152 meta: { filename: 'a.txt' },
153 state: TransferState.Initializing,
156 // The second file, after folder is done, is set to pending state.
157 queue.folders[0].state = TransferState.Done;
158 queue.folders[0].linkId = 'folderId';
159 addItemToQueue(mockLogCallback, 'shareId', queue, { path: ['folder'], file: testFile('b.txt') });
160 expect(queue.folders[0].files[1]).toMatchObject({
161 meta: { filename: 'b.txt' },
162 state: TransferState.Pending,