Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/add-configs-per-…
Browse files Browse the repository at this point in the history
…platform
  • Loading branch information
kuruk-mm committed Nov 7, 2024
2 parents 4bb1945 + ab0651b commit f6a05f1
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 275 deletions.
4 changes: 2 additions & 2 deletions src/components/Pages/RequestPage/RequestPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export const RequestPage = () => {

// Goes to the login page where the user will have to connect a wallet.
const toLoginPage = useCallback(() => {
navigate(locations.login(`/auth/requests/${requestId}`))
navigate(locations.login(`/auth/requests/${requestId}?targetConfigId=${targetConfigId}`))
}, [requestId])

const toSetupPage = useCallback(() => {
navigate(locations.setup(`/auth/requests/${requestId}`))
navigate(locations.setup(`/auth/requests/${requestId}?targetConfigId=${targetConfigId}`))
}, [requestId])

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Pages/SetupPage/SetupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export const SetupPage = () => {
identityRef.current = connectionData.identity
} catch (e) {
console.warn('No previous connection found')
return navigate(locations.login())
return navigate(locations.login(redirectTo))
}

const profile = await fetchProfile(accountRef.current)
Expand Down
251 changes: 106 additions & 145 deletions src/hooks/navigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,201 +14,162 @@ describe('useNavigateWithSearchParams', () => {
;(useLocation as jest.Mock).mockReturnValue(mockLocation)
})

it('navigates to the given path without modifying search params when targetConfigId is not present', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path')
describe('when targetConfigId parameter is not present', () => {
beforeEach(() => {
;(useLocation as jest.Mock).mockReturnValue({ search: '' })
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: undefined
})
})
it('should navigate to the given path without modifying search params', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

it('navigates to the given path with its own search params when targetConfigId is not present', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path?param1=value1&param2=value2')
})
act(() => {
result.current('/test-path')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&param2=value2'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: undefined
})
})
})

it('navigates to the given path and appends targetConfigId when it is present in current location', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=123' })
it('should navigate to the given path with its own search params', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path')
})
act(() => {
result.current('/test-path?param1=value1&param2=value2')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?targetConfigId=123'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&param2=value2'
})
})
})

it('navigates to the given path and merges targetConfigId with existing search params', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=123' })

const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path?param1=value1')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&targetConfigId=123'
describe('when targetConfigId parameter is present', () => {
beforeEach(() => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=123' })
})
})

it('navigates to the given path and overwrites targetConfigId with the one from current location', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=123' })

const { result } = renderHook(() => useNavigateWithSearchParams())
it('should append targetConfigId to the given path', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path?param1=value1&targetConfigId=456')
})
act(() => {
result.current('/test-path')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&targetConfigId=123'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?targetConfigId=123'
})
})
})

it('navigates to the given path and correctly merges multiple search params with targetConfigId', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=123' })

const { result } = renderHook(() => useNavigateWithSearchParams())
it('should merge targetConfigId with existing search params', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path?param1=value1&param2=value2')
})
act(() => {
result.current('/test-path?param1=value1')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&param2=value2&targetConfigId=123'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&targetConfigId=123'
})
})
})

it('navigates to the given path and keeps its own targetConfigId when none is present in current location', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '' })

const { result } = renderHook(() => useNavigateWithSearchParams())
it('should overwrite targetConfigId in the path with the one from current location', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path?targetConfigId=456')
})
act(() => {
result.current('/test-path?param1=value1&targetConfigId=456')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?targetConfigId=456'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&targetConfigId=123'
})
})
})

it('does not include other search params from current location besides targetConfigId', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=123&otherParam=abc' })
it('should correctly merge multiple search params with targetConfigId', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path')
})
act(() => {
result.current('/test-path?param1=value1&param2=value2')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?targetConfigId=123'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1&param2=value2&targetConfigId=123'
})
})
})

it('navigates correctly when provided path is a full URL', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '' })

const { result } = renderHook(() => useNavigateWithSearchParams())
describe('when navigating with different path formats', () => {
it('should handle full URLs with different origins', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('http://localhost/test-path?param1=value1')
})
act(() => {
result.current('http://example.com/test-path?param1=value1')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1'
})
})
})

it('navigates correctly when provided path is a full URL with different origin', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '' })

const { result } = renderHook(() => useNavigateWithSearchParams())
it('should navigate correctly with a relative path', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('http://example.com/test-path?param1=value1')
})
act(() => {
result.current('test-path')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?param1=value1'
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: undefined
})
})
})

it('throws an error when provided path is a malformed URL', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '' })
describe('when targetConfigId parameter has special characters', () => {
beforeEach(() => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=%2F%2Fmalicious.com' })
})

const { result } = renderHook(() => useNavigateWithSearchParams())
it('should encode targetConfigId with special characters', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

expect(() => {
act(() => {
result.current('http://')
result.current('/test-path')
})
}).toThrow('Invalid path provided')
})

it('navigates correctly when provided path is a relative path', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '' })

const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('test-path')
})

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: undefined
expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?targetConfigId=%2F%2Fmalicious.com'
})
})
})

it('encodes targetConfigId when it has special characters', () => {
;(useLocation as jest.Mock).mockReturnValue({ search: '?targetConfigId=%2F%2Fmalicious.com' })

const { result } = renderHook(() => useNavigateWithSearchParams())

act(() => {
result.current('/test-path')
})
describe('when handling errors in navigation', () => {
it('should throw an error when provided path is malformed', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

expect(mockNavigate).toHaveBeenCalledWith({
pathname: '/test-path',
search: '?targetConfigId=%2F%2Fmalicious.com'
expect(() => {
act(() => {
result.current('http://')
})
}).toThrow('Invalid path provided')
})
})

it('throws an error when navigate is called with an empty path', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())
it('should throw an error when navigate is called with an empty path', () => {
const { result } = renderHook(() => useNavigateWithSearchParams())

expect(() => {
act(() => {
result.current('')
})
}).toThrow('Path cannot be empty')
expect(() => {
act(() => {
result.current('')
})
}).toThrow('Path cannot be empty')
})
})
})
Loading

0 comments on commit f6a05f1

Please sign in to comment.