forked from Liquifact/Liquifact-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalletStatus.external.test.tsx
More file actions
92 lines (77 loc) · 2.45 KB
/
Copy pathWalletStatus.external.test.tsx
File metadata and controls
92 lines (77 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @jest-environment jsdom
*/
import "@testing-library/jest-dom";
import { act, render, screen, fireEvent } from "@testing-library/react";
import { ToastProvider } from "./ToastProvider";
import { WalletProvider, useWallet } from "./WalletProvider";
import WalletStatus from "./WalletStatus";
import { copy } from "../app/copy/en";
jest.mock("@stellar/freighter-api", () => ({
isConnected: jest.fn().mockResolvedValue(false),
requestAccess: jest.fn(),
getNetworkDetails: jest.fn(),
}));
function TestHarness() {
const { state } = useWallet();
return (
<div>
<div data-testid="wallet-state">{state}</div>
<WalletStatus />
</div>
);
}
function renderWithProviders() {
return render(
<ToastProvider>
<WalletProvider>
<TestHarness />
</WalletProvider>
</ToastProvider>
);
}
describe("WalletStatus external navigation", () => {
let openSpy: jest.SpyInstance;
let errorSpy: jest.SpyInstance;
let originalUrl: string;
beforeEach(() => {
jest.useFakeTimers();
openSpy = jest.spyOn(window, "open").mockImplementation();
errorSpy = jest.spyOn(console, "error").mockImplementation();
originalUrl = copy.wallet.installWalletUrl;
});
afterEach(() => {
copy.wallet.installWalletUrl = originalUrl;
jest.useRealTimers();
jest.restoreAllMocks();
});
async function connectToReachNoWalletState() {
renderWithProviders();
const button = screen.getByRole("button", { name: /connect wallet/i });
fireEvent.click(button);
// Allow the async connect flow to resolve
await act(async () => {
await Promise.resolve();
});
}
it("opens the trusted wallet URL with noopener and noreferrer", async () => {
await connectToReachNoWalletState();
const installButton = screen.getByRole("button", { name: /install/i });
fireEvent.click(installButton);
expect(openSpy).toHaveBeenCalledTimes(1);
expect(openSpy).toHaveBeenCalledWith(
copy.wallet.installWalletUrl,
"_blank",
"noopener,noreferrer"
);
expect(errorSpy).not.toHaveBeenCalled();
});
it("blocks an insecure (http) URL and logs an error", async () => {
copy.wallet.installWalletUrl = "http://insecure-wallet-site.com";
await connectToReachNoWalletState();
const installButton = screen.getByRole("button", { name: /install/i });
fireEvent.click(installButton);
expect(openSpy).not.toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
});
});