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 home page support to snaps-jest #2104

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 8 additions & 10 deletions packages/examples/packages/home-page/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { describe, it } from '@jest/globals';
import { installSnap } from '@metamask/snaps-jest';
import { panel, text, heading } from '@metamask/snaps-sdk';

import { onHomePage } from '.';

// The Snaps E2E testing framework doesn't currently support onHomePage, so we unit
// test it instead.
describe('onHomePage', () => {
it('returns custom UI', async () => {
expect(await onHomePage()).toStrictEqual({
content: panel([
heading('Hello world!'),
text('Welcome to my Snap home page!'),
]),
});
const { getHomePage } = await installSnap();

const response = await getHomePage();

expect(response).toRender(
panel([heading('Hello world!'), text('Welcome to my Snap home page!')]),
);
});
});
26 changes: 26 additions & 0 deletions packages/snaps-jest/src/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,32 @@ describe('installSnap', () => {
});
});

describe('getHomePage', () => {
it('sends a OnHomePage request and returns the result', async () => {
jest.spyOn(console, 'log').mockImplementation();

const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onHomePage = async () => {
return { content: { type: 'text', value: 'Hello, world!' } };
};
`,
});

const { getHomePage, close } = await installSnap(snapId);
const response = await getHomePage();

expect(response).toStrictEqual(
expect.objectContaining({
content: { type: 'text', value: 'Hello, world!' },
}),
);

await close();
await closeServer();
});
});

describe('mockJsonRpc', () => {
it('mocks a JSON-RPC method', async () => {
jest.spyOn(console, 'log').mockImplementation();
Expand Down
15 changes: 15 additions & 0 deletions packages/snaps-jest/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ export async function installSnap<
});
},

getHomePage: async (): Promise<SnapResponse> => {
log('Rendering home page.');

return handleRequest({
snapId: installedSnapId,
store,
executionService,
runSaga,
handler: HandlerType.OnHomePage,
request: {
method: '',
},
});
},

mockJsonRpc(mock: JsonRpcMockOptions) {
log('Mocking JSON-RPC request %o.', mock);

Expand Down
9 changes: 9 additions & 0 deletions packages/snaps-jest/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ export type Snap = {
*/
runCronjob(cronjob: CronjobOptions): SnapRequest;

/**
* Makes a request to the snap to render the snaps home page.
*
* This method takes no arguments.
*
* @returns The response.
*/
getHomePage(): Promise<SnapResponse>;

/**
* Mock a JSON-RPC request. This will cause the snap to respond with the
* specified response when a request with the specified method is sent.
Expand Down