Skip to content

Commit

Permalink
Merge branch 'main' into hm/test-duration
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Jan 14, 2025
2 parents 63dbffe + ba4fd7f commit 996273a
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ describe('onRpcRequest', () => {
const MOCK_VERSION = '1'; // Ethereum Mainnet

it('returns the current network version', async () => {
const { request, mockJsonRpc } = await installSnap();

// To avoid relying on the network, we mock the response from the Ethereum
// provider.
mockJsonRpc({
method: 'net_version',
result: MOCK_VERSION,
});
const { request } = await installSnap();

const response = await request({
method: 'getVersion',
Expand Down
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');
});
});
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { logError } from '@metamask/snaps-utils';
import type { Json, JsonRpcParams } from '@metamask/utils';

import { getAccountsHandler } from './accounts';
import { getChainIdHandler } from './chain-id';
import { getNetworkVersionHandler } from './net-version';
import { getProviderStateHandler } from './provider-state';

export type InternalMethodsMiddlewareHooks = {
Expand All @@ -19,6 +21,8 @@ const methodHandlers = {
metamask_getProviderState: getProviderStateHandler,
eth_requestAccounts: getAccountsHandler,
eth_accounts: getAccountsHandler,
eth_chainId: getChainIdHandler,
net_version: getNetworkVersionHandler,
/* eslint-enable @typescript-eslint/naming-convention */
};

Expand Down
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');
});
});
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();
}
1 change: 1 addition & 0 deletions packages/test-snaps/src/features/snaps/state/State.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const State: FunctionComponent = () => {
</pre>
</Result>

<GetState encrypted={false} />
<SetState encrypted={false} />
<ClearState encrypted={false} />
</Snap>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,22 @@ export const SetState: FunctionComponent<{ encrypted: boolean }> = ({
placeholder="Value"
value={value}
onChange={handleChangeValue}
id={encrypted ? 'dataManageState' : 'dataUnencryptedManageState'}
id={encrypted ? 'dataState' : 'dataUnencryptedState'}
className="mb-3"
/>
</Form.Group>

<Button
type="submit"
id={encrypted ? 'sendManageState' : 'sendUnencryptedManageState'}
id={encrypted ? 'sendState' : 'sendUnencryptedState'}
disabled={isLoading}
>
Set State
</Button>
</Form>

<Result className="mb-3">
<span
id={
encrypted
? 'sendManageStateResult'
: 'sendUnencryptedManageStateResult'
}
>
<span id={encrypted ? 'sendStateResult' : 'sendUnencryptedStateResult'}>
{JSON.stringify(data, null, 2)}
{JSON.stringify(error, null, 2)}
</span>
Expand Down

0 comments on commit 996273a

Please sign in to comment.