Skip to content

Commit 002b7de

Browse files
committed
Move helper functions to simulation package
1 parent d4e1403 commit 002b7de

File tree

14 files changed

+1104
-648
lines changed

14 files changed

+1104
-648
lines changed

packages/snaps-jest/src/environment.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import type {
44
} from '@jest/environment';
55
import type { AbstractExecutionService } from '@metamask/snaps-controllers';
66
import type { SnapId } from '@metamask/snaps-sdk';
7+
import { installSnap } from '@metamask/snaps-simulation';
78
import type {
89
InstalledSnap,
910
InstallSnapOptions,
11+
SnapHelpers,
1012
} from '@metamask/snaps-simulation';
11-
import { installSnap } from '@metamask/snaps-simulation';
1213
import { assert, createModuleLogger } from '@metamask/utils';
1314
import type { Server } from 'http';
1415
import NodeEnvironment from 'jest-environment-node';
@@ -31,7 +32,7 @@ export class SnapsEnvironment extends NodeEnvironment {
3132

3233
#server: Server | undefined;
3334

34-
#instance: InstalledSnap | undefined;
35+
#instance: (InstalledSnap & SnapHelpers) | undefined;
3536

3637
/**
3738
* Constructor.

packages/snaps-jest/src/helpers.ts

+19-169
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
import type { AbstractExecutionService } from '@metamask/snaps-controllers';
22
import type { SnapId } from '@metamask/snaps-sdk';
3-
import type { InstallSnapOptions } from '@metamask/snaps-simulation';
4-
import {
5-
JsonRpcMockOptionsStruct,
6-
SignatureOptionsStruct,
7-
handleRequest,
8-
TransactionOptionsStruct,
9-
addJsonRpcMock,
10-
removeJsonRpcMock,
11-
SnapResponseWithInterfaceStruct,
12-
} from '@metamask/snaps-simulation';
13-
import { HandlerType, logInfo } from '@metamask/snaps-utils';
14-
import { create } from '@metamask/superstruct';
15-
import { assertStruct, createModuleLogger } from '@metamask/utils';
3+
import type { InstallSnapOptions, Snap } from '@metamask/snaps-simulation';
4+
import { logInfo } from '@metamask/snaps-utils';
5+
import { createModuleLogger } from '@metamask/utils';
166

177
import { rootLogger, getEnvironment } from './internals';
18-
import type {
19-
SnapResponseWithInterface,
20-
CronjobOptions,
21-
JsonRpcMockOptions,
22-
Snap,
23-
SnapResponse,
24-
TransactionOptions,
25-
} from './types';
268

279
const log = createModuleLogger(rootLogger, 'helpers');
2810

@@ -48,17 +30,6 @@ function getOptions<
4830
return [snapId, options];
4931
}
5032

51-
/**
52-
* Ensure that the actual response contains `getInterface`.
53-
*
54-
* @param response - The response of the handler.
55-
*/
56-
function assertIsResponseWithInterface(
57-
response: SnapResponse,
58-
): asserts response is SnapResponseWithInterface {
59-
assertStruct(response, SnapResponseWithInterfaceStruct);
60-
}
61-
6233
/**
6334
* Load a snap into the environment. This is the main entry point for testing
6435
* snaps: It returns a {@link Snap} object that can be used to interact with the
@@ -200,154 +171,33 @@ export async function installSnap<
200171
): Promise<Snap> {
201172
const resolvedOptions = getOptions(snapId, options);
202173
const {
203-
snapId: installedSnapId,
204-
store,
205-
executionService,
206-
runSaga,
207-
controllerMessenger,
174+
request,
175+
onTransaction,
176+
sendTransaction,
177+
onSignature,
178+
onCronjob,
179+
runCronjob,
180+
onHomePage,
181+
mockJsonRpc,
182+
close,
208183
} = await getEnvironment().installSnap(...resolvedOptions);
209184

210-
const onTransaction = async (
211-
request: TransactionOptions,
212-
): Promise<SnapResponseWithInterface> => {
213-
log('Sending transaction %o.', request);
214-
215-
const {
216-
origin: transactionOrigin,
217-
chainId,
218-
...transaction
219-
} = create(request, TransactionOptionsStruct);
220-
221-
const response = await handleRequest({
222-
snapId: installedSnapId,
223-
store,
224-
executionService,
225-
runSaga,
226-
controllerMessenger,
227-
handler: HandlerType.OnTransaction,
228-
request: {
229-
method: '',
230-
params: {
231-
chainId,
232-
transaction,
233-
transactionOrigin,
234-
},
235-
},
236-
});
237-
238-
assertIsResponseWithInterface(response);
239-
240-
return response;
241-
};
242-
243-
const onCronjob = (request: CronjobOptions) => {
244-
log('Running cronjob %o.', options);
245-
246-
return handleRequest({
247-
snapId: installedSnapId,
248-
store,
249-
executionService,
250-
controllerMessenger,
251-
runSaga,
252-
handler: HandlerType.OnCronjob,
253-
request,
254-
});
255-
};
256-
257185
return {
258-
request: (request) => {
259-
log('Sending request %o.', request);
260-
261-
return handleRequest({
262-
snapId: installedSnapId,
263-
store,
264-
executionService,
265-
controllerMessenger,
266-
runSaga,
267-
handler: HandlerType.OnRpcRequest,
268-
request,
269-
});
270-
},
271-
186+
request,
272187
onTransaction,
273-
sendTransaction: onTransaction,
274-
275-
onSignature: async (
276-
request: unknown,
277-
): Promise<SnapResponseWithInterface> => {
278-
log('Requesting signature %o.', request);
279-
280-
const { origin: signatureOrigin, ...signature } = create(
281-
request,
282-
SignatureOptionsStruct,
283-
);
284-
285-
const response = await handleRequest({
286-
snapId: installedSnapId,
287-
store,
288-
executionService,
289-
controllerMessenger,
290-
runSaga,
291-
handler: HandlerType.OnSignature,
292-
request: {
293-
method: '',
294-
params: {
295-
signature,
296-
signatureOrigin,
297-
},
298-
},
299-
});
300-
301-
assertIsResponseWithInterface(response);
302-
303-
return response;
304-
},
305-
188+
sendTransaction,
189+
onSignature,
306190
onCronjob,
307-
runCronjob: onCronjob,
308-
309-
onHomePage: async (): Promise<SnapResponseWithInterface> => {
310-
log('Rendering home page.');
311-
312-
const response = await handleRequest({
313-
snapId: installedSnapId,
314-
store,
315-
executionService,
316-
controllerMessenger,
317-
runSaga,
318-
handler: HandlerType.OnHomePage,
319-
request: {
320-
method: '',
321-
},
322-
});
323-
324-
assertIsResponseWithInterface(response);
325-
326-
return response;
327-
},
328-
329-
mockJsonRpc(mock: JsonRpcMockOptions) {
330-
log('Mocking JSON-RPC request %o.', mock);
331-
332-
const { method, result } = create(mock, JsonRpcMockOptionsStruct);
333-
store.dispatch(addJsonRpcMock({ method, result }));
334-
335-
return {
336-
unmock() {
337-
log('Unmocking JSON-RPC request %o.', mock);
338-
339-
store.dispatch(removeJsonRpcMock(method));
340-
},
341-
};
342-
},
343-
191+
runCronjob,
192+
onHomePage,
193+
mockJsonRpc,
344194
close: async () => {
345195
log('Closing execution service.');
346196
logInfo(
347197
'Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.',
348198
);
349199

350-
await executionService.terminateAllSnaps();
200+
await close();
351201
},
352202
};
353203
}

packages/snaps-jest/src/index.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import './global';
44
export { default, default as TestEnvironment } from './environment';
55
export * from './helpers';
66
export * from './options';
7-
export * from './types';
87

98
export {
109
assertCustomDialogHasNoFooter,
@@ -14,3 +13,29 @@ export {
1413
assertIsCustomDialog,
1514
assertIsPromptDialog,
1615
} from '@metamask/snaps-simulation';
16+
17+
export type {
18+
CronjobOptions,
19+
DefaultSnapInterface,
20+
DefaultSnapInterfaceWithFooter,
21+
DefaultSnapInterfaceWithPartialFooter,
22+
DefaultSnapInterfaceWithoutFooter,
23+
FileOptions,
24+
RequestOptions,
25+
SignatureOptions,
26+
Snap,
27+
SnapAlertInterface,
28+
SnapConfirmationInterface,
29+
SnapHandlerInterface,
30+
SnapInterface,
31+
SnapInterfaceActions,
32+
SnapOptions,
33+
SnapPromptInterface,
34+
SnapResponse,
35+
SnapResponseType,
36+
SnapResponseWithInterface,
37+
SnapResponseWithoutInterface,
38+
SnapRequest,
39+
SnapRequestObject,
40+
TransactionOptions,
41+
} from '@metamask/snaps-simulation';

packages/snaps-jest/src/test-utils/response.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { JSXElement } from '@metamask/snaps-sdk/jsx';
2-
3-
import type { SnapHandlerInterface, SnapResponse } from '../types';
2+
import type {
3+
SnapHandlerInterface,
4+
SnapResponse,
5+
} from '@metamask/snaps-simulation';
46

57
/**
68
* Get a mock response.
@@ -40,6 +42,7 @@ export function getMockInterfaceResponse(
4042
typeInField: jest.fn(),
4143
selectInDropdown: jest.fn(),
4244
selectFromRadioGroup: jest.fn(),
45+
selectFromSelector: jest.fn(),
4346
uploadFile: jest.fn(),
4447
};
4548
}

packages/snaps-jest/src/types/index.ts

-1
This file was deleted.

0 commit comments

Comments
 (0)