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]);
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,
35 jest.mock('../_user');
36 const mockedUsePublicSessionUser = jest.mocked(usePublicSessionUser).mockReturnValue({
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', () => {
49 ...jest.requireActual('@proton/shared/lib/authentication/persistedSessionHelper'),
50 resumeSession: jest.fn().mockReturnValue({}),
54 const defaultProps = {
55 customPassword: undefined,
59 describe('useBookmarksPublicView', () => {
64 verifyAllWhenMocksCalled();
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,
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);
88 it('Should set bookmarks tokens from listing', async () => {
89 mockedUsePublicSessionUser.mockReturnValueOnce({ user: 'user', UID } as any);
90 mockedListBookmarks.mockResolvedValueOnce([
92 token: { Token: mockedToken },
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();
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);
108 void result.current.addBookmark();
110 await waitFor(() => expect(result.current.isAlreadyBookmarked).toBe(true));