diff --git a/packages/ledger-icp/src/index.canister.spec.ts b/packages/ledger-icp/src/index.canister.spec.ts new file mode 100644 index 000000000..7db4b501c --- /dev/null +++ b/packages/ledger-icp/src/index.canister.spec.ts @@ -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>(); + 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>(); + 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>(); + 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>(); + service.get_account_identifier_balance.mockImplementation(() => { + throw new Error(); + }); + + const index = IndexCanister.create({ + serviceOverride: service, + }); + + expect(() => + index.accountBalance({ + accountIdentifier: mockAccountIdentifier.toHex(), + certified: false, + }), + ).toThrowError(); + }); + }); +}); diff --git a/packages/ledger-icp/src/ledger.canister.spec.ts b/packages/ledger-icp/src/ledger.canister.spec.ts index 79ad5e3ed..bbaf791e7 100644 --- a/packages/ledger-icp/src/ledger.canister.spec.ts +++ b/packages/ledger-icp/src/ledger.canister.spec.ts @@ -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 = { @@ -35,7 +33,7 @@ describe("LedgerCanister", () => { }); const balance = await ledger.accountBalance({ - accountIdentifier, + accountIdentifier: mockAccountIdentifier, certified: false, }); expect(balance).toEqual(tokens.e8s); @@ -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>(); + 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", () => { @@ -87,7 +100,7 @@ describe("LedgerCanister", () => { hardwareWallet: true, }); const balance = await ledger.accountBalance({ - accountIdentifier, + accountIdentifier: mockAccountIdentifier, certified: false, }); expect(typeof balance).toEqual("bigint"); @@ -103,7 +116,7 @@ describe("LedgerCanister", () => { hardwareWallet: true, }); const balance = await ledger.accountBalance({ - accountIdentifier, + accountIdentifier: mockAccountIdentifier, certified: true, }); expect(typeof balance).toEqual("bigint"); @@ -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 () => { @@ -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 () => { diff --git a/packages/ledger-icp/src/mocks/ledger.mock.ts b/packages/ledger-icp/src/mocks/ledger.mock.ts new file mode 100644 index 000000000..f35202044 --- /dev/null +++ b/packages/ledger-icp/src/mocks/ledger.mock.ts @@ -0,0 +1,5 @@ +import { AccountIdentifier } from "../account_identifier"; + +export const mockAccountIdentifier = AccountIdentifier.fromHex( + "3e8bbceef8b9338e56a1b561a127326e6614894ab9b0739df4cc3664d40a5958", +);