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

openExternalUrl.test.ts

36 lines1.2 KB

1import { Linking } from "react-native";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import { tryOpenExternalUrl } 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});