Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/snaps-jest/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export async function installSnap<
onUpdate,
onNameLookup,
onProtocolRequest,
onClientRequest,
mockJsonRpc,
close,
} = await getEnvironment().installSnap(...resolvedOptions);
Expand All @@ -209,6 +210,7 @@ export async function installSnap<
onUpdate,
onNameLookup,
onProtocolRequest,
onClientRequest,
mockJsonRpc,
close: async () => {
log('Closing execution service.');
Expand Down
28 changes: 28 additions & 0 deletions packages/snaps-simulation/src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,34 @@ describe('helpers', () => {
});
});

describe('onClientRequest', () => {
it('sends a onClientRequest request and returns the result', async () => {
jest.spyOn(console, 'log').mockImplementation();

const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onClientRequest = async ({ request }) => {
return request.method;
};
`,
});

const { onClientRequest, close } = await installSnap(snapId);
const response = await onClientRequest({ method: 'foo' });

expect(response).toStrictEqual(
expect.objectContaining({
response: {
result: 'foo',
},
}),
);

await close();
await closeServer();
});
});

describe('mockJsonRpc', () => {
it('mocks a JSON-RPC method', async () => {
jest.spyOn(console, 'log').mockImplementation();
Expand Down
24 changes: 24 additions & 0 deletions packages/snaps-simulation/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ export type SnapHelpers = {
request: RequestOptions,
): Promise<SnapResponseWithoutInterface>;

/**
* Send a JSON-RPC client request to the Snap.
*
* @param request - The JSON-RPC request.
* @returns The response promise, with extra {@link SnapRequestObject} fields.
*/
onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;

/**
* Mock a JSON-RPC request. This will cause the snap to respond with the
* specified response when a request with the specified method is sent.
Expand Down Expand Up @@ -491,6 +499,22 @@ export function getHelpers({
return response;
},

// This can't be async because it returns a `SnapRequest`.
// eslint-disable-next-line @typescript-eslint/promise-function-async
onClientRequest: (request) => {
log('Sending client request.');

return handleRequest({
snapId,
store,
executionService,
controllerMessenger,
runSaga,
handler: HandlerType.OnClientRequest,
request,
});
},

mockJsonRpc(mock: JsonRpcMockOptions) {
log('Mocking JSON-RPC request %o.', mock);

Expand Down
8 changes: 8 additions & 0 deletions packages/snaps-simulation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ export type Snap = {
request: RequestOptions,
): Promise<SnapResponseWithoutInterface>;

/**
* Send a JSON-RPC client request to the Snap.
*
* @param request - The JSON-RPC request.
* @returns The response promise, with extra {@link SnapRequestObject} fields.
*/
onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;

/**
* Mock a JSON-RPC request. This will cause the snap to respond with the
* specified response when a request with the specified method is sent.
Expand Down
Loading