From 1aa0ab1386e33a49f9fe25b7018332d20fd456fe Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Mon, 23 Dec 2024 23:11:25 -0500 Subject: [PATCH 1/3] add new helper (alias) for background events --- packages/snaps-simulation/src/helpers.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/snaps-simulation/src/helpers.ts b/packages/snaps-simulation/src/helpers.ts index 8ec77c2513..b05e3ec180 100644 --- a/packages/snaps-simulation/src/helpers.ts +++ b/packages/snaps-simulation/src/helpers.ts @@ -111,6 +111,17 @@ export type SnapHelpers = { */ runCronjob(cronjob: CronjobOptions): SnapRequest; + /** + * Run a background event in the snap. This is similar to {@link request}, but the + * request will be sent to the `onCronjob` method of the snap. + * + * @param backgroundEvent - The background event request. This is similar to a JSON-RPC + * request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method. + * @returns The response promise, with extra {@link SnapRequestObject} fields. + * @deprecated Use {@link onCronjob} instead. + */ + runBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; + /** * Get the response from the snap's `onHomePage` method. * @@ -388,6 +399,7 @@ export function getHelpers({ onCronjob, runCronjob: onCronjob, + runBackgroundEvent: onCronjob, onHomePage: async (): Promise => { log('Rendering home page.'); From 148453b7be95ef5c83e5b71fe54774572e958031 Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Mon, 23 Dec 2024 23:33:26 -0500 Subject: [PATCH 2/3] update tests and add new helper --- .../packages/cronjobs/src/index.test.ts | 15 ++++++++++ packages/snaps-jest/src/helpers.test.tsx | 30 +++++++++++++++++++ packages/snaps-jest/src/helpers.ts | 2 ++ .../snaps-simulation/src/helpers.test.tsx | 30 +++++++++++++++++++ packages/snaps-simulation/src/helpers.ts | 1 - packages/snaps-simulation/src/types.ts | 10 +++++++ 6 files changed, 87 insertions(+), 1 deletion(-) diff --git a/packages/examples/packages/cronjobs/src/index.test.ts b/packages/examples/packages/cronjobs/src/index.test.ts index 6a3d6c2a93..bde5751702 100644 --- a/packages/examples/packages/cronjobs/src/index.test.ts +++ b/packages/examples/packages/cronjobs/src/index.test.ts @@ -29,4 +29,19 @@ describe('onCronjob', () => { expect(response).toRespondWith(null); }); }); + + describe('fireNotification', () => { + it('shows an inApp notification', async () => { + const { runBackgroundEvent } = await installSnap(); + + const response = await runBackgroundEvent({ + // This would normally be called by the MetaMask extension, but to make + // this testable, `@metamask/snaps-jest` exposes a `runBackgroundEvent` method. + method: 'fireNotification', + }); + + expect(response).toRespondWith(null); + expect(response).toSendNotification('Hello world!', 'inApp'); + }); + }); }); diff --git a/packages/snaps-jest/src/helpers.test.tsx b/packages/snaps-jest/src/helpers.test.tsx index 998325c349..cf43eebb57 100644 --- a/packages/snaps-jest/src/helpers.test.tsx +++ b/packages/snaps-jest/src/helpers.test.tsx @@ -751,6 +751,36 @@ describe('installSnap', () => { }); }); + describe('runBackgroundEvent', () => { + it('runs a cronjob and returns the result', async () => { + jest.spyOn(console, 'log').mockImplementation(); + + const { snapId, close: closeServer } = await getMockServer({ + sourceCode: ` + module.exports.onCronjob = async ({ request }) => { + return request.method; + }; + `, + }); + + const { runBackgroundEvent, close } = await installSnap(snapId); + const response = await runBackgroundEvent({ + method: 'foo', + }); + + expect(response).toStrictEqual( + expect.objectContaining({ + response: { + result: 'foo', + }, + }), + ); + + await close(); + await closeServer(); + }); + }); + describe('getHomePage', () => { it('sends a OnHomePage request and returns the result', async () => { jest.spyOn(console, 'log').mockImplementation(); diff --git a/packages/snaps-jest/src/helpers.ts b/packages/snaps-jest/src/helpers.ts index 0688084cd6..3cc726a0c5 100644 --- a/packages/snaps-jest/src/helpers.ts +++ b/packages/snaps-jest/src/helpers.ts @@ -177,6 +177,7 @@ export async function installSnap< onSignature, onCronjob, runCronjob, + runBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, @@ -194,6 +195,7 @@ export async function installSnap< onSignature, onCronjob, runCronjob, + runBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, diff --git a/packages/snaps-simulation/src/helpers.test.tsx b/packages/snaps-simulation/src/helpers.test.tsx index 7dc369ef8e..7276cb26fa 100644 --- a/packages/snaps-simulation/src/helpers.test.tsx +++ b/packages/snaps-simulation/src/helpers.test.tsx @@ -525,6 +525,36 @@ describe('helpers', () => { }); }); + describe('runBackgroundEvent', () => { + it('runs a cronjob and returns the result', async () => { + jest.spyOn(console, 'log').mockImplementation(); + + const { snapId, close: closeServer } = await getMockServer({ + sourceCode: ` + module.exports.onCronjob = async ({ request }) => { + return request.method; + }; + `, + }); + + const { runBackgroundEvent, close } = await installSnap(snapId); + const response = await runBackgroundEvent({ + method: 'foo', + }); + + expect(response).toStrictEqual( + expect.objectContaining({ + response: { + result: 'foo', + }, + }), + ); + + await close(); + await closeServer(); + }); + }); + describe('getHomePage', () => { it('sends a OnHomePage request and returns the result', async () => { jest.spyOn(console, 'log').mockImplementation(); diff --git a/packages/snaps-simulation/src/helpers.ts b/packages/snaps-simulation/src/helpers.ts index b05e3ec180..db9253c784 100644 --- a/packages/snaps-simulation/src/helpers.ts +++ b/packages/snaps-simulation/src/helpers.ts @@ -118,7 +118,6 @@ export type SnapHelpers = { * @param backgroundEvent - The background event request. This is similar to a JSON-RPC * request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method. * @returns The response promise, with extra {@link SnapRequestObject} fields. - * @deprecated Use {@link onCronjob} instead. */ runBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; diff --git a/packages/snaps-simulation/src/types.ts b/packages/snaps-simulation/src/types.ts index 0c1c119c34..ecea0af091 100644 --- a/packages/snaps-simulation/src/types.ts +++ b/packages/snaps-simulation/src/types.ts @@ -418,6 +418,16 @@ export type Snap = { */ runCronjob(cronjob: CronjobOptions): SnapRequest; + /** + * Run a background event in the snap. This is similar to {@link request}, but the + * request will be sent to the `onCronjob` method of the snap. + * + * @param backgroundEvent - The cronjob request. This is similar to a JSON-RPC + * request, and is normally specified as the `request` param in the `snap_scheduleBackgroundEvent` method. + * @returns The response promise, with extra {@link SnapRequestObject} fields. + */ + runBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; + /** * Get the response from the snap's `onHomePage` method. * From c7d4a0bcc4bd8f2a6bf252e8619d6a3ce58222ea Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Mon, 6 Jan 2025 07:30:22 -0500 Subject: [PATCH 3/3] change name of helper --- packages/examples/packages/cronjobs/src/index.test.ts | 6 +++--- packages/snaps-jest/src/helpers.test.tsx | 6 +++--- packages/snaps-jest/src/helpers.ts | 4 ++-- packages/snaps-simulation/src/helpers.test.tsx | 6 +++--- packages/snaps-simulation/src/helpers.ts | 4 ++-- packages/snaps-simulation/src/types.ts | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/examples/packages/cronjobs/src/index.test.ts b/packages/examples/packages/cronjobs/src/index.test.ts index bde5751702..7d5a1ab15b 100644 --- a/packages/examples/packages/cronjobs/src/index.test.ts +++ b/packages/examples/packages/cronjobs/src/index.test.ts @@ -32,11 +32,11 @@ describe('onCronjob', () => { describe('fireNotification', () => { it('shows an inApp notification', async () => { - const { runBackgroundEvent } = await installSnap(); + const { onBackgroundEvent } = await installSnap(); - const response = await runBackgroundEvent({ + const response = await onBackgroundEvent({ // This would normally be called by the MetaMask extension, but to make - // this testable, `@metamask/snaps-jest` exposes a `runBackgroundEvent` method. + // this testable, `@metamask/snaps-jest` exposes a `onBackgroundEvent` method. method: 'fireNotification', }); diff --git a/packages/snaps-jest/src/helpers.test.tsx b/packages/snaps-jest/src/helpers.test.tsx index cf43eebb57..44a514cddf 100644 --- a/packages/snaps-jest/src/helpers.test.tsx +++ b/packages/snaps-jest/src/helpers.test.tsx @@ -751,7 +751,7 @@ describe('installSnap', () => { }); }); - describe('runBackgroundEvent', () => { + describe('onBackgroundEvent', () => { it('runs a cronjob and returns the result', async () => { jest.spyOn(console, 'log').mockImplementation(); @@ -763,8 +763,8 @@ describe('installSnap', () => { `, }); - const { runBackgroundEvent, close } = await installSnap(snapId); - const response = await runBackgroundEvent({ + const { onBackgroundEvent, close } = await installSnap(snapId); + const response = await onBackgroundEvent({ method: 'foo', }); diff --git a/packages/snaps-jest/src/helpers.ts b/packages/snaps-jest/src/helpers.ts index 3cc726a0c5..d6f51cab11 100644 --- a/packages/snaps-jest/src/helpers.ts +++ b/packages/snaps-jest/src/helpers.ts @@ -177,7 +177,7 @@ export async function installSnap< onSignature, onCronjob, runCronjob, - runBackgroundEvent, + onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, @@ -195,7 +195,7 @@ export async function installSnap< onSignature, onCronjob, runCronjob, - runBackgroundEvent, + onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, diff --git a/packages/snaps-simulation/src/helpers.test.tsx b/packages/snaps-simulation/src/helpers.test.tsx index 7276cb26fa..adc96d41d4 100644 --- a/packages/snaps-simulation/src/helpers.test.tsx +++ b/packages/snaps-simulation/src/helpers.test.tsx @@ -525,7 +525,7 @@ describe('helpers', () => { }); }); - describe('runBackgroundEvent', () => { + describe('onBackgroundEvent', () => { it('runs a cronjob and returns the result', async () => { jest.spyOn(console, 'log').mockImplementation(); @@ -537,8 +537,8 @@ describe('helpers', () => { `, }); - const { runBackgroundEvent, close } = await installSnap(snapId); - const response = await runBackgroundEvent({ + const { onBackgroundEvent, close } = await installSnap(snapId); + const response = await onBackgroundEvent({ method: 'foo', }); diff --git a/packages/snaps-simulation/src/helpers.ts b/packages/snaps-simulation/src/helpers.ts index db9253c784..b1924e9656 100644 --- a/packages/snaps-simulation/src/helpers.ts +++ b/packages/snaps-simulation/src/helpers.ts @@ -119,7 +119,7 @@ export type SnapHelpers = { * request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method. * @returns The response promise, with extra {@link SnapRequestObject} fields. */ - runBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; + onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; /** * Get the response from the snap's `onHomePage` method. @@ -398,7 +398,7 @@ export function getHelpers({ onCronjob, runCronjob: onCronjob, - runBackgroundEvent: onCronjob, + onBackgroundEvent: onCronjob, onHomePage: async (): Promise => { log('Rendering home page.'); diff --git a/packages/snaps-simulation/src/types.ts b/packages/snaps-simulation/src/types.ts index ecea0af091..5edfbf8f37 100644 --- a/packages/snaps-simulation/src/types.ts +++ b/packages/snaps-simulation/src/types.ts @@ -426,7 +426,7 @@ export type Snap = { * request, and is normally specified as the `request` param in the `snap_scheduleBackgroundEvent` method. * @returns The response promise, with extra {@link SnapRequestObject} fields. */ - runBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; + onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; /** * Get the response from the snap's `onHomePage` method.