From 65880bbbb7a4ece6958791cdc0f2a47f1519da21 Mon Sep 17 00:00:00 2001 From: Erdi Maden Date: Mon, 20 May 2024 13:47:55 -0500 Subject: [PATCH] Updating Address tests --- src/coinbase/tests/address_test.ts | 66 ++++++++++++++++++++--------- src/coinbase/tests/coinbase_test.ts | 10 +++++ 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/coinbase/tests/address_test.ts b/src/coinbase/tests/address_test.ts index 600957e0..6e0e493d 100644 --- a/src/coinbase/tests/address_test.ts +++ b/src/coinbase/tests/address_test.ts @@ -21,10 +21,17 @@ const VALID_ADDRESS_MODEL: AddressModel = { // Test suite for Address class describe("Address", () => { + const transactionHash = "0xdeadbeef"; let address: Address; + const getAddressBalance = jest.fn().mockResolvedValue({ data: VALID_BALANCE_MODEL }); + const listAddressBalances = jest.fn().mockResolvedValue({ data: VALID_ADDRESS_BALANCE_LIST }); + const requestFaucetFunds = jest + .fn() + .mockResolvedValue({ data: { transaction_hash: transactionHash } }); beforeEach(() => { address = new Address(VALID_ADDRESS_MODEL); + jest.clearAllMocks(); }); it("should initialize a new Address", () => { @@ -42,55 +49,68 @@ describe("Address", () => { it("should return the correct list of balances", async () => { Coinbase.apiClients.address = { ...addressesApiMock, - listAddressBalances: jest.fn().mockResolvedValue({ data: VALID_ADDRESS_BALANCE_LIST }), + listAddressBalances, }; const balances = await address.listBalances(); expect(balances.get(Coinbase.assetList.Eth)).toEqual(new Decimal(1)); expect(balances.get("usdc")).toEqual(new Decimal(5000)); expect(balances.get("weth")).toEqual(new Decimal(3)); + expect(listAddressBalances).toHaveBeenCalledWith(address.getWalletId(), address.getId()); + expect(listAddressBalances).toHaveBeenCalledTimes(1); }); it("should return the correct ETH balance", async () => { Coinbase.apiClients.address = { ...addressesApiMock, - getAddressBalance: jest.fn().mockResolvedValue({ data: VALID_BALANCE_MODEL }), + getAddressBalance, }; const ethBalance = await address.getBalance(Coinbase.assetList.Eth); expect(ethBalance).toBeInstanceOf(Decimal); expect(ethBalance).toEqual(new Decimal(1)); + expect(getAddressBalance).toHaveBeenCalledWith( + address.getWalletId(), + address.getId(), + Coinbase.assetList.Eth, + ); + expect(getAddressBalance).toHaveBeenCalledTimes(1); }); it("should return the correct Gwei balance", async () => { + const assetId = "gwei"; Coinbase.apiClients.address = { ...addressesApiMock, - getAddressBalance: jest.fn().mockResolvedValue({ data: VALID_BALANCE_MODEL }), + getAddressBalance, }; - const ethBalance = await address.getBalance("gwei"); + const ethBalance = await address.getBalance(assetId); expect(ethBalance).toBeInstanceOf(Decimal); expect(ethBalance).toEqual(new Decimal(1000000000)); + expect(getAddressBalance).toHaveBeenCalledWith(address.getWalletId(), address.getId(), assetId); + expect(getAddressBalance).toHaveBeenCalledTimes(1); }); it("should return the correct Wei balance", async () => { + const assetId = "wei"; Coinbase.apiClients.address = { ...addressesApiMock, - getAddressBalance: jest.fn().mockResolvedValue({ data: VALID_BALANCE_MODEL }), + getAddressBalance, }; - const ethBalance = await address.getBalance("wei"); + const ethBalance = await address.getBalance(assetId); expect(ethBalance).toBeInstanceOf(Decimal); expect(ethBalance).toEqual(new Decimal(1000000000000000000)); + expect(getAddressBalance).toHaveBeenCalledWith(address.getWalletId(), address.getId(), assetId); + expect(getAddressBalance).toHaveBeenCalledTimes(1); }); it("should return an error for an unsupported asset", async () => { + const getAddressBalance = jest.fn().mockRejectedValue(new APIError("")); + const assetId = "unsupported-asset"; Coinbase.apiClients.address = { ...addressesApiMock, - getAddressBalance: jest.fn().mockRejectedValue(new APIError("")), + getAddressBalance, }; - try { - await address.getBalance("unsupported-asset"); - fail("Expect 404 to be thrown"); - } catch (error) { - expect(error).toBeInstanceOf(APIError); - } + await expect(address.getBalance(assetId)).rejects.toThrow(APIError); + expect(getAddressBalance).toHaveBeenCalledWith(address.getWalletId(), address.getId(), assetId); + expect(getAddressBalance).toHaveBeenCalledTimes(1); }); it("should return the wallet ID", () => { @@ -102,40 +122,46 @@ describe("Address", () => { }); it("should request funds from the faucet and returns the faucet transaction", async () => { - const transactionHash = "0xdeadbeef"; Coinbase.apiClients.address = { ...addressesApiMock, - requestFaucetFunds: jest - .fn() - .mockResolvedValue({ data: { transaction_hash: transactionHash } }), + requestFaucetFunds, }; const faucetTransaction = await address.faucet(); expect(faucetTransaction).toBeInstanceOf(FaucetTransaction); expect(faucetTransaction.getTransactionHash()).toBe(transactionHash); + expect(requestFaucetFunds).toHaveBeenCalledWith(address.getWalletId(), address.getId()); + expect(requestFaucetFunds).toHaveBeenCalledTimes(1); }); it("should throw an APIError when the request is unsuccesful", async () => { + const requestFaucetFunds = jest.fn().mockRejectedValue(new APIError("")); Coinbase.apiClients.address = { ...addressesApiMock, - requestFaucetFunds: jest.fn().mockRejectedValue(new APIError("")), + requestFaucetFunds, }; await expect(address.faucet()).rejects.toThrow(APIError); + expect(requestFaucetFunds).toHaveBeenCalledWith(address.getWalletId(), address.getId()); + expect(requestFaucetFunds).toHaveBeenCalledTimes(1); }); it("should throw a FaucetLimitReachedError when the faucet limit is reached", async () => { + const requestFaucetFunds = jest.fn().mockRejectedValue(new FaucetLimitReachedError("")); Coinbase.apiClients.address = { ...addressesApiMock, - requestFaucetFunds: jest.fn().mockRejectedValue(new FaucetLimitReachedError("")), + requestFaucetFunds, }; await expect(address.faucet()).rejects.toThrow(FaucetLimitReachedError); + expect(requestFaucetFunds).toHaveBeenCalledTimes(1); }); it("should throw an InternalError when the request fails unexpectedly", async () => { + const requestFaucetFunds = jest.fn().mockRejectedValue(new InternalError("")); Coinbase.apiClients.address = { ...addressesApiMock, - requestFaucetFunds: jest.fn().mockRejectedValue(new InternalError("")), + requestFaucetFunds, }; await expect(address.faucet()).rejects.toThrow(InternalError); + expect(requestFaucetFunds).toHaveBeenCalledTimes(1); }); it("should return the correct string representation", () => { diff --git a/src/coinbase/tests/coinbase_test.ts b/src/coinbase/tests/coinbase_test.ts index a9e66298..9be5428e 100644 --- a/src/coinbase/tests/coinbase_test.ts +++ b/src/coinbase/tests/coinbase_test.ts @@ -50,17 +50,27 @@ describe("Coinbase tests", () => { expect(user.getId()).toBe(123); expect(user.toString()).toBe("User{ userId: 123 }"); + expect(usersApiMock.getCurrentUser).toHaveBeenCalledWith(); + expect(usersApiMock.getCurrentUser).toHaveBeenCalledTimes(1); }); it("should be able to get faucet funds", async () => { const wallet = await user.createWallet(); expect(wallet.getId()).toBe(VALID_WALLET_MODEL.id); + const payload = { wallet: { network_id: Coinbase.networkList.BaseSepolia } }; + expect(walletsApiMock.createWallet).toHaveBeenCalledWith(payload); + expect(walletsApiMock.createWallet).toHaveBeenCalledTimes(1); const defaultAddress = wallet.defaultAddress(); expect(defaultAddress?.getId()).toBe(VALID_WALLET_MODEL.default_address?.address_id); const faucetTransaction = await wallet?.faucet(); expect(faucetTransaction.getTransactionHash()).toBe("0xdeadbeef"); + expect(addressesApiMock.requestFaucetFunds).toHaveBeenCalledWith( + wallet.getId(), + wallet.defaultAddress()?.getId(), + ); + expect(addressesApiMock.requestFaucetFunds).toHaveBeenCalledTimes(1); }); });