Skip to content

Commit

Permalink
test: Add spec for assetsHistoricEarnings
Browse files Browse the repository at this point in the history
  • Loading branch information
karelianpie committed Feb 15, 2022
1 parent 6b41954 commit b0134ec
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
69 changes: 65 additions & 4 deletions src/interfaces/earnings.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { getAddress } from "@ethersproject/address";
import BigNumber from "bignumber.js";

import { ChainId, EarningsInterface, SdkError, Usdc } from "..";
import { AssetHistoricEarnings, ChainId, EarningsInterface, SdkError, Usdc, VaultStatic } from "..";
import { Context } from "../context";
import { createMockAssetHistoricEarnings, createMockAssetStaticVaultV2 } from "../test-utils/factories";
import { Yearn } from "../yearn";

const getAddressMock = jest.fn();
const subgraphFetchQueryMock = jest.fn();
const oracleGetPriceUsdcMock: jest.Mock<Promise<Usdc>> = jest.fn();
const lensAdaptersVaultsV2AssetsStaticMock: jest.Mock<Promise<VaultStatic[]>> = jest.fn();
const getBlockNumberMock: jest.Mock<Promise<number>> = jest.fn();

jest.mock("../yearn", () => ({
Yearn: jest.fn().mockImplementation(() => ({
Expand All @@ -16,7 +19,9 @@ jest.mock("../yearn", () => ({
fetchQuery: subgraphFetchQueryMock
},
vision: {},
lens: {},
lens: {
adapters: { vaults: { v2: { assetsStatic: lensAdaptersVaultsV2AssetsStaticMock } } }
},
oracle: {
getPriceUsdc: oracleGetPriceUsdcMock
}
Expand All @@ -28,6 +33,16 @@ jest.mock("@ethersproject/address", () => ({
getAddress: jest.fn().mockImplementation(() => getAddressMock)
}));

jest.mock("../context", () => ({
Context: jest.fn().mockImplementation(() => ({
provider: {
read: {
getBlockNumber: getBlockNumberMock
}
}
}))
}));

describe("EarningsInterface", () => {
let earningsInterface: EarningsInterface<1>;

Expand Down Expand Up @@ -142,9 +157,55 @@ describe("EarningsInterface", () => {
});
});

describe("accountAssetsData", () => {});
describe("accountAssetsData", () => {
it.todo("todo");
});

describe("assetsHistoricEarnings", () => {
const assetHistoricEarnings = createMockAssetHistoricEarnings();
let assetHistoricEarningsCacheFetchMock: jest.Mock<Promise<AssetHistoricEarnings[] | undefined>> = jest.fn();

beforeEach(() => {
(earningsInterface as any).assetHistoricEarningsCache.fetch = assetHistoricEarningsCacheFetchMock;
});

describe("when there is cached data", () => {
beforeEach(() => {
assetHistoricEarningsCacheFetchMock.mockResolvedValue([assetHistoricEarnings]);
});

describe("assetsHistoricEarnings", () => {});
it("return the cached data", async () => {
const actualAssetsHistoricEarnings = await earningsInterface.assetsHistoricEarnings();

expect(actualAssetsHistoricEarnings).toEqual([assetHistoricEarnings]);
expect(assetHistoricEarningsCacheFetchMock).toHaveBeenCalledTimes(1);
});
});

describe("when there is no cached data", () => {
let assetHistoricEarningsMock: jest.Mock<Promise<AssetHistoricEarnings>> = jest.fn();
const assetsStatic = [createMockAssetStaticVaultV2()];
const assetHistoricEarnings = createMockAssetHistoricEarnings();

beforeEach(() => {
assetHistoricEarningsCacheFetchMock.mockResolvedValue(undefined);
lensAdaptersVaultsV2AssetsStaticMock.mockResolvedValue(assetsStatic);
getBlockNumberMock.mockResolvedValue(42000);
(earningsInterface as any).assetHistoricEarnings = assetHistoricEarningsMock;
assetHistoricEarningsMock.mockResolvedValue(assetHistoricEarnings);
});

it("should not call `assetHistoricEarningsCache.fetch`", async () => {
const actualAssetsHistoricEarnings = await earningsInterface.assetsHistoricEarnings();

expect(actualAssetsHistoricEarnings).toEqual([assetHistoricEarnings]);
expect(lensAdaptersVaultsV2AssetsStaticMock).toHaveBeenCalledTimes(1);
expect(getBlockNumberMock).toHaveBeenCalledTimes(1);
expect(assetHistoricEarningsMock).toHaveBeenCalledTimes(1);
expect(assetHistoricEarningsMock).toHaveBeenCalledWith("0x001", 30, 42000);
});
});
});

describe("assetHistoricEarnings", () => {});

Expand Down
12 changes: 12 additions & 0 deletions src/test-utils/factories/assetHistoricEarnings.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AssetHistoricEarnings } from "../..";

export const DEFAULT_ASSET_HISTORIC_EARNINGS: AssetHistoricEarnings = {
assetAddress: "0x001",
decimals: 18,
dayData: [{ earnings: { amount: "1", amountUsdc: "1" }, date: "15-02-2022" }]
};

export const createMockAssetHistoricEarnings = (overwrites: Partial<AssetHistoricEarnings> = {}) => ({
...DEFAULT_ASSET_HISTORIC_EARNINGS,
...overwrites
});
1 change: 1 addition & 0 deletions src/test-utils/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./asset.factory";
export * from "./assetHistoricEarnings.factory";
export * from "./balance.factory";
export * from "./token.factory";

0 comments on commit b0134ec

Please sign in to comment.