forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.test.tsx
More file actions
131 lines (108 loc) · 3.51 KB
/
Copy pathpage.test.tsx
File metadata and controls
131 lines (108 loc) · 3.51 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { render, screen, act } from "@testing-library/react";
import { ChakraProvider } from "@chakra-ui/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useStellarWallet } from "@/context/StellarWalletContext";
import { ErrorProvider } from "@/context/ErrorContext";
import HistoryPage from "./page";
vi.mock("@/context/StellarWalletContext", () => ({
useStellarWallet: vi.fn(),
}));
vi.mock("@/config", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/config")>();
return { ...actual, poolContractId: "CPOOLCONTRACTID" };
});
vi.mock("@/lib/soroban", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/soroban")>();
return { ...actual, getUserTransactionHistory: vi.fn() };
});
const { getUserTransactionHistory } = await import("@/lib/soroban");
const getUserTransactionHistoryMock = vi.mocked(getUserTransactionHistory);
const useStellarWalletMock = vi.mocked(useStellarWallet);
const connectedWallet = {
publicKey: "GA3CD2PYXOQCXW7ZVQW3MOA3JFZCE4F4IG2FD66I55TQASPCNKYYEFRN",
walletApi: null,
networkName: "TESTNET",
isNetworkMismatch: false,
isConnected: true,
connect: vi.fn(),
disconnect: vi.fn(),
};
function historyEntry(i: number) {
return {
date: new Date(2024, 0, i + 1).toISOString(),
action: (i % 2 === 0 ? "lock" : "unlock") as "lock" | "unlock",
amount: "10000000",
symbol: "XLM",
poolId: "pool-1",
creditsEarned: "5",
txHash: `hash-${i}`,
};
}
function renderPage() {
return render(
<ChakraProvider>
<ErrorProvider>
<HistoryPage />
</ErrorProvider>
</ChakraProvider>,
);
}
function liveRegionText() {
return screen.getByRole("status").textContent;
}
beforeEach(() => {
getUserTransactionHistoryMock.mockReset();
useStellarWalletMock.mockReturnValue(connectedWallet);
});
afterEach(() => {
vi.useRealTimers();
});
describe("HistoryPage accessible live-refresh announcements (#86)", () => {
it("does not announce anything while disconnected", async () => {
useStellarWalletMock.mockReturnValue({
...connectedWallet,
publicKey: null,
isConnected: false,
});
await act(async () => {
renderPage();
});
expect(liveRegionText()).toBe("");
});
it("announces the loaded range once history resolves", async () => {
getUserTransactionHistoryMock.mockResolvedValue(
Array.from({ length: 5 }, (_, i) => historyEntry(i)),
);
await act(async () => {
renderPage();
});
expect(liveRegionText()).toBe(
"History updated, showing 1-5 of 5 transactions.",
);
});
it("announces 'No farming history found' rather than staying silent on zero entries", async () => {
getUserTransactionHistoryMock.mockResolvedValue([]);
await act(async () => {
renderPage();
});
expect(liveRegionText()).toBe("No farming history found.");
});
it("computes the correct upper bound on a partial final page", async () => {
// PAGE_SIZE is 20; 45 entries means the last page holds 5, not 20.
getUserTransactionHistoryMock.mockResolvedValue(
Array.from({ length: 45 }, (_, i) => historyEntry(i)),
);
await act(async () => {
renderPage();
});
expect(liveRegionText()).toBe(
"History updated, showing 1-20 of 45 transactions.",
);
await act(async () => {
screen.getByRole("button", { name: "3" }).click();
});
expect(liveRegionText()).toBe(
"History updated, showing 41-45 of 45 transactions.",
);
});
});