Merge branch 'comment-composer' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / store / _uploads / UploadProvider / useUploadQueue.attributes.test.ts
blob295322a8cf9c5851fe976452eb83e8fc64594b49
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 } from './interface';
7 import useUploadQueue from './useUploadQueue';
9 describe("useUploadQueue' attributes", () => {
10     const mockLogCallback = jest.fn();
12     let hook: {
13         current: {
14             hasUploads: boolean;
15             fileUploads: FileUpload[];
16             folderUploads: FolderUpload[];
17             allUploads: (FileUpload | FolderUpload)[];
18             add: (shareId: string, parentId: string, fileList: UploadFileList) => void;
19         };
20     };
22     beforeEach(() => {
23         mockGlobalFile();
24         const { result } = renderHook(() => useUploadQueue(mockLogCallback));
25         hook = result;
26     });
28     it('returns empty queue', () => {
29         expect(hook.current.hasUploads).toBe(false);
30         expect(hook.current.fileUploads).toMatchObject([]);
31         expect(hook.current.folderUploads).toMatchObject([]);
32         expect(hook.current.allUploads).toMatchObject([]);
33     });
35     it('returns folder only', () => {
36         act(() => {
37             hook.current.add('shareId', 'parentId', [{ path: [], folder: 'folder' }]);
38         });
40         const expectedFolder = {
41             // We don't check ID and startDate.
42             shareId: 'shareId',
43             parentId: 'parentId',
44             state: TransferState.Pending,
45             name: 'folder',
46             files: [],
47             folders: [],
48             meta: {
49                 filename: 'folder',
50                 size: 0,
51                 mimeType: 'Folder',
52             },
53         };
55         expect(hook.current.hasUploads).toBe(true);
56         expect(hook.current.fileUploads).toMatchObject([]);
57         expect(hook.current.folderUploads).toMatchObject([expectedFolder]);
58         expect(hook.current.allUploads).toMatchObject([expectedFolder]);
59     });
61     it('returns file only', () => {
62         const file = testFile('file.txt');
63         const dsStore = testFile('.DS_Store');
64         act(() => {
65             hook.current.add('shareId', 'parentId', [
66                 { path: [], file },
67                 { path: [], file: dsStore }, // .DS_Store files are ignored.
68             ]);
69         });
71         const expectedFile = {
72             // We don't check ID and startDate.
73             shareId: 'shareId',
74             parentId: 'parentId',
75             state: TransferState.Pending,
76             file,
77             meta: {
78                 filename: file.name,
79                 mimeType: file.type,
80                 size: file.size,
81             },
82         };
84         expect(hook.current.hasUploads).toBe(true);
85         expect(hook.current.fileUploads).toMatchObject([expectedFile]);
86         expect(hook.current.folderUploads).toMatchObject([]);
87         expect(hook.current.allUploads).toMatchObject([expectedFile]);
88     });
90     it('returns both files and folders', () => {
91         const file = testFile('file.txt');
92         act(() => {
93             hook.current.add('shareId', 'parentId', [
94                 { path: [], folder: 'folder' },
95                 { path: ['folder'], file },
96             ]);
97         });
99         expect(hook.current.hasUploads).toBe(true);
100         expect(hook.current.fileUploads).toMatchObject([
101             {
102                 state: TransferState.Initializing,
103                 parentId: undefined,
104             },
105         ]);
106         expect(hook.current.folderUploads).toMatchObject([
107             {
108                 state: TransferState.Pending,
109                 parentId: 'parentId',
110                 files: [{ meta: { filename: 'file.txt' } }],
111             },
112         ]);
113         expect(hook.current.allUploads.length).toBe(2);
114     });