Skip to content

Commit

Permalink
Allow stubbing all getBlock and encodeFunctionData calls by omitt…
Browse files Browse the repository at this point in the history
…ing params.
  • Loading branch information
ryangoree committed Nov 4, 2024
1 parent 25a1e75 commit 4bb4281
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
10 changes: 2 additions & 8 deletions packages/drift/src/adapter/MockAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe("MockAdapter", () => {
} catch (e) {
error = e;
}
console.log("ERROR", error);
expect(error).toBeInstanceOf(Error);
});

Expand All @@ -59,6 +58,7 @@ describe("MockAdapter", () => {
};
adapter.onGetBlock().resolves(block);
expect(await adapter.getBlock()).toBe(block);
expect(await adapter.getBlock({ blockNumber: 123n })).toBe(block);
});

it("Can be stubbed with specific args", async () => {
Expand Down Expand Up @@ -219,13 +219,7 @@ describe("MockAdapter", () => {

it("Can be stubbed", async () => {
const adapter = new MockAdapter();
const foo = adapter
.onEncodeFunctionData({
abi: erc20.abi,
fn: "balanceOf",
args: { account: "0x" },
})
.returns("0x123");
adapter.onEncodeFunctionData().returns("0x123");
expect(
adapter.encodeFunctionData({
abi: erc20.abi,
Expand Down
33 changes: 22 additions & 11 deletions packages/drift/src/adapter/MockAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ export class MockAdapter implements ReadWriteAdapter {
// getBlock //

onGetBlock(params?: Partial<NetworkGetBlockParams>) {
return this.stubs
.get<[NetworkGetBlockParams?], Promise<Block | undefined>>({
method: "getBlock",
})
.withArgs(params);
let stub = this.stubs.get<
[NetworkGetBlockParams?],
Promise<Block | undefined>
>({
method: "getBlock",
});
if (params) {
stub = stub.withArgs(params);
}
return stub;
}

async getBlock(params?: NetworkGetBlockParams) {
Expand Down Expand Up @@ -142,18 +147,24 @@ export class MockAdapter implements ReadWriteAdapter {
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi>,
>(
params: OptionalKeys<
params?: OptionalKeys<
AdapterEncodeFunctionDataParams<TAbi, TFunctionName>,
"args"
>,
) {
return this.stubs
.get<[AdapterEncodeFunctionDataParams<TAbi, TFunctionName>], Bytes>({
method: "encodeFunctionData",
})
.withArgs(
let stub = this.stubs.get<
[AdapterEncodeFunctionDataParams<TAbi, TFunctionName>],
Bytes
>({
method: "encodeFunctionData",
});

if (params) {
stub = stub.withArgs(
params as Partial<AdapterEncodeFunctionDataParams<TAbi, TFunctionName>>,
);
}
return stub;
}

encodeFunctionData<
Expand Down

0 comments on commit 4bb4281

Please sign in to comment.