From b0134ec008245226c9948fb0665d78d129767b4f Mon Sep 17 00:00:00 2001 From: Karelian Pie Date: Tue, 15 Feb 2022 19:04:07 +1100 Subject: [PATCH] test: Add spec for `assetsHistoricEarnings` --- src/interfaces/earnings.spec.ts | 69 +++++++++++++++++-- .../assetHistoricEarnings.factory.ts | 12 ++++ src/test-utils/factories/index.ts | 1 + 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 src/test-utils/factories/assetHistoricEarnings.factory.ts diff --git a/src/interfaces/earnings.spec.ts b/src/interfaces/earnings.spec.ts index ebd68a94..84e77670 100644 --- a/src/interfaces/earnings.spec.ts +++ b/src/interfaces/earnings.spec.ts @@ -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> = jest.fn(); +const lensAdaptersVaultsV2AssetsStaticMock: jest.Mock> = jest.fn(); +const getBlockNumberMock: jest.Mock> = jest.fn(); jest.mock("../yearn", () => ({ Yearn: jest.fn().mockImplementation(() => ({ @@ -16,7 +19,9 @@ jest.mock("../yearn", () => ({ fetchQuery: subgraphFetchQueryMock }, vision: {}, - lens: {}, + lens: { + adapters: { vaults: { v2: { assetsStatic: lensAdaptersVaultsV2AssetsStaticMock } } } + }, oracle: { getPriceUsdc: oracleGetPriceUsdcMock } @@ -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>; @@ -142,9 +157,55 @@ describe("EarningsInterface", () => { }); }); - describe("accountAssetsData", () => {}); + describe("accountAssetsData", () => { + it.todo("todo"); + }); + + describe("assetsHistoricEarnings", () => { + const assetHistoricEarnings = createMockAssetHistoricEarnings(); + let assetHistoricEarningsCacheFetchMock: jest.Mock> = 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> = 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", () => {}); diff --git a/src/test-utils/factories/assetHistoricEarnings.factory.ts b/src/test-utils/factories/assetHistoricEarnings.factory.ts new file mode 100644 index 00000000..9dd9ab0a --- /dev/null +++ b/src/test-utils/factories/assetHistoricEarnings.factory.ts @@ -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 = {}) => ({ + ...DEFAULT_ASSET_HISTORIC_EARNINGS, + ...overwrites +}); diff --git a/src/test-utils/factories/index.ts b/src/test-utils/factories/index.ts index 1173d66e..1dbbda4a 100644 --- a/src/test-utils/factories/index.ts +++ b/src/test-utils/factories/index.ts @@ -1,3 +1,4 @@ export * from "./asset.factory"; +export * from "./assetHistoricEarnings.factory"; export * from "./balance.factory"; export * from "./token.factory";