-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid making home view query requests when opening deep links
Co-authored-by: Ole <[email protected]> Co-authored-by: Anandaroop Roy <[email protected]>
- Loading branch information
1 parent
0ac34d0
commit 4ea8724
Showing
4 changed files
with
111 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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,54 @@ | ||
import { renderHook } from "@testing-library/react-hooks" | ||
import { useIsDeepLink } from "app/utils/hooks/useIsDeepLink" | ||
import { Linking } from "react-native" | ||
|
||
// Existing mocks | ||
const mockUseIsFocusedMock = jest.fn() | ||
jest.mock("@react-navigation/native", () => ({ | ||
useIsFocused: () => mockUseIsFocusedMock(), | ||
})) | ||
|
||
jest.mock("react-native", () => ({ | ||
Linking: { | ||
getInitialURL: jest.fn(), | ||
}, | ||
})) | ||
|
||
// Start of the new test case | ||
describe("useIsDeepLink", () => { | ||
const mockLinkingGetInitialURL = Linking.getInitialURL as jest.Mock | ||
|
||
it("should return true if opened from a deep link", async () => { | ||
// Setup the mock to return the specific URL | ||
mockLinkingGetInitialURL.mockResolvedValue("artst.net/foo/bar") | ||
mockUseIsFocusedMock.mockReturnValue(true) | ||
|
||
// Render the hook under test | ||
const { result, waitForNextUpdate } = renderHook(() => useIsDeepLink()) | ||
|
||
// Wait for async effects to resolve | ||
await waitForNextUpdate() | ||
|
||
expect(result.current).toEqual({ | ||
isDeepLink: true, | ||
}) | ||
expect(mockUseIsFocusedMock).toHaveBeenCalled() | ||
expect(mockLinkingGetInitialURL).toHaveBeenCalled() | ||
}) | ||
|
||
it("should return false if not opened from a deep link", async () => { | ||
// Setup the mock to return null | ||
mockLinkingGetInitialURL.mockResolvedValue(null) | ||
mockUseIsFocusedMock.mockReturnValue(true) | ||
|
||
// Render the hook under test | ||
const { result, waitForNextUpdate } = renderHook(() => useIsDeepLink()) | ||
|
||
// Wait for async effects to resolve | ||
await waitForNextUpdate() | ||
|
||
expect(result.current.isDeepLink).toEqual(false) | ||
expect(mockUseIsFocusedMock).toHaveBeenCalled() | ||
expect(mockLinkingGetInitialURL).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains 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,42 @@ | ||
import { useIsFocused } from "@react-navigation/native" | ||
import { useEffect, useState } from "react" | ||
import { Linking } from "react-native" | ||
|
||
/** | ||
* This is a hook that returns whether the user came from a deep link or not | ||
* This can be used to avoid rendering content in previous screens in react-navigation history | ||
* @returns {isDeepLink: boolean | null} isDeepLink is true if the user came from a deep link | ||
*/ | ||
export const useIsDeepLink = () => { | ||
const [isDeepLink, setIsDeepLink] = useState<boolean | null>(null) | ||
|
||
const isFocused = useIsFocused() | ||
|
||
useEffect(() => { | ||
// When the user comes back from a deep link, | ||
// we want to show content again | ||
if (isFocused && isDeepLink === true) { | ||
setIsDeepLink(false) | ||
} | ||
}, [isFocused]) | ||
|
||
useEffect(() => { | ||
Linking.getInitialURL() | ||
.then((url) => { | ||
const isDeepLink = !!url | ||
if (!isDeepLink) { | ||
setIsDeepLink(false) | ||
} else { | ||
setIsDeepLink(true) | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error("Error getting initial URL", error) | ||
setIsDeepLink(false) | ||
}) | ||
}, []) | ||
|
||
return { | ||
isDeepLink, | ||
} | ||
} |