-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplify routing logic [Part 2]: navigate.tsx (#11186)
* chore: simplify navigate method * fix: broken tests
- Loading branch information
1 parent
6fd2008
commit 0e9cb52
Showing
6 changed files
with
148 additions
and
77 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
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
48 changes: 48 additions & 0 deletions
48
src/app/system/navigation/utils/getValidTargetURL.tests.ts
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,48 @@ | ||
import * as sentry from "@sentry/react-native" | ||
import fetchMock from "jest-fetch-mock" | ||
import { getValidTargetURL } from "./getValidTargetURL" | ||
|
||
describe("getValidTargetURL", () => { | ||
beforeAll(() => { | ||
fetchMock.enableMocks() | ||
}) | ||
|
||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
it("should strip artsy protocol from the URL", async () => { | ||
const url = "artsy://artist/andy-warhol" | ||
const result = await getValidTargetURL(url) | ||
expect(result).toEqual("artist/andy-warhol") | ||
}) | ||
|
||
it("should return the original URL if no redirects are necessary", async () => { | ||
const url = "https://www.artsy.net/artist/andy-warhol" | ||
const result = await getValidTargetURL(url) | ||
expect(result).toEqual(url) | ||
}) | ||
|
||
it("should handle an error during fetch and log it if not in development", async () => { | ||
const url = "https://click.artsy.net/redirect" | ||
fetchMock.mockReject(new Error("Network failure")) | ||
|
||
// @ts-expect-error | ||
global.__DEV__ = false | ||
const spy = jest.spyOn(sentry, "captureMessage").mockImplementation() | ||
|
||
const result = await getValidTargetURL(url) | ||
expect(result).toEqual(url) | ||
expect(spy).toBeCalled() | ||
spy.mockRestore() | ||
}) | ||
|
||
it("should correctly resolve redirected URL", async () => { | ||
const originalUrl = "https://click.artsy.net/redirect" | ||
const redirectedUrl = "https://www.artsy.net/feature" | ||
fetchMock.mockResponseOnce("", { url: redirectedUrl }) | ||
|
||
const result = await getValidTargetURL(originalUrl) | ||
expect(result).toEqual(redirectedUrl) | ||
}) | ||
}) |
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,36 @@ | ||
import { captureMessage } from "@sentry/react-native" | ||
|
||
/** | ||
* This helper function is used to convert the href to a valid screen name that can be navigated to. | ||
* This is required because the href can be a non trivial marketing url that needs additional handling. | ||
* @param url | ||
* @returns targetURL | ||
*/ | ||
export const getValidTargetURL = async (url: string) => { | ||
let targetURL = url | ||
|
||
targetURL = url.replace("artsy://", "") | ||
|
||
// marketing url requires redirect | ||
if (targetURL.startsWith("https://click.artsy.net")) { | ||
let response | ||
try { | ||
response = await fetch(targetURL) | ||
} catch (error) { | ||
if (__DEV__) { | ||
console.warn(error) | ||
} else { | ||
captureMessage( | ||
`[navigate] Error fetching marketing url redirect on: ${targetURL} failed with error: ${error}`, | ||
"error" | ||
) | ||
} | ||
} | ||
|
||
if (response?.url) { | ||
targetURL = response.url | ||
} | ||
} | ||
|
||
return targetURL | ||
} |