Merge branch 'comment-composer' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / store / _uploads / UploadProvider / useUploadQueue.remove.test.ts
blobea1a07cf13457b869470925280b27e2ea43a638d
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();
12     let hook: {
13         current: {
14             fileUploads: FileUpload[];
15             folderUploads: FolderUpload[];
16             add: (shareId: string, parentId: string, fileList: UploadFileList) => void;
17             remove: (idOrFilter: UpdateFilter, callback?: UpdateCallback) => void;
18         };
19     };
21     let firstFileId: string;
22     let firstFolderId: string;
23     let secondIds: string[];
25     beforeEach(() => {
26         mockCallback.mockClear();
28         mockGlobalFile();
30         const { result } = renderHook(() => useUploadQueue(mockLogCallback));
31         hook = result;
33         act(() => {
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') },
42             ]);
43         });
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];
48     });
50     it('removes file from the queue using id', () => {
51         act(() => {
52             hook.current.remove(firstFileId, mockCallback);
53         });
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' } },
59         ]);
60         expect(hook.current.fileUploads).toMatchObject([
61             { meta: { filename: 'file2.txt' } },
62             { meta: { filename: 'file3.txt' } },
63             { meta: { filename: 'file4.txt' } },
64         ]);
65     });
67     it('removes folder from the queue using id', () => {
68         act(() => {
69             hook.current.remove(firstFolderId, mockCallback);
70         });
71         expect(mockCallback.mock.calls).toMatchObject([
72             [{ meta: { filename: 'folder1' } }],
73             [{ meta: { filename: 'file2.txt' } }],
74             [{ meta: { filename: 'file3.txt' } }],
75         ]);
76         expect(hook.current.folderUploads).toMatchObject([
77             { meta: { filename: 'folder2' } },
78             { meta: { filename: 'folder3' } },
79         ]);
80         expect(hook.current.fileUploads).toMatchObject([
81             { meta: { filename: 'file1.txt' } },
82             { meta: { filename: 'file4.txt' } },
83         ]);
84     });
86     it('removes file and folder from the queue using filter', () => {
87         act(() => {
88             hook.current.remove(({ id }) => secondIds.includes(id), mockCallback);
89         });
90         expect(mockCallback.mock.calls).toMatchObject([
91             [{ meta: { filename: 'folder2' } }],
92             [{ meta: { filename: 'file4.txt' } }],
93             [{ meta: { filename: 'file2.txt' } }],
94         ]);
95         expect(hook.current.folderUploads).toMatchObject([
96             { meta: { filename: 'folder1' } },
97             { meta: { filename: 'folder3' } },
98         ]);
99         expect(hook.current.fileUploads).toMatchObject([
100             { meta: { filename: 'file1.txt' } },
101             { meta: { filename: 'file3.txt' } },
102         ]);
103     });