Skip to content

Commit

Permalink
Chore/regen pnpmlock + fix axios type in cosmos unit test (#7735)
Browse files Browse the repository at this point in the history
* regen pnpmlock

* fix mock type in cosmos unit test and regen pnpm lock
  • Loading branch information
Wozacosta committed Sep 3, 2024
1 parent a0bb74b commit c8ac662
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 112 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-penguins-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/coin-cosmos": patch
---

fix mock type in cosmos unit test and regen pnpm lock
122 changes: 64 additions & 58 deletions libs/coin-modules/coin-cosmos/src/api/Cosmos.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import network from "@ledgerhq/live-network/network";
import { Operation } from "@ledgerhq/types-live";
import { AxiosResponse } from "axios";
import BigNumber from "bignumber.js";
import cryptoFactory from "../chain/chain";
import { CosmosAPI } from "./Cosmos";

jest.mock("@ledgerhq/live-network/network");
const mockedNetwork = jest.mocked(network);
const mockAxiosResponse = (data: any) => {
return mockedNetwork.mockResolvedValue({
data,
status: 200,
headers: {} as any,
statusText: "",
config: {
headers: {} as any,
},
});
};

describe("CosmosApi", () => {
let cosmosApi: CosmosAPI;
Expand All @@ -21,20 +31,28 @@ describe("CosmosApi", () => {

describe("getAccount", () => {
it("should return base_account if available", async () => {
mockedNetwork.mockResolvedValue({
data: {
account: {
account_number: 1,
sequence: 0,
pub_key: { key: "k", "@type": "type" },
base_account: {
account_number: 2,
sequence: 42,
pub_key: { key: "k2", "@type": "type2" },
mockedNetwork.mockResolvedValue(
Promise.resolve({
data: {
account: {
account_number: 1,
sequence: 0,
pub_key: { key: "k", "@type": "type" },
base_account: {
account_number: 2,
sequence: 42,
pub_key: { key: "k2", "@type": "type2" },
},
},
},
},
} as AxiosResponse);
status: 200,
headers: {} as any,
statusText: "",
config: {
headers: {} as any,
},
}),
);

const account = await cosmosApi.getAccount("addr");
expect(account.accountNumber).toEqual(2);
Expand All @@ -44,16 +62,14 @@ describe("CosmosApi", () => {
});

it("should return the correct account based on type", async () => {
mockedNetwork.mockResolvedValue({
data: {
account: {
"@type": "/cosmos.auth.v1beta1.BaseAccount",
account_number: 1,
sequence: 0,
pub_key: { key: "k", "@type": "type" },
},
mockAxiosResponse({
account: {
"@type": "/cosmos.auth.v1beta1.BaseAccount",
account_number: 1,
sequence: 0,
pub_key: { key: "k", "@type": "type" },
},
} as AxiosResponse);
});

const account = await cosmosApi.getAccount("addr");
expect(account).toEqual({
Expand All @@ -63,24 +79,22 @@ describe("CosmosApi", () => {
pubKeyType: "type",
});

mockedNetwork.mockResolvedValue({
data: {
account: {
"@type": "/ethermint.types.v1.EthAccount",
account_number: 1,
sequence: 0,
pub_key: { key: "k", "@type": "type" },
mockAxiosResponse({
account: {
"@type": "/ethermint.types.v1.EthAccount",
account_number: 1,
sequence: 0,
pub_key: { key: "k", "@type": "type" },

base_account: {
address: "address",
pub_key: { key: "k2", "@type": "type2" },
account_number: 2,
sequence: 3,
},
code_hash: "codeHash",
base_account: {
address: "address",
pub_key: { key: "k2", "@type": "type2" },
account_number: 2,
sequence: 3,
},
code_hash: "codeHash",
},
} as AxiosResponse);
});

const ethermintAccount = await cosmosApi.getAccount("addr");
expect(ethermintAccount).toEqual({
Expand Down Expand Up @@ -144,9 +158,7 @@ describe("CosmosApi", () => {
total: "1",
},
};
mockedNetwork.mockResolvedValue({
data: getApiResponseSinceSdk041,
} as AxiosResponse);
mockAxiosResponse(getApiResponseSinceSdk041);
const cosmosRedelegations = await cosmosApi.getRedelegations("cosmosDelegatorAddress");
expect(cosmosRedelegations[0].amount).toEqual(new BigNumber(100));
expect(cosmosRedelegations[0].completionDate).toEqual(new Date("2023-06-04T15:12:58.567Z"));
Expand Down Expand Up @@ -180,9 +192,8 @@ describe("CosmosApi", () => {
],
pagination: { next_key: null, total: "1" },
};
mockedNetwork.mockResolvedValue({
data: getApiResponseSinceSdk046,
} as AxiosResponse);

mockAxiosResponse(getApiResponseSinceSdk046);

const cosmosRedelegations = await cosmosApi.getRedelegations("cosmosDelegatorAddress");
expect(cosmosRedelegations[0].amount).toEqual(new BigNumber(100));
Expand All @@ -194,9 +205,7 @@ describe("CosmosApi", () => {
});

it("should return no redelegations if there are none", async () => {
mockedNetwork.mockResolvedValue({
data: { redelegation_responses: [], pagination: { next_key: null, total: "0" } },
} as AxiosResponse);
mockAxiosResponse({ redelegation_responses: [], pagination: { next_key: null, total: "0" } });

const cosmosRedelegations = await cosmosApi.getRedelegations("cosmosDelegatorAddress");
expect(cosmosRedelegations.length).toEqual(0);
Expand All @@ -205,21 +214,20 @@ describe("CosmosApi", () => {

describe("simulate", () => {
it("should return gas used when the network call returns gas used", async () => {
mockedNetwork.mockResolvedValue({
data: {
gas_info: {
gas_used: 42000,
},
mockAxiosResponse({
gas_info: {
gas_used: 42000,
},
} as AxiosResponse);
});
const gas = await cosmosApi.simulate([]);
expect(gas).toEqual(new BigNumber(42000));
});

it("should throw an error when the network call does not return gas used", async () => {
mockedNetwork.mockResolvedValue({
data: { gas_info: {} },
} as AxiosResponse);
mockAxiosResponse({
gas_info: {},
});

await expect(cosmosApi.simulate([])).rejects.toThrowError();
});

Expand Down Expand Up @@ -590,9 +598,7 @@ describe("CosmosApi", () => {

describe("broadcastTransaction", () => {
it("should throw a SequenceNumberError exception in case of sequence number error", async () => {
mockedNetwork.mockResolvedValue({
data: { tx_response: { code: 32 } },
} as AxiosResponse);
mockAxiosResponse({ tx_response: { code: 32 } });
await expect(
cosmosApi.broadcast({
signedOperation: { operation: {} as Operation, signature: "signedOperation" },
Expand Down
Loading

0 comments on commit c8ac662

Please sign in to comment.