-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Mock chain ID and network version calls in simulation (#3017)
Mock calls to `eth_chainId` and `net_version` in the simulation framework, so users don't have to mock them manually. This should also prevent some flakiness in our tests.
- Loading branch information
1 parent
030ebee
commit 1ab6bc9
Showing
6 changed files
with
129 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/snaps-simulation/src/middleware/internal-methods/chain-id.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Json, PendingJsonRpcResponse } from '@metamask/utils'; | ||
|
||
import { getChainIdHandler } from './chain-id'; | ||
|
||
describe('getChainIdHandler', () => { | ||
it('returns the chain id', async () => { | ||
const end = jest.fn(); | ||
const result: PendingJsonRpcResponse<Json> = { | ||
jsonrpc: '2.0' as const, | ||
id: 1, | ||
}; | ||
|
||
await getChainIdHandler( | ||
{ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: 'eth_chainId', | ||
params: [], | ||
}, | ||
result, | ||
jest.fn(), | ||
end, | ||
); | ||
|
||
expect(end).toHaveBeenCalled(); | ||
expect(result.result).toBe('0x01'); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
packages/snaps-simulation/src/middleware/internal-methods/chain-id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { | ||
JsonRpcEngineEndCallback, | ||
JsonRpcEngineNextCallback, | ||
} from '@metamask/json-rpc-engine'; | ||
import type { | ||
Json, | ||
JsonRpcRequest, | ||
PendingJsonRpcResponse, | ||
} from '@metamask/utils'; | ||
|
||
/** | ||
* A mock handler for eth_chainId that always returns a specific | ||
* hardcoded result. | ||
* | ||
* @param _request - Incoming JSON-RPC request. Ignored for this specific | ||
* handler. | ||
* @param response - The outgoing JSON-RPC response, modified to return the | ||
* result. | ||
* @param _next - The `json-rpc-engine` middleware next handler. | ||
* @param end - The `json-rpc-engine` middleware end handler. | ||
* @returns The JSON-RPC response. | ||
*/ | ||
export async function getChainIdHandler( | ||
_request: JsonRpcRequest, | ||
response: PendingJsonRpcResponse<Json>, | ||
_next: JsonRpcEngineNextCallback, | ||
end: JsonRpcEngineEndCallback, | ||
) { | ||
// For now this will return a mocked result, this should probably match | ||
// whatever network the simulation is using. | ||
response.result = '0x01'; | ||
|
||
return end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Json, PendingJsonRpcResponse } from '@metamask/utils'; | ||
|
||
import { getNetworkVersionHandler } from './net-version'; | ||
|
||
describe('getNetworkVersionHandler', () => { | ||
it('returns the network version', async () => { | ||
const end = jest.fn(); | ||
const result: PendingJsonRpcResponse<Json> = { | ||
jsonrpc: '2.0' as const, | ||
id: 1, | ||
}; | ||
|
||
await getNetworkVersionHandler( | ||
{ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: 'net_version', | ||
params: [], | ||
}, | ||
result, | ||
jest.fn(), | ||
end, | ||
); | ||
|
||
expect(end).toHaveBeenCalled(); | ||
expect(result.result).toBe('1'); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
packages/snaps-simulation/src/middleware/internal-methods/net-version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { | ||
JsonRpcEngineEndCallback, | ||
JsonRpcEngineNextCallback, | ||
} from '@metamask/json-rpc-engine'; | ||
import type { | ||
Json, | ||
JsonRpcRequest, | ||
PendingJsonRpcResponse, | ||
} from '@metamask/utils'; | ||
|
||
/** | ||
* A mock handler for net_version that always returns a specific | ||
* hardcoded result. | ||
* | ||
* @param _request - Incoming JSON-RPC request. Ignored for this specific | ||
* handler. | ||
* @param response - The outgoing JSON-RPC response, modified to return the | ||
* result. | ||
* @param _next - The `json-rpc-engine` middleware next handler. | ||
* @param end - The `json-rpc-engine` middleware end handler. | ||
* @returns The JSON-RPC response. | ||
*/ | ||
export async function getNetworkVersionHandler( | ||
_request: JsonRpcRequest, | ||
response: PendingJsonRpcResponse<Json>, | ||
_next: JsonRpcEngineNextCallback, | ||
end: JsonRpcEngineEndCallback, | ||
) { | ||
// For now this will return a mocked result, this should probably match | ||
// whatever network the simulation is using. | ||
response.result = '1'; | ||
|
||
return end(); | ||
} |