Skip to content

Commit

Permalink
feat: update signature of mockRequest (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
VGLoic authored Oct 18, 2022
1 parent 32c3b46 commit 0907b92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/mock-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
* });
* ```
Expand Down
10 changes: 8 additions & 2 deletions src/testing-utils/testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 0907b92

Please sign in to comment.