1 import { act, renderHook } from '@testing-library/react-hooks';
3 import { VolumesStateProvider } from '../_volumes/useVolumesState';
4 import useDefaultShare from './useDefaultShare';
6 const mockRequest = jest.fn();
7 const mockCreateVolume = jest.fn();
8 const mockGetDefaultShareId = jest.fn();
9 const mockGetShare = jest.fn();
10 const mockGetShareWithKey = jest.fn();
12 jest.mock('../_api/useDebouncedRequest', () => {
13 const useDebouncedRequest = () => {
16 return useDebouncedRequest;
19 jest.mock('../_api/useDebouncedRequest', () => {
20 const useDebouncedRequest = () => {
23 return useDebouncedRequest;
26 jest.mock('../_crypto/useDriveCrypto', () => {
27 const useDriveCrypto = () => {
29 getOwnAddressAndPrimaryKeys: () => {},
33 return useDriveCrypto;
36 jest.mock('../_utils/useDebouncedFunction', () => {
37 const useDebouncedFunction = () => {
38 return (wrapper: any) => wrapper();
40 return useDebouncedFunction;
43 jest.mock('./useSharesState', () => {
44 const useSharesState = () => {
47 getDefaultShareId: mockGetDefaultShareId,
52 ...jest.requireActual('./useSharesState'),
54 default: useSharesState,
58 jest.mock('../_shares/useShare', () => {
59 const useLink = () => {
61 getShare: mockGetShare,
62 getShareWithKey: mockGetShareWithKey,
68 jest.mock('./useVolume', () => {
69 const useVolume = () => {
71 createVolume: mockCreateVolume,
77 describe('useDefaultShare', () => {
79 current: ReturnType<typeof useDefaultShare>;
82 const defaultShareId = Symbol('shareId');
84 const ac = new AbortController();
89 mockCreateVolume.mockImplementation(async () => {
90 return { shareId: defaultShareId };
93 mockRequest.mockImplementation(async () => {
94 return { Shares: [] };
97 const wrapper = ({ children }: { children: React.ReactNode }) => (
98 <VolumesStateProvider>{children}</VolumesStateProvider>
101 const { result } = renderHook(() => useDefaultShare(), { wrapper });
105 it('creates a volume if existing shares are locked/soft deleted', async () => {
106 mockGetDefaultShareId.mockImplementation(() => {
107 // no valid shares were found
111 await act(async () => {
112 await hook.current.getDefaultShare();
115 expect(mockCreateVolume.mock.calls.length).toBe(1);
116 expect(mockGetShareWithKey).toHaveBeenCalledWith(expect.anything(), defaultShareId);
119 it('creates a volume if no shares exist', async () => {
120 mockRequest.mockImplementation(async () => {
121 return { Shares: [] };
124 await act(async () => {
125 await hook.current.getDefaultShare();
128 expect(mockCreateVolume.mock.calls.length).toBe(1);
129 expect(mockGetShareWithKey).toHaveBeenCalledWith(expect.anything(), defaultShareId);
132 it("creates a volume if default share doesn't exist", async () => {
133 mockRequest.mockImplementation(async () => {
143 await act(async () => {
144 await hook.current.getDefaultShare();
147 expect(mockCreateVolume.mock.calls.length).toBe(1);
148 expect(mockGetShareWithKey).toHaveBeenCalledWith(expect.anything(), defaultShareId);
151 it('says share is available by default', async () => {
152 mockGetShare.mockImplementation(async () => ({}));
154 await act(async () => {
155 const isAvailable = await hook.current.isShareAvailable(ac.signal, 'shareId');
156 expect(isAvailable).toBeTruthy();
160 it('says share is not available if locked', async () => {
161 mockGetShare.mockImplementation(async () => {
167 await act(async () => {
168 const isAvailable = await hook.current.isShareAvailable(ac.signal, 'shareId');
169 expect(isAvailable).toBeFalsy();
173 it('says share is not available if soft deleted', async () => {
174 mockGetShare.mockImplementation(async () => {
176 isVolumeSoftDeleted: true,
180 await act(async () => {
181 const isAvailable = await hook.current.isShareAvailable(ac.signal, 'shareId');
182 expect(isAvailable).toBeFalsy();