Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(react): useBalance #499

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
Loading