1 import { act, renderHook } from '@testing-library/react-hooks';
3 import { mockGlobalFile, testFile } from '../../../utils/test/file';
4 import type { UploadFileList } from '../interface';
5 import type { FileUpload, FolderUpload, UpdateCallback, UpdateFilter } from './interface';
6 import useUploadQueue from './useUploadQueue';
8 describe('useUploadQueue::remove', () => {
9 const mockCallback = jest.fn();
10 const mockLogCallback = jest.fn();
14 fileUploads: FileUpload[];
15 folderUploads: FolderUpload[];
16 add: (shareId: string, parentId: string, fileList: UploadFileList) => void;
17 remove: (idOrFilter: UpdateFilter, callback?: UpdateCallback) => void;
21 let firstFileId: string;
22 let firstFolderId: string;
23 let secondIds: string[];
26 mockCallback.mockClear();
30 const { result } = renderHook(() => useUploadQueue(mockLogCallback));
34 hook.current.add('shareId', 'parentId', [
35 { path: [], folder: 'folder1' },
36 { path: [], folder: 'folder2' },
37 { path: [], folder: 'folder3' },
38 { path: [], file: testFile('file1.txt') },
39 { path: ['folder1'], file: testFile('file2.txt') },
40 { path: ['folder1'], file: testFile('file3.txt') },
41 { path: ['folder2'], file: testFile('file4.txt') },
45 firstFileId = hook.current.fileUploads[0].id;
46 firstFolderId = hook.current.folderUploads[0].id;
47 secondIds = [hook.current.fileUploads[1].id, hook.current.folderUploads[1].id];
50 it('removes file from the queue using id', () => {
52 hook.current.remove(firstFileId, mockCallback);
54 expect(mockCallback.mock.calls).toMatchObject([[{ meta: { filename: 'file1.txt' } }]]);
55 expect(hook.current.folderUploads).toMatchObject([
56 { meta: { filename: 'folder1' } },
57 { meta: { filename: 'folder2' } },
58 { meta: { filename: 'folder3' } },
60 expect(hook.current.fileUploads).toMatchObject([
61 { meta: { filename: 'file2.txt' } },
62 { meta: { filename: 'file3.txt' } },
63 { meta: { filename: 'file4.txt' } },
67 it('removes folder from the queue using id', () => {
69 hook.current.remove(firstFolderId, mockCallback);
71 expect(mockCallback.mock.calls).toMatchObject([
72 [{ meta: { filename: 'folder1' } }],
73 [{ meta: { filename: 'file2.txt' } }],
74 [{ meta: { filename: 'file3.txt' } }],
76 expect(hook.current.folderUploads).toMatchObject([
77 { meta: { filename: 'folder2' } },
78 { meta: { filename: 'folder3' } },
80 expect(hook.current.fileUploads).toMatchObject([
81 { meta: { filename: 'file1.txt' } },
82 { meta: { filename: 'file4.txt' } },
86 it('removes file and folder from the queue using filter', () => {
88 hook.current.remove(({ id }) => secondIds.includes(id), mockCallback);
90 expect(mockCallback.mock.calls).toMatchObject([
91 [{ meta: { filename: 'folder2' } }],
92 [{ meta: { filename: 'file4.txt' } }],
93 [{ meta: { filename: 'file2.txt' } }],
95 expect(hook.current.folderUploads).toMatchObject([
96 { meta: { filename: 'folder1' } },
97 { meta: { filename: 'folder3' } },
99 expect(hook.current.fileUploads).toMatchObject([
100 { meta: { filename: 'file1.txt' } },
101 { meta: { filename: 'file3.txt' } },