Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / src / app / store / _views / useBookmarksPublicView.test.ts
blob961a125cc8cd2c8648e8fa08e16e56bf2423ed3f
1 import { act, renderHook, waitFor } from '@testing-library/react';
2 import { verifyAllWhenMocksCalled, when } from 'jest-when';
4 import useApi from '@proton/components/hooks/useApi';
5 import useLoading from '@proton/hooks/useLoading';
6 import useFlag from '@proton/unleash/useFlag';
8 import usePublicToken from '../../hooks/drive/usePublicToken';
9 import { useBookmarks } from '../_bookmarks/useBookmarks';
10 import { usePublicSessionUser } from '../_user';
11 import { useBookmarksPublicView } from './useBookmarksPublicView';
13 jest.mock('@proton/unleash/useFlag');
14 const mockedKillSwitch = jest.mocked(useFlag).mockReturnValue(false);
16 jest.mock('@proton/hooks/useLoading');
17 const mockedWithLoading = jest.fn().mockImplementation((fn) => fn());
18 const mockedSetIsLoading = jest.fn();
19 jest.mocked(useLoading).mockReturnValue([true, mockedWithLoading, mockedSetIsLoading]);
21 jest.mock('../_api');
23 jest.mock('@proton/components/hooks/useApi');
24 jest.mocked(useApi).mockReturnValue({ UID: undefined } as any);
26 jest.mock('../_bookmarks/useBookmarks');
27 const mockedListBookmarks = jest.fn().mockResolvedValue([]);
28 const mockedAddBookmarks = jest.fn().mockResolvedValue({});
29 jest.mocked(useBookmarks).mockReturnValue({
30     listBookmarks: mockedListBookmarks as any,
31     addBookmark: mockedAddBookmarks as any,
32     deleteBookmark: jest.fn().mockResolvedValue({}) as any,
33 });
35 jest.mock('../_user');
36 const mockedUsePublicSessionUser = jest.mocked(usePublicSessionUser).mockReturnValue({
37     user: undefined,
38     localID: undefined,
39     UID: '',
40 });
42 const mockedToken = 'token';
43 const mockedUrlPassword = '#password';
44 jest.mock('../../hooks/drive/usePublicToken');
45 jest.mocked(usePublicToken).mockReturnValue({ urlPassword: mockedUrlPassword, token: mockedToken });
47 jest.mock('@proton/shared/lib/authentication/persistedSessionHelper', () => {
48     return {
49         ...jest.requireActual('@proton/shared/lib/authentication/persistedSessionHelper'),
50         resumeSession: jest.fn().mockReturnValue({}),
51     };
52 });
54 const defaultProps = {
55     customPassword: undefined,
57 const UID = 'UID';
59 describe('useBookmarksPublicView', () => {
60     beforeEach(() => {
61         jest.clearAllMocks();
62     });
63     afterAll(() => {
64         verifyAllWhenMocksCalled();
65     });
66     it('Should not list bookmarks if user or UID is undefined or if kill switch enable', () => {
67         const { rerender, result } = renderHook((props) => useBookmarksPublicView(props), {
68             initialProps: defaultProps,
69         });
71         expect(mockedWithLoading).not.toHaveBeenCalled();
72         expect(mockedListBookmarks).not.toHaveBeenCalled();
73         expect(result.current.isAlreadyBookmarked).toBe(false);
75         rerender(defaultProps);
76         expect(mockedWithLoading).not.toHaveBeenCalled();
77         expect(mockedListBookmarks).not.toHaveBeenCalled();
78         expect(result.current.isAlreadyBookmarked).toBe(false);
80         mockedUsePublicSessionUser.mockReturnValueOnce({ user: 'user', UID } as any);
81         mockedKillSwitch.mockReturnValueOnce(true);
82         rerender(defaultProps);
83         expect(mockedWithLoading).not.toHaveBeenCalled();
84         expect(mockedListBookmarks).not.toHaveBeenCalled();
85         expect(result.current.isAlreadyBookmarked).toBe(false);
86     });
88     it('Should set bookmarks tokens from listing', async () => {
89         mockedUsePublicSessionUser.mockReturnValueOnce({ user: 'user', UID } as any);
90         mockedListBookmarks.mockResolvedValueOnce([
91             {
92                 token: { Token: mockedToken },
93             },
94         ]);
96         const { result } = renderHook(() => useBookmarksPublicView(defaultProps));
97         await waitFor(() => expect(mockedListBookmarks).toHaveBeenCalled());
98         await waitFor(() => expect(result.current.isAlreadyBookmarked).toBe(true));
99         expect(mockedWithLoading).toHaveBeenCalled();
100     });
102     it('Should be able to add bookmark', async () => {
103         const { result } = renderHook(() => useBookmarksPublicView(defaultProps));
104         when(mockedAddBookmarks).calledWith({ token: mockedToken, password: mockedUrlPassword });
106         expect(result.current.isAlreadyBookmarked).toBe(false);
107         act(() => {
108             void result.current.addBookmark();
109         });
110         await waitFor(() => expect(result.current.isAlreadyBookmarked).toBe(true));
111     });