Skip to content

Commit

Permalink
fix: Open OnlyOffice file without redirect link
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Oct 13, 2023
1 parent 210faf6 commit 5420230
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/drive/hooks/useRedirectLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
99 changes: 99 additions & 0 deletions src/drive/hooks/useRedirectLink.spec.jsx
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 5420230

Please sign in to comment.