Skip to content

Commit

Permalink
lint format
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-agarwal-coinbase committed Nov 25, 2024
1 parent 073b1ae commit e1c0acb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 57 deletions.
80 changes: 61 additions & 19 deletions src/tests/fund_operation_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { FundOperation as FundOperationModel, Asset as AssetModel, FundOperationList, FundOperationStatusEnum } from "../client/api";
import {
FundOperation as FundOperationModel,
Asset as AssetModel,
FundOperationList,
FundOperationStatusEnum,
} from "../client/api";
import { Coinbase } from "../coinbase/coinbase";
import {
VALID_ASSET_MODEL,
Expand All @@ -17,7 +22,7 @@ import { TimeoutError } from "../coinbase/errors";
import { FundOperationStatus } from "../coinbase/types";

describe("FundOperation", () => {
let assetModel: AssetModel;
let assetModel: AssetModel;
let asset: Asset;
let fundOperationModel: FundOperationModel;
let fundOperation: FundOperation;
Expand Down Expand Up @@ -48,22 +53,47 @@ describe("FundOperation", () => {

describe(".create", () => {
it("should create a new fund operation without quote", async () => {
const newFundOperation = await FundOperation.create(fundOperationModel.wallet_id, fundOperationModel.address_id, new Decimal(fundOperationModel.crypto_amount.amount), fundOperationModel.crypto_amount.asset.asset_id, fundOperationModel.network_id);
const newFundOperation = await FundOperation.create(
fundOperationModel.wallet_id,
fundOperationModel.address_id,
new Decimal(fundOperationModel.crypto_amount.amount),
fundOperationModel.crypto_amount.asset.asset_id,
fundOperationModel.network_id,
);
expect(newFundOperation).toBeInstanceOf(FundOperation);
expect(Coinbase.apiClients.fund!.createFundOperation).toHaveBeenCalledWith(fundOperationModel.wallet_id, fundOperationModel.address_id, {
fund_quote_id: undefined,
amount: new Decimal(fundOperationModel.crypto_amount.amount).mul(10 ** asset.decimals).toString(),
asset_id: fundOperationModel.crypto_amount.asset.asset_id,
});
expect(Coinbase.apiClients.fund!.createFundOperation).toHaveBeenCalledWith(
fundOperationModel.wallet_id,
fundOperationModel.address_id,
{
fund_quote_id: undefined,
amount: new Decimal(fundOperationModel.crypto_amount.amount)
.mul(10 ** asset.decimals)
.toString(),
asset_id: fundOperationModel.crypto_amount.asset.asset_id,
},
);
});
it("should create a new fund operation with quote", async () => {
const newFundOperation = await FundOperation.create(fundOperationModel.wallet_id, fundOperationModel.address_id, new Decimal(fundOperationModel.crypto_amount.amount), fundOperationModel.crypto_amount.asset.asset_id, fundOperationModel.network_id, FundQuote.fromModel(VALID_FUND_QUOTE_MODEL));
const newFundOperation = await FundOperation.create(
fundOperationModel.wallet_id,
fundOperationModel.address_id,
new Decimal(fundOperationModel.crypto_amount.amount),
fundOperationModel.crypto_amount.asset.asset_id,
fundOperationModel.network_id,
FundQuote.fromModel(VALID_FUND_QUOTE_MODEL),
);
expect(newFundOperation).toBeInstanceOf(FundOperation);
expect(Coinbase.apiClients.fund!.createFundOperation).toHaveBeenCalledWith(fundOperationModel.wallet_id, fundOperationModel.address_id, {
fund_quote_id: VALID_FUND_QUOTE_MODEL.fund_quote_id,
amount: new Decimal(fundOperationModel.crypto_amount.amount).mul(10 ** asset.decimals).toString(),
asset_id: fundOperationModel.crypto_amount.asset.asset_id,
});
expect(Coinbase.apiClients.fund!.createFundOperation).toHaveBeenCalledWith(
fundOperationModel.wallet_id,
fundOperationModel.address_id,
{
fund_quote_id: VALID_FUND_QUOTE_MODEL.fund_quote_id,
amount: new Decimal(fundOperationModel.crypto_amount.amount)
.mul(10 ** asset.decimals)
.toString(),
asset_id: fundOperationModel.crypto_amount.asset.asset_id,
},
);
});
});

Expand All @@ -76,7 +106,10 @@ describe("FundOperation", () => {
total_count: 0,
} as FundOperationList;
Coinbase.apiClients.fund!.listFundOperations = mockReturnValue(response);
const paginationResponse = await FundOperation.listFundOperations(fundOperationModel.wallet_id, fundOperationModel.address_id);
const paginationResponse = await FundOperation.listFundOperations(
fundOperationModel.wallet_id,
fundOperationModel.address_id,
);
const fundOperations = paginationResponse.data;
expect(fundOperations).toHaveLength(1);
expect(fundOperations[0]).toBeInstanceOf(FundOperation);
Expand All @@ -96,7 +129,10 @@ describe("FundOperation", () => {
total_count: 0,
} as FundOperationList;
Coinbase.apiClients.fund!.listFundOperations = mockReturnValue(response);
const paginationResponse = await FundOperation.listFundOperations(fundOperationModel.wallet_id, fundOperationModel.address_id);
const paginationResponse = await FundOperation.listFundOperations(
fundOperationModel.wallet_id,
fundOperationModel.address_id,
);
expect(paginationResponse.nextPage).toEqual("abc");
expect(paginationResponse.hasMore).toEqual(true);
const fundOperations = paginationResponse.data;
Expand Down Expand Up @@ -144,13 +180,17 @@ describe("FundOperation", () => {

describe("#getAmount", () => {
it("should return the amount", () => {
expect(fundOperation.getAmount()).toEqual(CryptoAmount.fromModel(fundOperationModel.crypto_amount));
expect(fundOperation.getAmount()).toEqual(
CryptoAmount.fromModel(fundOperationModel.crypto_amount),
);
});
});

describe("#getFiatAmount", () => {
it("should return the fiat amount", () => {
expect(fundOperation.getFiatAmount()).toEqual(new Decimal(fundOperationModel.fiat_amount.amount));
expect(fundOperation.getFiatAmount()).toEqual(
new Decimal(fundOperationModel.fiat_amount.amount),
);
});
});

Expand Down Expand Up @@ -224,7 +264,9 @@ describe("FundOperation", () => {
...VALID_FUND_OPERATION_MODEL,
status: FundOperationStatus.PENDING,
});
await expect(fundOperation.wait({ timeoutSeconds: 0.05, intervalSeconds: 0.05 })).rejects.toThrow(new TimeoutError("Fund operation timed out"));
await expect(
fundOperation.wait({ timeoutSeconds: 0.05, intervalSeconds: 0.05 }),
).rejects.toThrow(new TimeoutError("Fund operation timed out"));
});
});
});
36 changes: 18 additions & 18 deletions src/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,29 +439,29 @@ export const VALID_FUND_QUOTE_MODEL: FundQuoteModel = {
export const VALID_FUND_OPERATION_MODEL: FundOperationModel = {
fund_operation_id: "test-operation-id",
network_id: Coinbase.networks.BaseSepolia,
wallet_id: "test-wallet-id",
wallet_id: "test-wallet-id",
address_id: "test-address-id",
crypto_amount: VALID_ETH_CRYPTO_AMOUNT_MODEL,
fiat_amount: {
amount: "100",
currency: "USD"
amount: "100",
currency: "USD",
},
fees: {
buy_fee: {
amount: "1",
currency: "USD"
},
transfer_fee: {
amount: "10000000000000000", // 0.01 ETH in wei
asset: {
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networks.BaseSepolia,
decimals: 18,
contract_address: "0x",
}
}
},
status: "complete" as const
buy_fee: {
amount: "1",
currency: "USD",
},
transfer_fee: {
amount: "10000000000000000", // 0.01 ETH in wei
asset: {
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networks.BaseSepolia,
decimals: 18,
contract_address: "0x",
},
},
},
status: "complete" as const,
};

/**
Expand Down
16 changes: 8 additions & 8 deletions src/tests/wallet_address_fund_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe("WalletAddress Fund", () => {
beforeEach(() => {
walletAddress = new WalletAddress(newAddressModel(walletId, addressId));

jest.spyOn(FundOperation, 'create').mockResolvedValue({} as FundOperation);
jest.spyOn(FundQuote, 'create').mockResolvedValue({} as FundQuote);
jest.spyOn(FundOperation, "create").mockResolvedValue({} as FundOperation);
jest.spyOn(FundQuote, "create").mockResolvedValue({} as FundQuote);
});

afterEach(() => {
Expand All @@ -32,7 +32,7 @@ describe("WalletAddress Fund", () => {
addressId,
amount,
assetId,
walletAddress.getNetworkId()
walletAddress.getNetworkId(),
);
});
it("should call FundOperation.create with correct parameters when passing in number amount", async () => {
Expand All @@ -46,7 +46,7 @@ describe("WalletAddress Fund", () => {
addressId,
new Decimal(amount),
assetId,
walletAddress.getNetworkId()
walletAddress.getNetworkId(),
);
});
it("should call FundOperation.create with correct parameters when passing in bigint amount", async () => {
Expand All @@ -60,7 +60,7 @@ describe("WalletAddress Fund", () => {
addressId,
new Decimal(amount.toString()),
assetId,
walletAddress.getNetworkId()
walletAddress.getNetworkId(),
);
});
});
Expand All @@ -77,7 +77,7 @@ describe("WalletAddress Fund", () => {
addressId,
amount,
assetId,
walletAddress.getNetworkId()
walletAddress.getNetworkId(),
);
});
it("should call FundQuote.create with correct parameters when passing in number amount", async () => {
Expand All @@ -91,7 +91,7 @@ describe("WalletAddress Fund", () => {
addressId,
new Decimal(amount),
assetId,
walletAddress.getNetworkId()
walletAddress.getNetworkId(),
);
});
it("should call FundQuote.create with correct parameters when passing in bigint amount", async () => {
Expand All @@ -105,7 +105,7 @@ describe("WalletAddress Fund", () => {
addressId,
new Decimal(amount.toString()),
assetId,
walletAddress.getNetworkId()
walletAddress.getNetworkId(),
);
});
});
Expand Down
30 changes: 18 additions & 12 deletions src/tests/wallet_fund_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ describe("Wallet Fund", () => {
defaultAddress = new WalletAddress(addressModel);

walletModel = {
id: walletId,
network_id: Coinbase.networks.BaseSepolia,
default_address: addressModel,
feature_set: {} as FeatureSet,
};
id: walletId,
network_id: Coinbase.networks.BaseSepolia,
default_address: addressModel,
feature_set: {} as FeatureSet,
};

wallet = Wallet.init(walletModel, "");

// Mock getDefaultAddress to return our test address
jest.spyOn(wallet, "getDefaultAddress").mockResolvedValue(defaultAddress);

// Mock the fund and quoteFund methods on the default address
jest.spyOn(defaultAddress, "fund").mockResolvedValue({} as FundOperation);
jest.spyOn(defaultAddress, "quoteFund").mockResolvedValue({} as FundQuote);
Expand Down Expand Up @@ -68,8 +68,10 @@ describe("Wallet Fund", () => {
});

it("should throw error if default address does not exist", async () => {
jest.spyOn(wallet, "getDefaultAddress").mockRejectedValue(new Error("Default address does not exist"));

jest
.spyOn(wallet, "getDefaultAddress")
.mockRejectedValue(new Error("Default address does not exist"));

const amount = new Decimal("1.0");
const assetId = "eth";

Expand Down Expand Up @@ -106,12 +108,16 @@ describe("Wallet Fund", () => {
});

it("should throw error if default address does not exist", async () => {
jest.spyOn(wallet, "getDefaultAddress").mockRejectedValue(new Error("Default address does not exist"));

jest
.spyOn(wallet, "getDefaultAddress")
.mockRejectedValue(new Error("Default address does not exist"));

const amount = new Decimal("1.0");
const assetId = "eth";

await expect(wallet.quoteFund(amount, assetId)).rejects.toThrow("Default address does not exist");
await expect(wallet.quoteFund(amount, assetId)).rejects.toThrow(
"Default address does not exist",
);
});
});
});

0 comments on commit e1c0acb

Please sign in to comment.