Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper for background events #2974

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions packages/examples/packages/cronjobs/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
30 changes: 30 additions & 0 deletions packages/snaps-jest/src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-jest/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export async function installSnap<
onSignature,
onCronjob,
runCronjob,
runBackgroundEvent,
onHomePage,
onSettingsPage,
onKeyringRequest,
Expand All @@ -194,6 +195,7 @@ export async function installSnap<
onSignature,
onCronjob,
runCronjob,
runBackgroundEvent,
onHomePage,
onSettingsPage,
onKeyringRequest,
Expand Down
30 changes: 30 additions & 0 deletions packages/snaps-simulation/src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions packages/snaps-simulation/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
runBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest;
Mrtenz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get the response from the snap's `onHomePage` method.
*
Expand Down Expand Up @@ -388,6 +398,7 @@ export function getHelpers({

onCronjob,
runCronjob: onCronjob,
runBackgroundEvent: onCronjob,

onHomePage: async (): Promise<SnapResponseWithInterface> => {
log('Rendering home page.');
Expand Down
10 changes: 10 additions & 0 deletions packages/snaps-simulation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading