forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectWalletButton.test.tsx
More file actions
81 lines (68 loc) · 2.31 KB
/
Copy pathConnectWalletButton.test.tsx
File metadata and controls
81 lines (68 loc) · 2.31 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
import { ChakraProvider } from "@chakra-ui/react";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useErrorHandler } from "@/context/ErrorContext";
import {
FREIGHTER_CONNECT_TIMEOUT_MS,
StellarWalletProvider,
} from "@/context/StellarWalletContext";
import { FreighterError } from "@/lib/error-handler";
import ConnectWalletButton from "./ConnectWalletButton";
const freighterMock = vi.hoisted(() => ({
getAddress: vi.fn(),
getNetworkDetails: vi.fn(),
isAllowed: vi.fn(),
isConnected: vi.fn(),
requestAccess: vi.fn(),
signTransaction: vi.fn(),
}));
const errorHandlerMock = vi.hoisted(() => ({
error: vi.fn(),
handleError: vi.fn(),
info: vi.fn(),
success: vi.fn(),
warning: vi.fn(),
withErrorHandling: vi.fn(),
}));
vi.mock("@stellar/freighter-api", () => freighterMock);
vi.mock("@/context/ErrorContext", () => ({
useErrorHandler: vi.fn(() => errorHandlerMock),
}));
function renderConnectButton() {
return render(
<ChakraProvider>
<StellarWalletProvider>
<ConnectWalletButton />
</StellarWalletProvider>
</ChakraProvider>,
);
}
describe("ConnectWalletButton", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.clearAllMocks();
vi.mocked(useErrorHandler).mockReturnValue(errorHandlerMock);
});
afterEach(() => {
vi.useRealTimers();
});
it("clears loading and reports a typed timeout when Freighter never responds", async () => {
freighterMock.isConnected.mockReturnValue(new Promise(() => {}));
renderConnectButton();
const button = screen.getByRole("button", { name: "Connect Freighter" });
fireEvent.click(button);
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
});
await act(async () => {
await vi.advanceTimersByTimeAsync(FREIGHTER_CONNECT_TIMEOUT_MS);
});
expect(errorHandlerMock.handleError).toHaveBeenCalledTimes(1);
const [error, context] = errorHandlerMock.handleError.mock.calls[0];
expect(error).toBeInstanceOf(FreighterError);
expect((error as FreighterError).code).toBe("FREIGHTER_TIMEOUT");
expect(context).toBe("Wallet Connection");
expect(button.hasAttribute("disabled")).toBe(false);
expect(button.textContent).toContain("Connect Freighter");
});
});