|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { ANVIL_CHAIN } from "~test/chains.js"; |
3 | | -import { render, screen, waitFor } from "~test/react-render.js"; |
| 2 | +import { VITALIK_WALLET } from "~test/addresses.js"; |
4 | 3 | import { TEST_CLIENT } from "~test/test-clients.js"; |
5 | | -import { TEST_ACCOUNT_A } from "~test/test-wallets.js"; |
6 | | -import { getWalletBalance } from "../../../../../wallets/utils/getWalletBalance.js"; |
7 | | -import { AccountBalance } from "./balance.js"; |
8 | | -import { AccountProvider } from "./provider.js"; |
| 4 | +import { ethereum } from "../../../../../chains/chain-definitions/ethereum.js"; |
| 5 | +import { NATIVE_TOKEN_ADDRESS } from "../../../../../constants/addresses.js"; |
| 6 | +import { |
| 7 | + formatAccountFiatBalance, |
| 8 | + formatAccountTokenBalance, |
| 9 | + loadAccountBalance, |
| 10 | +} from "./balance.js"; |
9 | 11 |
|
10 | 12 | describe.runIf(process.env.TW_SECRET_KEY)("AccountBalance component", () => { |
11 | | - it("format the balance properly", async () => { |
12 | | - const roundTo1Decimal = (num: number): number => Math.round(num * 10) / 10; |
13 | | - const balance = await getWalletBalance({ |
14 | | - chain: ANVIL_CHAIN, |
| 13 | + it("`loadAccountBalance` should fetch the native balance properly", async () => { |
| 14 | + const result = await loadAccountBalance({ |
15 | 15 | client: TEST_CLIENT, |
16 | | - address: TEST_ACCOUNT_A.address, |
| 16 | + chain: ethereum, |
| 17 | + address: VITALIK_WALLET, |
17 | 18 | }); |
18 | 19 |
|
19 | | - render( |
20 | | - <AccountProvider address={TEST_ACCOUNT_A.address} client={TEST_CLIENT}> |
21 | | - <AccountBalance chain={ANVIL_CHAIN} formatFn={roundTo1Decimal} /> |
22 | | - </AccountProvider>, |
23 | | - ); |
| 20 | + expect(Number.isNaN(result.balance)).toBe(false); |
| 21 | + expect(result.symbol).toBe("ETH"); |
| 22 | + }); |
24 | 23 |
|
25 | | - waitFor(() => |
26 | | - expect( |
27 | | - screen.getByText(roundTo1Decimal(Number(balance.displayValue)), { |
28 | | - exact: true, |
29 | | - selector: "span", |
30 | | - }), |
31 | | - ).toBeInTheDocument(), |
32 | | - ); |
| 24 | + it("`loadAccountBalance` should fetch the token balance properly", async () => { |
| 25 | + const result = await loadAccountBalance({ |
| 26 | + client: TEST_CLIENT, |
| 27 | + chain: ethereum, |
| 28 | + address: VITALIK_WALLET, |
| 29 | + // USDC |
| 30 | + tokenAddress: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", |
| 31 | + }); |
| 32 | + |
| 33 | + expect(Number.isNaN(result.balance)).toBe(false); |
| 34 | + expect(result.symbol).toBe("USDC"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("`loadAccountBalance` should fetch the fiat balance properly", async () => { |
| 38 | + const result = await loadAccountBalance({ |
| 39 | + client: TEST_CLIENT, |
| 40 | + chain: ethereum, |
| 41 | + address: VITALIK_WALLET, |
| 42 | + showInFiat: "USD", |
| 43 | + }); |
| 44 | + |
| 45 | + expect(Number.isNaN(result.balance)).toBe(false); |
| 46 | + expect(result.symbol).toBe("$"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("`loadAccountBalance` should throw if `chain` is not passed", async () => { |
| 50 | + await expect(() => |
| 51 | + loadAccountBalance({ client: TEST_CLIENT, address: VITALIK_WALLET }), |
| 52 | + ).rejects.toThrowError("chain is required"); |
33 | 53 | }); |
34 | 54 |
|
35 | | - it("should fallback properly if failed to load", () => { |
36 | | - render( |
37 | | - <AccountProvider address={TEST_ACCOUNT_A.address} client={TEST_CLIENT}> |
38 | | - <AccountBalance |
39 | | - chain={undefined} |
40 | | - fallbackComponent={<span>oops</span>} |
41 | | - /> |
42 | | - </AccountProvider>, |
| 55 | + it("`loadAccountBalance` should throw if `tokenAddress` is mistakenly passed as native token", async () => { |
| 56 | + await expect(() => |
| 57 | + loadAccountBalance({ |
| 58 | + client: TEST_CLIENT, |
| 59 | + address: VITALIK_WALLET, |
| 60 | + tokenAddress: NATIVE_TOKEN_ADDRESS, |
| 61 | + chain: ethereum, |
| 62 | + }), |
| 63 | + ).rejects.toThrowError( |
| 64 | + `Invalid tokenAddress - cannot be ${NATIVE_TOKEN_ADDRESS}`, |
43 | 65 | ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("`loadAccountBalance` should throw if `address` is not a valid evm address", async () => { |
| 69 | + await expect(() => |
| 70 | + loadAccountBalance({ |
| 71 | + client: TEST_CLIENT, |
| 72 | + address: "haha", |
| 73 | + chain: ethereum, |
| 74 | + }), |
| 75 | + ).rejects.toThrowError("Invalid wallet address. Expected an EVM address"); |
| 76 | + }); |
44 | 77 |
|
45 | | - waitFor(() => |
46 | | - expect( |
47 | | - screen.getByText("oops", { |
48 | | - exact: true, |
49 | | - selector: "span", |
50 | | - }), |
51 | | - ).toBeInTheDocument(), |
| 78 | + it("`loadAccountBalance` should throw if `tokenAddress` is passed but is not a valid evm address", async () => { |
| 79 | + await expect(() => |
| 80 | + loadAccountBalance({ |
| 81 | + client: TEST_CLIENT, |
| 82 | + address: VITALIK_WALLET, |
| 83 | + tokenAddress: "haha", |
| 84 | + chain: ethereum, |
| 85 | + }), |
| 86 | + ).rejects.toThrowError( |
| 87 | + "Invalid tokenAddress. Expected an EVM contract address", |
52 | 88 | ); |
53 | 89 | }); |
| 90 | + |
| 91 | + it("`formatAccountTokenBalance` should display a rounded-up value + symbol", () => { |
| 92 | + expect( |
| 93 | + formatAccountTokenBalance({ |
| 94 | + balance: 1.1999, |
| 95 | + symbol: "ETH", |
| 96 | + decimals: 1, |
| 97 | + }), |
| 98 | + ).toBe("1.2 ETH"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("`formatAccountFiatBalance` should display fiat symbol followed by a rounded-up fiat value", () => { |
| 102 | + expect( |
| 103 | + formatAccountFiatBalance({ balance: 55.001, symbol: "$", decimals: 0 }), |
| 104 | + ).toBe("$55"); |
| 105 | + }); |
54 | 106 | }); |
0 commit comments