-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix deeplink handler #9919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
enahum
wants to merge
3
commits into
main
Choose a base branch
from
deeplinks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix deeplink handler #9919
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| import {Redirect} from 'expo-router'; | ||
| import {Redirect, useLocalSearchParams} from 'expo-router'; | ||
|
|
||
| export default function HomeIndex() { | ||
| return <Redirect href='/(authenticated)/(home)/channel_list'/>; | ||
| const params = useLocalSearchParams(); | ||
|
|
||
| return <Redirect href={{pathname: '/(authenticated)/(home)/channel_list', params}}/>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| import {Linking} from 'react-native'; | ||
|
|
||
| import {Sso} from '@constants'; | ||
| import {alertInvalidDeepLink, parseAndHandleDeepLink} from '@utils/deep_link'; | ||
|
|
||
| import {addEventListener, redirectSystemPath} from './+native-intent'; | ||
|
|
||
| jest.mock('@utils/deep_link'); | ||
|
|
||
| describe('native-intent', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| (parseAndHandleDeepLink as jest.Mock).mockResolvedValue({error: false}); | ||
| }); | ||
|
|
||
| describe('addEventListener', () => { | ||
| it('should subscribe to Linking url events and return an unsubscribe function', () => { | ||
| const remove = jest.fn(); | ||
| (Linking.addEventListener as jest.Mock).mockReturnValue({remove}); | ||
|
|
||
| const unsubscribe = addEventListener(); | ||
|
|
||
| expect(Linking.addEventListener).toHaveBeenCalledWith('url', expect.any(Function)); | ||
|
|
||
| unsubscribe(); | ||
|
|
||
| expect(remove).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('redirectSystemPath', () => { | ||
| it('should return the path unchanged when the url is not handled, regardless of initial', async () => { | ||
| const path = 'mailto:someone@example.com'; | ||
|
|
||
| expect(await redirectSystemPath({path, initial: false})).toBe(path); | ||
| expect(await redirectSystemPath({path, initial: true})).toBe(path); | ||
| expect(parseAndHandleDeepLink).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return null when the url is an SSO redirect, regardless of initial', async () => { | ||
| const path = `${Sso.REDIRECT_URL_SCHEME}some/path`; | ||
|
|
||
| expect(await redirectSystemPath({path, initial: false})).toBeNull(); | ||
| expect(await redirectSystemPath({path, initial: true})).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null and not alert when the deep link is handled successfully, regardless of initial', async () => { | ||
| const path = 'https://community.mattermost.com/team/channels/town-square'; | ||
|
|
||
| expect(await redirectSystemPath({path, initial: false})).toBeNull(); | ||
| expect(await redirectSystemPath({path, initial: true})).toBeNull(); | ||
|
|
||
| expect(parseAndHandleDeepLink).toHaveBeenCalledWith(path, undefined, undefined, true); | ||
| expect(alertInvalidDeepLink).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return the path and alert when the deep link handling errors, regardless of initial', async () => { | ||
| (parseAndHandleDeepLink as jest.Mock).mockResolvedValue({error: true}); | ||
|
|
||
| const path = 'https://community.mattermost.com/team/channels/town-square'; | ||
|
|
||
| expect(await redirectSystemPath({path, initial: false})).toBe(path); | ||
| expect(await redirectSystemPath({path, initial: true})).toBe(path); | ||
|
|
||
| expect(alertInvalidDeepLink).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleUrl (via addEventListener subscription callback)', () => { | ||
| const getHandler = (): (event: {url: string}) => Promise<boolean> => { | ||
| addEventListener(); | ||
| const call = (Linking.addEventListener as jest.Mock).mock.calls[0]; | ||
| return call[1]; | ||
| }; | ||
|
|
||
| it('should return false for a url with a protocol but no host', async () => { | ||
| const handleUrl = getHandler(); | ||
|
|
||
| const result = await handleUrl({url: 'mailto:someone@example.com'}); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(parseAndHandleDeepLink).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return true and skip deep link handling for the SSO redirect scheme', async () => { | ||
| const handleUrl = getHandler(); | ||
|
|
||
| const result = await handleUrl({url: `${Sso.REDIRECT_URL_SCHEME}callback`}); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(parseAndHandleDeepLink).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return true and skip deep link handling for the SSO dev redirect scheme', async () => { | ||
| const handleUrl = getHandler(); | ||
|
|
||
| const result = await handleUrl({url: `${Sso.REDIRECT_URL_SCHEME_DEV}callback`}); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(parseAndHandleDeepLink).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return true when the deep link is handled successfully', async () => { | ||
| const handleUrl = getHandler(); | ||
|
|
||
| const result = await handleUrl({url: 'https://community.mattermost.com/team/channels/town-square'}); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(alertInvalidDeepLink).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should alert and return false when the deep link errors', async () => { | ||
| (parseAndHandleDeepLink as jest.Mock).mockResolvedValue({error: true}); | ||
| const handleUrl = getHandler(); | ||
|
|
||
| const result = await handleUrl({url: 'https://community.mattermost.com/team/channels/town-square'}); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(alertInvalidDeepLink).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return false when the url is empty', async () => { | ||
| const handleUrl = getHandler(); | ||
|
|
||
| const result = await handleUrl({url: ''}); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(parseAndHandleDeepLink).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to use propsToParams here and other similar places in this PR? Is it in order to stringify non string values that is causing some issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes is needed as the router uses a query string and passing the object would be serialized to "[Object object]" this was an oversight on my part before.. you can see the navigation file, navigateToScreen where it first converts the props to params