diff --git a/src/drive/hooks/useRedirectLink.jsx b/src/drive/hooks/useRedirectLink.jsx index 5a888bbf6d..0c8d68fb10 100644 --- a/src/drive/hooks/useRedirectLink.jsx +++ b/src/drive/hooks/useRedirectLink.jsx @@ -43,7 +43,9 @@ const useRedirectLink = () => { searchParams.get('redirectLink') || params.get('redirectLink') const redirectWebLink = useMemo(() => { - if (redirectLink === null && fetchStatus !== 'loaded') return null + if (redirectLink === null || fetchStatus !== 'loaded') { + return null + } const { slug, pathname, hash } = deconstructRedirectLink(redirectLink) const { subdomain: subDomainType } = client.getInstanceOptions() diff --git a/src/drive/hooks/useRedirectLink.spec.jsx b/src/drive/hooks/useRedirectLink.spec.jsx new file mode 100644 index 0000000000..c23928a9b2 --- /dev/null +++ b/src/drive/hooks/useRedirectLink.spec.jsx @@ -0,0 +1,99 @@ +import { renderHook, act } from '@testing-library/react-hooks' +import { useClient } from 'cozy-client' +import { useSearchParams } from 'react-router-dom' +import { useRedirectLink } from './useRedirectLink' + +jest.mock('cozy-client', () => ({ + ...jest.requireActual('cozy-client'), + useClient: jest.fn() +})) +jest.mock('react-router-dom', () => ({ + useSearchParams: jest.fn() +})) + +describe('useRedirectLink', () => { + const mockClient = { + collection: jest.fn().mockReturnValue({ + fetchOwnPermissions: jest.fn().mockResolvedValue({ + included: [] + }) + }), + getStackClient: jest.fn().mockReturnValue({ + uri: 'https://my.cozy.cloud' + }), + getInstanceOptions: jest.fn().mockReturnValue({ + subdomain: 'flat' + }) + } + + beforeEach(() => { + useClient.mockReturnValue(mockClient) + useSearchParams.mockReturnValue([ + new URLSearchParams('?redirectLink=drive%23%2Ffolder%2Fid123') + ]) + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + it('should return null if redirectLink is null', async () => { + useSearchParams.mockReturnValue([new URLSearchParams()]) + + let render + await act(async () => { + render = renderHook(() => useRedirectLink()) + }) + + expect(render.result.current.redirectWebLink).toBeNull() + expect(render.result.current.redirectLink).toBeNull() + }) + + it('should return a redirectWebLink if fetchStatus is loaded', async () => { + let render + await act(async () => { + render = renderHook(() => useRedirectLink()) + }) + + expect(render.result.current.redirectWebLink).toBe( + 'https://my-drive.cozy.cloud/#/folder/id123' + ) + expect(render.result.current.redirectLink).toBe('drive#/folder/id123') + }) + + it('should return a redirectWebLink as null if fetchStatus is failed', async () => { + mockClient + .collection() + .fetchOwnPermissions.mockImplementation(() => Promise.reject()) + + let render + await act(async () => { + render = renderHook(() => useRedirectLink()) + }) + + expect(render.result.current.redirectWebLink).toBeNull() + expect(render.result.current.redirectLink).toBe('drive#/folder/id123') + }) + + it('should return a redirectWebLink to the instance that opened the shared file', async () => { + mockClient.collection().fetchOwnPermissions.mockResolvedValueOnce({ + included: [ + { + attributes: { + instance: 'https://other.cozy.cloud' + } + } + ] + }) + + let render + await act(async () => { + render = renderHook(() => useRedirectLink()) + }) + + expect(render.result.current.redirectWebLink).toBe( + 'https://other-drive.cozy.cloud/#/folder/id123' + ) + expect(render.result.current.redirectLink).toBe('drive#/folder/id123') + }) +})