Skip to content

Commit

Permalink
test(react): useBalance (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Feb 17, 2025
1 parent 5c85606 commit 293d300
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/core/src/maths/spendable-balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ test.each([
{ free: 100n, frozen: 0n, reserved: 0n, spendable: 99n },
{ free: 100n, frozen: 80n, reserved: 0n, spendable: 20n },
{ free: 80n, frozen: 80n, reserved: 20n, spendable: 20n },
{
free: 80n,
frozen: 80n,
reserved: 20n,
spendable: 20n,
includesExistentialDeposit: true,
},
])(
"$spendable = $free - max($frozen - $reserved, existentialDeposit)",
({ free, frozen, reserved, spendable }) => {
"$spendable = $free - max($frozen - $reserved, existentialDeposit ($includesExistentialDeposit))",
({ free, frozen, reserved, spendable, includesExistentialDeposit }) => {
const existentialDeposit = 1n;

expect(
Expand All @@ -17,7 +24,7 @@ test.each([
frozen,
reserved,
existentialDeposit,
includesExistentialDeposit: false,
includesExistentialDeposit,
}),
).toBe(spendable);
},
Expand Down
101 changes: 101 additions & 0 deletions packages/react/src/hooks/use-balance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { DenominatedNumber } from "../../../utils/build/denominated-number";
import { useSpendableBalance } from "./use-balance";
import { useNativeTokenAmountFromPlanck } from "./use-native-token-amount";
import { useLazyLoadQuery } from "./use-query";
import { idle, Query } from "@reactive-dot/core";
import { renderHook } from "@testing-library/react";
import { expect, it, vi } from "vitest";

vi.mock("./use-query");
vi.mock("./use-native-token-amount");

const free = 1000n;

vi.mocked(useLazyLoadQuery).mockImplementation((builder) => {
if (!builder) {
return idle;
}

const query = builder(new Query([]));

if (!query) {
return idle;
}

return query.instructions.map((instruction) => {
if (
instruction.instruction === "get-constant" &&
instruction.pallet === "Balances" &&
instruction.constant === "ExistentialDeposit"
) {
return 100n;
} else if (
instruction.instruction === "read-storage" &&
instruction.pallet === "System" &&
instruction.storage === "Account" &&
"multi" in instruction &&
instruction.multi
) {
return Array.from({ length: instruction.args.length }).fill({
nonce: 0,
consumers: 0,
providers: 0,
sufficients: 0,
data: {
free,
reserved: 1000n,
frozen: 50n,
flags: 0n,
},
});
} else {
throw new Error("Invalid instruction");
}
});
});

vi.mocked(useNativeTokenAmountFromPlanck).mockReturnValue(
(planck) => new DenominatedNumber(planck, 0),
);

it("should return spendable balance for single address", () => {
const { result } = renderHook(() =>
useSpendableBalance("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"),
);

expect(result.current).toBeInstanceOf(DenominatedNumber);
});

it("should return spendable balances array for multiple addresses", () => {
const { result } = renderHook(() =>
useSpendableBalance([
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
]),
);

expect(result.current).toEqual(
expect.arrayContaining([
expect.any(DenominatedNumber),
expect.any(DenominatedNumber),
]),
);
});

it("should handle includesExistentialDeposit option", () => {
const { result } = renderHook(() =>
useSpendableBalance("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", {
includesExistentialDeposit: false,
}),
);

expect(result.current.planck).toBeLessThan(free);

const { result: result2 } = renderHook(() =>
useSpendableBalance("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", {
includesExistentialDeposit: true,
}),
);

expect(result2.current.planck).toBeGreaterThan(result.current.planck);
});

0 comments on commit 293d300

Please sign in to comment.