Skip to content

Commit 68245cc

Browse files
hmalik88PatrykLucka
authored andcommitted
Add helper for background events (#2974)
Added new helper `onBackgroundEvent` which is an alias of the `onCronjob` helper. Updated the cronjob example snap's tests as well.
1 parent 4645e48 commit 68245cc

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

packages/examples/packages/cronjobs/src/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,19 @@ describe('onCronjob', () => {
2929
expect(response).toRespondWith(null);
3030
});
3131
});
32+
33+
describe('fireNotification', () => {
34+
it('shows an inApp notification', async () => {
35+
const { onBackgroundEvent } = await installSnap();
36+
37+
const response = await onBackgroundEvent({
38+
// This would normally be called by the MetaMask extension, but to make
39+
// this testable, `@metamask/snaps-jest` exposes a `onBackgroundEvent` method.
40+
method: 'fireNotification',
41+
});
42+
43+
expect(response).toRespondWith(null);
44+
expect(response).toSendNotification('Hello world!', 'inApp');
45+
});
46+
});
3247
});

packages/snaps-jest/src/helpers.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,36 @@ describe('installSnap', () => {
751751
});
752752
});
753753

754+
describe('onBackgroundEvent', () => {
755+
it('runs a cronjob and returns the result', async () => {
756+
jest.spyOn(console, 'log').mockImplementation();
757+
758+
const { snapId, close: closeServer } = await getMockServer({
759+
sourceCode: `
760+
module.exports.onCronjob = async ({ request }) => {
761+
return request.method;
762+
};
763+
`,
764+
});
765+
766+
const { onBackgroundEvent, close } = await installSnap(snapId);
767+
const response = await onBackgroundEvent({
768+
method: 'foo',
769+
});
770+
771+
expect(response).toStrictEqual(
772+
expect.objectContaining({
773+
response: {
774+
result: 'foo',
775+
},
776+
}),
777+
);
778+
779+
await close();
780+
await closeServer();
781+
});
782+
});
783+
754784
describe('getHomePage', () => {
755785
it('sends a OnHomePage request and returns the result', async () => {
756786
jest.spyOn(console, 'log').mockImplementation();

packages/snaps-jest/src/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export async function installSnap<
177177
onSignature,
178178
onCronjob,
179179
runCronjob,
180+
onBackgroundEvent,
180181
onHomePage,
181182
onSettingsPage,
182183
onKeyringRequest,
@@ -194,6 +195,7 @@ export async function installSnap<
194195
onSignature,
195196
onCronjob,
196197
runCronjob,
198+
onBackgroundEvent,
197199
onHomePage,
198200
onSettingsPage,
199201
onKeyringRequest,

packages/snaps-simulation/src/helpers.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,36 @@ describe('helpers', () => {
525525
});
526526
});
527527

528+
describe('onBackgroundEvent', () => {
529+
it('runs a cronjob and returns the result', async () => {
530+
jest.spyOn(console, 'log').mockImplementation();
531+
532+
const { snapId, close: closeServer } = await getMockServer({
533+
sourceCode: `
534+
module.exports.onCronjob = async ({ request }) => {
535+
return request.method;
536+
};
537+
`,
538+
});
539+
540+
const { onBackgroundEvent, close } = await installSnap(snapId);
541+
const response = await onBackgroundEvent({
542+
method: 'foo',
543+
});
544+
545+
expect(response).toStrictEqual(
546+
expect.objectContaining({
547+
response: {
548+
result: 'foo',
549+
},
550+
}),
551+
);
552+
553+
await close();
554+
await closeServer();
555+
});
556+
});
557+
528558
describe('getHomePage', () => {
529559
it('sends a OnHomePage request and returns the result', async () => {
530560
jest.spyOn(console, 'log').mockImplementation();

packages/snaps-simulation/src/helpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ export type SnapHelpers = {
111111
*/
112112
runCronjob(cronjob: CronjobOptions): SnapRequest;
113113

114+
/**
115+
* Run a background event in the snap. This is similar to {@link request}, but the
116+
* request will be sent to the `onCronjob` method of the snap.
117+
*
118+
* @param backgroundEvent - The background event request. This is similar to a JSON-RPC
119+
* request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method.
120+
* @returns The response promise, with extra {@link SnapRequestObject} fields.
121+
*/
122+
onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest;
123+
114124
/**
115125
* Get the response from the snap's `onHomePage` method.
116126
*
@@ -388,6 +398,7 @@ export function getHelpers({
388398

389399
onCronjob,
390400
runCronjob: onCronjob,
401+
onBackgroundEvent: onCronjob,
391402

392403
onHomePage: async (): Promise<SnapResponseWithInterface> => {
393404
log('Rendering home page.');

packages/snaps-simulation/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ export type Snap = {
418418
*/
419419
runCronjob(cronjob: CronjobOptions): SnapRequest;
420420

421+
/**
422+
* Run a background event in the snap. This is similar to {@link request}, but the
423+
* request will be sent to the `onCronjob` method of the snap.
424+
*
425+
* @param backgroundEvent - The cronjob request. This is similar to a JSON-RPC
426+
* request, and is normally specified as the `request` param in the `snap_scheduleBackgroundEvent` method.
427+
* @returns The response promise, with extra {@link SnapRequestObject} fields.
428+
*/
429+
onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest;
430+
421431
/**
422432
* Get the response from the snap's `onHomePage` method.
423433
*

0 commit comments

Comments
 (0)