From 0907b9255d3b79b5ed662f3fb080f69d9265cb20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Vincent-Genod?= <37535451+VGLoic@users.noreply.github.com> Date: Tue, 18 Oct 2022 09:10:57 +0200 Subject: [PATCH] feat: update signature of mockRequest (#52) --- src/mock-manager.ts | 6 +++--- src/testing-utils/testing-utils.ts | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/mock-manager.ts b/src/mock-manager.ts index d500117..97b3507 100644 --- a/src/mock-manager.ts +++ b/src/mock-manager.ts @@ -31,9 +31,9 @@ export class MockManager { * // Persistently mock "eth_chainId" request * mockManager.mockRequest("eth_chainId", "0x1", { persistent: true }); * // Mock with a dynamical value based on params - * // "personal_sign" in this case - * mockManager.mockRequest("personal_sign", async (params: any) => { - * let statement = (params as string[])[0]; + * // "personal_sign" in this case, `bobsWallet` is externally defined here + * mockManager.mockRequest("personal_sign", async (params: unknown[]) => { + * const statement = (params as string[])[0]; * return await bobsWallet.signMessage(statement); * }); * ``` diff --git a/src/testing-utils/testing-utils.ts b/src/testing-utils/testing-utils.ts index ada082e..403ad6d 100644 --- a/src/testing-utils/testing-utils.ts +++ b/src/testing-utils/testing-utils.ts @@ -31,7 +31,7 @@ export class LowLevelTestingUtils { /** * Mock a JSON-RPC request * @param method JSON-RPC method name - * @param data Data to be resolved, or error to be thrown in case of throw + * @param data Data to be resolved, or function to be called, or error to be thrown in case of throw * @param mockOptions.persistent If true, the mock will persist * @param mockOptions.shouldThrow If true, the mocked request will throw, the thrown error is the data field * @param mockOptions.timeout Timeout of the request, in milliseconds @@ -42,11 +42,17 @@ export class LowLevelTestingUtils { * lowLevelTestingUtils.mockRequest("eth_accounts", ["0x..."]); * // Persistently mock "eth_chainId" request * lowLevelTestingUtils.mockRequest("eth_chainId", "0x1", { persistent: true }); + * // Mock with a dynamical value based on params + * // "personal_sign" in this case, `bobsWallet` is externally defined here + * mockManager.mockRequest("personal_sign", async (params: unknown[]) => { + * const statement = (params as string[])[0]; + * return await bobsWallet.signMessage(statement); + * }); * ``` */ public mockRequest( method: string, - data: unknown, + data: unknown | ((params: unknown[]) => unknown), mockOptions: MockOptions = {} ) { this.mockManager.mockRequest(method, data, mockOptions);