Merge branch 'MAILWEB-6067-improve-circular-dependencies-prevention' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / store / _shares / useSharesState.test.tsx
blob50b9b744f42b87c95da2543bc3162ca9a7ad5bda
1 import { act, renderHook } from '@testing-library/react-hooks';
3 import type { Share } from './interface';
4 import { ShareState, ShareType } from './interface';
5 import { useSharesStateProvider } from './useSharesState';
7 function createTestShare(
8     shareId: string,
9     volumeId: string,
10     flags = {
11         isLocked: false,
12         isDefault: false,
13         isVolumeSoftDeleted: false,
14         type: ShareType.standard,
15         state: ShareState.active,
16     }
17 ): Share {
18     return {
19         shareId,
20         rootLinkId: 'linkId',
21         volumeId,
22         creator: 'creator',
23         ...flags,
24         possibleKeyPackets: [],
25         createTime: 1234,
26     };
29 describe('useSharesState', () => {
30     let hook: {
31         current: ReturnType<typeof useSharesStateProvider>;
32     };
34     const mainShare1 = createTestShare('mainShare1', 'volume1', {
35         isLocked: true,
36         isDefault: true,
37         isVolumeSoftDeleted: false,
38         type: ShareType.default,
39         state: ShareState.active,
40     });
41     const device1 = createTestShare('device1', 'volume1', {
42         isLocked: true,
43         isDefault: false,
44         isVolumeSoftDeleted: false,
45         type: ShareType.device,
46         state: ShareState.active,
47     });
48     const device2 = createTestShare('device2', 'volume1', {
49         isLocked: true,
50         isDefault: false,
51         isVolumeSoftDeleted: false,
52         type: ShareType.device,
53         state: ShareState.active,
54     });
55     const share1 = createTestShare('share1', 'volume1', {
56         isLocked: true,
57         isDefault: false,
58         isVolumeSoftDeleted: false,
59         type: ShareType.standard,
60         state: ShareState.active,
61     });
62     const mainShare2 = createTestShare('mainShare2', 'volume2', {
63         isLocked: true,
64         isDefault: true,
65         isVolumeSoftDeleted: false,
66         type: ShareType.default,
67         state: ShareState.active,
68     });
69     const device3 = createTestShare('device3', 'volume2', {
70         isLocked: true,
71         isDefault: false,
72         isVolumeSoftDeleted: false,
73         type: ShareType.device,
74         state: ShareState.active,
75     });
76     const share2 = createTestShare('share2', 'volume2', {
77         isLocked: true,
78         isDefault: false,
79         isVolumeSoftDeleted: false,
80         type: ShareType.standard,
81         state: ShareState.active,
82     });
83     const mainShare3 = createTestShare('mainShare3', 'volume3', {
84         isLocked: false,
85         isDefault: true,
86         isVolumeSoftDeleted: false,
87         type: ShareType.default,
88         state: ShareState.active,
89     });
90     const device4 = createTestShare('device4', 'volume3', {
91         isLocked: false,
92         isDefault: false,
93         isVolumeSoftDeleted: false,
94         type: ShareType.device,
95         state: ShareState.active,
96     });
97     const mainShare4 = createTestShare('mainShare4', 'volume4', {
98         isLocked: true,
99         isDefault: true,
100         isVolumeSoftDeleted: true,
101         type: ShareType.default,
102         state: ShareState.active,
103     });
104     const device5 = createTestShare('device5', 'volume4', {
105         isLocked: true,
106         isDefault: false,
107         isVolumeSoftDeleted: true,
108         type: ShareType.device,
109         state: ShareState.active,
110     });
112     beforeEach(() => {
113         jest.resetAllMocks();
115         const { result } = renderHook(() => useSharesStateProvider());
116         hook = result;
118         act(() => {
119             hook.current.setShares([
120                 mainShare1,
121                 device1,
122                 device2,
123                 share1,
124                 mainShare2,
125                 device3,
126                 share2,
127                 mainShare3,
128                 device4,
129                 mainShare4,
130                 device5,
131             ]);
132         });
133     });
135     it('returns only locked undeleted shares with its devices', async () => {
136         const lockedShares = hook.current.getLockedShares();
137         expect(lockedShares).toMatchObject([
138             {
139                 defaultShare: mainShare1,
140                 devices: [device1, device2],
141             },
142             {
143                 defaultShare: mainShare2,
144                 devices: [device3],
145             },
146         ]);
147     });