diff --git a/packages/examples/packages/cronjobs/src/index.test.ts b/packages/examples/packages/cronjobs/src/index.test.ts index 6a3d6c2a93..7d5a1ab15b 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 { onBackgroundEvent } = await installSnap(); + + const response = await onBackgroundEvent({ + // This would normally be called by the MetaMask extension, but to make + // this testable, `@metamask/snaps-jest` exposes a `onBackgroundEvent` 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..44a514cddf 100644 --- a/packages/snaps-jest/src/helpers.test.tsx +++ b/packages/snaps-jest/src/helpers.test.tsx @@ -751,6 +751,36 @@ describe('installSnap', () => { }); }); + describe('onBackgroundEvent', () => { + 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 { onBackgroundEvent, close } = await installSnap(snapId); + const response = await onBackgroundEvent({ + 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..d6f51cab11 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, + onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, @@ -194,6 +195,7 @@ export async function installSnap< onSignature, onCronjob, runCronjob, + onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, diff --git a/packages/snaps-simulation/src/helpers.test.tsx b/packages/snaps-simulation/src/helpers.test.tsx index 7dc369ef8e..adc96d41d4 100644 --- a/packages/snaps-simulation/src/helpers.test.tsx +++ b/packages/snaps-simulation/src/helpers.test.tsx @@ -525,6 +525,36 @@ describe('helpers', () => { }); }); + describe('onBackgroundEvent', () => { + 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 { onBackgroundEvent, close } = await installSnap(snapId); + const response = await onBackgroundEvent({ + 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 8ec77c2513..b1924e9656 100644 --- a/packages/snaps-simulation/src/helpers.ts +++ b/packages/snaps-simulation/src/helpers.ts @@ -111,6 +111,16 @@ 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. + */ + onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; + /** * Get the response from the snap's `onHomePage` method. * @@ -388,6 +398,7 @@ export function getHelpers({ onCronjob, runCronjob: 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 0c1c119c34..5edfbf8f37 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. + */ + onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest; + /** * Get the response from the snap's `onHomePage` method. *