Skip to content

Commit

Permalink
test: account balance
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Nov 6, 2023
1 parent ba1d71f commit ceea076
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
74 changes: 74 additions & 0 deletions packages/ledger-icp/src/index.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { ActorSubclass } from "@dfinity/agent";
import { mock } from "jest-mock-extended";
import { _SERVICE as IndexService } from "../candid/index";
import { IndexCanister } from "./index.canister";
import { mockAccountIdentifier } from "./mocks/ledger.mock";

describe("IndexCanister", () => {
describe("accountBalance", () => {
const balanceMock = 30_000_000n;

it("returns account balance with query call", async () => {
const service = mock<ActorSubclass<IndexService>>();
service.get_account_identifier_balance.mockResolvedValue(balanceMock);
const index = IndexCanister.create({
serviceOverride: service,
});

const balance = await index.accountBalance({
accountIdentifier: mockAccountIdentifier,
certified: false,
});
expect(balance).toEqual(balanceMock);
expect(service.get_account_identifier_balance).toBeCalled();
});

it("returns account balance with update call", async () => {
const service = mock<ActorSubclass<IndexService>>();
service.get_account_identifier_balance.mockResolvedValue(balanceMock);
const index = IndexCanister.create({
certifiedServiceOverride: service,
});

const balance = await index.accountBalance({
accountIdentifier: mockAccountIdentifier,
certified: true,
});
expect(balance).toEqual(balanceMock);
expect(service.get_account_identifier_balance).toBeCalled();
});

it("returns account balance with account identifier as hex", async () => {
const service = mock<ActorSubclass<IndexService>>();
service.get_account_identifier_balance.mockResolvedValue(balanceMock);
const index = IndexCanister.create({
serviceOverride: service,
});

const balance = await index.accountBalance({
accountIdentifier: mockAccountIdentifier.toHex(),
certified: false,
});
expect(balance).toEqual(balanceMock);
expect(service.get_account_identifier_balance).toBeCalled();
});

it("should bubble errors", () => {
const service = mock<ActorSubclass<IndexService>>();
service.get_account_identifier_balance.mockImplementation(() => {
throw new Error();
});

const index = IndexCanister.create({
serviceOverride: service,
});

expect(() =>
index.accountBalance({
accountIdentifier: mockAccountIdentifier.toHex(),
certified: false,
}),
).toThrowError();
});
});
});
31 changes: 22 additions & 9 deletions packages/ledger-icp/src/ledger.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ import {
TxTooOldError,
} from "./errors/ledger.errors";
import { LedgerCanister } from "./ledger.canister";
import { mockAccountIdentifier } from "./mocks/ledger.mock";
import { E8s } from "./types/common";

describe("LedgerCanister", () => {
const accountIdentifier = AccountIdentifier.fromHex(
"3e8bbceef8b9338e56a1b561a127326e6614894ab9b0739df4cc3664d40a5958",
);
describe("accountBalance", () => {
describe("no hardware wallet", () => {
const tokens = {
Expand All @@ -35,7 +33,7 @@ describe("LedgerCanister", () => {
});

const balance = await ledger.accountBalance({
accountIdentifier,
accountIdentifier: mockAccountIdentifier,
certified: false,
});
expect(balance).toEqual(tokens.e8s);
Expand All @@ -50,12 +48,27 @@ describe("LedgerCanister", () => {
});

const balance = await ledger.accountBalance({
accountIdentifier,
accountIdentifier: mockAccountIdentifier,
certified: true,
});
expect(balance).toEqual(tokens.e8s);
expect(service.account_balance).toBeCalled();
});

it("returns account balance with account identifier as hex", async () => {
const service = mock<ActorSubclass<LedgerService>>();
service.account_balance.mockResolvedValue(tokens);
const ledger = LedgerCanister.create({
serviceOverride: service,
});

const balance = await ledger.accountBalance({
accountIdentifier: mockAccountIdentifier.toHex(),
certified: false,
});
expect(balance).toEqual(tokens.e8s);
expect(service.account_balance).toBeCalled();
});
});

describe("transactionFee", () => {
Expand Down Expand Up @@ -87,7 +100,7 @@ describe("LedgerCanister", () => {
hardwareWallet: true,
});
const balance = await ledger.accountBalance({
accountIdentifier,
accountIdentifier: mockAccountIdentifier,
certified: false,
});
expect(typeof balance).toEqual("bigint");
Expand All @@ -103,7 +116,7 @@ describe("LedgerCanister", () => {
hardwareWallet: true,
});
const balance = await ledger.accountBalance({
accountIdentifier,
accountIdentifier: mockAccountIdentifier,
certified: true,
});
expect(typeof balance).toEqual("bigint");
Expand All @@ -114,7 +127,7 @@ describe("LedgerCanister", () => {

describe("transfer", () => {
describe("no hardware wallet", () => {
const to = accountIdentifier;
const to = mockAccountIdentifier;
const amount = BigInt(100000);

it("fetches transaction fee if not present", async () => {
Expand Down Expand Up @@ -386,7 +399,7 @@ describe("LedgerCanister", () => {
});

describe("for hardware wallet", () => {
const to = accountIdentifier;
const to = mockAccountIdentifier;
const amount = BigInt(100000);

it("handles invalid sender", async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/ledger-icp/src/mocks/ledger.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AccountIdentifier } from "../account_identifier";

export const mockAccountIdentifier = AccountIdentifier.fromHex(
"3e8bbceef8b9338e56a1b561a127326e6614894ab9b0739df4cc3664d40a5958",
);

0 comments on commit ceea076

Please sign in to comment.