Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

openExternalUrl.test.ts

49 lines1.8 KB

1import { Linking } from "react-native";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import { tryOpenExternalUrl, tryOpenWebLink } from "./openExternalUrl";
5​
6vi.mock("react-native", () => ({
7 Linking: { openURL: vi.fn() },
8}));
9​
10const openURL = vi.mocked(Linking.openURL);
11​
12beforeEach(() => {
13 vi.clearAllMocks();
14});
15​
16afterEach(() => {
17 vi.restoreAllMocks();
18});
19​
20describe("tryOpenExternalUrl", () => {
21 it("keeps provider sign-in URLs unchanged and out of failure logs", async () => {
22 const url =
23 "https://accounts.google.com/o/oauth2/v2/auth?state=private-state&code_challenge=private-challenge&redirect_uri=http%3A%2F%2F127.0.0.1%3A43123%2F";
24 openURL.mockRejectedValue(new Error(`Cannot open ${url}`));
25 const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
26​
27 await expect(tryOpenExternalUrl(url, "provider-auth")).resolves.toBe(false);
28​
29 expect(openURL).toHaveBeenCalledWith(url);
30 const diagnostics = JSON.stringify(consoleError.mock.calls);
31 expect(diagnostics).toContain("accounts.google.com");
32 expect(diagnostics).not.toContain("private-state");
33 expect(diagnostics).not.toContain("private-challenge");
34 expect(diagnostics).not.toContain("43123");
35 });
36});
37​
38describe("tryOpenWebLink", () => {
39 it("opens web, mail and phone links only", async () => {
40 openURL.mockResolvedValue(undefined);
41 for (const url of ["https://example.com/a", "HTTP://example.com", "mailto:a@example.com"]) {
42 await expect(tryOpenWebLink(url, "pull-request")).resolves.toBe(true);
43 }
44 for (const url of ["otherapp://open", "intent://x#Intent;end", "/owner/repo", ""]) {
45 await expect(tryOpenWebLink(url, "pull-request")).resolves.toBe(false);
46 }
47 expect(openURL).toHaveBeenCalledTimes(3);
48 });
49});