Skip to content

Commit

Permalink
test: refactor "MockStrategy" mock class
Browse files Browse the repository at this point in the history
test: remove superfluous "await" from synchronous calls
  • Loading branch information
scorpion9979 committed Feb 8, 2024
1 parent f6c4a77 commit bda63bf
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 23 deletions.
4 changes: 2 additions & 2 deletions test/shared/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StrategyArgs } from "../../src/types";
import { StrategyArgs, StrategyName } from "../../src/types";
import { BaseStrategy } from "../../src//strategies/base";

import type { BigNumber, ContractReceipt, Signer } from "ethers";
Expand All @@ -11,7 +11,7 @@ const { deployMockContract } = hre.waffle;

export class MockStrategy extends BaseStrategy {
constructor(args: StrategyArgs) {
super({ ...args, strategyName: "uniswap-v3" });
super({ ...args, strategyName: null as unknown as StrategyName });
}
protected async liquidate(
_account: string,
Expand Down
8 changes: 0 additions & 8 deletions test/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ export class StrategyTester<S extends BaseStrategy> {
// @ts-ignore
return this.strategy.isUnderwater(account);
}

public htokens(): Htokens {
// @ts-ignore
return this.strategy.htokens();
}

public vaults(): Vaults {
// @ts-ignore
return this.strategy.vaults();
Expand All @@ -31,12 +29,10 @@ export class StrategyTester<S extends BaseStrategy> {
// @ts-ignore
return this.strategy.cacheHtoken(htoken);
}

public async clearHtoken(htoken: string): Promise<void> {
// @ts-ignore
return this.strategy.clearHtoken(htoken);
}

public liquidate(
_account: string,
_bond: string,
Expand All @@ -47,22 +43,18 @@ export class StrategyTester<S extends BaseStrategy> {
// @ts-ignore
return this.strategy.liquidate(_account, _bond, _collateral, _underlyingAmount, _underlying);
}

public async liquidateAllMature(_latestBlock: number): Promise<void> {
// @ts-ignore
return this.strategy.liquidateAllMature(_latestBlock);
}

public async liquidateAllUnderwater(): Promise<void> {
// @ts-ignore
return this.strategy.liquidateAllUnderwater();
}

public async syncAll(_latestBlock?: number): Promise<void> {
// @ts-ignore
return this.strategy.syncAll(_latestBlock);
}

public async updateVaults(account: string, type: "pop" | "push", fragment: { [key: string]: string }): Promise<void> {
// @ts-ignore
return this.strategy.updateVaults(account, type, fragment);
Expand Down
7 changes: 2 additions & 5 deletions test/unit/base/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provider } from "../../../src/types";
import { NetworkConfig, Provider } from "../../../src/types";
import { unitFixtureBase } from "../../shared/fixtures";
import { MockStrategy } from "../../shared/mocks";
import { StrategyTester } from "../../shared/utils";
Expand All @@ -13,13 +13,10 @@ export function unitTestBase(): void {
this.mocks.usdc = usdc;
this.mocks.weth = weth;
this.liquidator = new StrategyTester(MockStrategy, {
networkConfig: {
networkConfig: <NetworkConfig>{
contracts: {
balanceSheet: balanceSheet.address,
strategies: {},
},
flashbotsEnabled: false,
startBlock: 0,
},
persistenceEnabled: false,
provider: this.signers.runner.provider as Provider,
Expand Down
8 changes: 4 additions & 4 deletions test/unit/base/write/cacheHtoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ export function shouldBehaveLikeCacheHtoken(): void {
});

it("does nothing", async function () {
const htokensBefore = await this.liquidator.htokens();
const htokensBefore = this.liquidator.htokens();
expect(htokensBefore).to.not.be.empty;
const htoken = htokensBefore[this.mocks.bond.address];
expect(htoken).to.have.keys("maturity", "underlying", "underlyingPrecisionScalar");
expect(htoken.maturity).to.be.eq(await this.mocks.bond.maturity());
expect(htoken.underlying).to.be.eq(await this.mocks.bond.underlying());
expect(htoken.underlyingPrecisionScalar).to.be.eq(await this.mocks.bond.underlyingPrecisionScalar());
await this.liquidator.cacheHtoken(this.mocks.bond.address);
const htokensAfter = await this.liquidator.htokens();
const htokensAfter = this.liquidator.htokens();
expect(htokensBefore).to.deep.equal(htokensAfter);
});
});

context("when the hToken is not cached", function () {
it("caches the hToken", async function () {
const htokensBefore = await this.liquidator.htokens();
const htokensBefore = this.liquidator.htokens();
expect(htokensBefore).to.be.empty;
await this.liquidator.cacheHtoken(this.mocks.bond.address);
const htokensAfter = await this.liquidator.htokens();
const htokensAfter = this.liquidator.htokens();
expect(htokensAfter).to.not.be.empty;
expect(htokensAfter).to.have.keys(this.mocks.bond.address);
const htoken = htokensAfter[this.mocks.bond.address];
Expand Down
8 changes: 4 additions & 4 deletions test/unit/base/write/clearHtoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { expect } from "chai";
export function shouldBehaveLikeClearHtoken(): void {
context("when the hToken is not cached", function () {
it("does nothing", async function () {
const htokensBefore = await this.liquidator.htokens();
const htokensBefore = this.liquidator.htokens();
expect(htokensBefore).to.be.empty;
await this.liquidator.clearHtoken(this.mocks.bond.address);
const htokensAfter = await this.liquidator.htokens();
const htokensAfter = this.liquidator.htokens();
expect(htokensAfter).to.be.empty;
});
});
Expand All @@ -17,15 +17,15 @@ export function shouldBehaveLikeClearHtoken(): void {
});

it("clears the hToken", async function () {
const htokensBefore = await this.liquidator.htokens();
const htokensBefore = this.liquidator.htokens();
expect(htokensBefore).to.not.be.empty;
const htoken = htokensBefore[this.mocks.bond.address];
expect(htoken).to.have.keys("maturity", "underlying", "underlyingPrecisionScalar");
expect(htoken.maturity).to.be.eq(await this.mocks.bond.maturity());
expect(htoken.underlying).to.be.eq(await this.mocks.bond.underlying());
expect(htoken.underlyingPrecisionScalar).to.be.eq(await this.mocks.bond.underlyingPrecisionScalar());
await this.liquidator.clearHtoken(this.mocks.bond.address);
const htokensAfter = await this.liquidator.htokens();
const htokensAfter = this.liquidator.htokens();
expect(htokensAfter).to.be.empty;
});
});
Expand Down

0 comments on commit bda63bf

Please sign in to comment.