Skip to content

Commit

Permalink
Updating Address tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erdimaden committed May 20, 2024
1 parent 80cce7c commit 65880bb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
66 changes: 46 additions & 20 deletions src/coinbase/tests/address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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", () => {
Expand All @@ -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", () => {
Expand Down
10 changes: 10 additions & 0 deletions src/coinbase/tests/coinbase_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 65880bb

Please sign in to comment.