Skip to content

Commit

Permalink
Expand testing for expanded-view notifications (#2956)
Browse files Browse the repository at this point in the history
1. Updated the `@metamask/notification-example-snap` to have a method to
trigger an expanded view notification.
2. Updated `test-snaps` to trigger the new methods added above.
3. Updated `snaps-jest` to allow for unit testing of expanded view
notifications.
  • Loading branch information
hmalik88 authored Jan 10, 2025
1 parent f21e859 commit e68e282
Show file tree
Hide file tree
Showing 19 changed files with 587 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/examples/packages/notifications/snap.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SnapConfig } from '@metamask/snaps-cli';

const config: SnapConfig = {
input: './src/index.ts',
input: './src/index.tsx',
server: {
port: 8016,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "jrOzxCG7RKSqVS0TPw4sZiUoB4A00AXqdfkA5iGJ94g=",
"shasum": "DV0AnwJKM1iKxKscfZOjr/y8qAKP0tdp8wU+2PBMxk8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from '@jest/globals';
import { installSnap } from '@metamask/snaps-jest';
import { NotificationType } from '@metamask/snaps-sdk';
import { Address, Box, Row } from '@metamask/snaps-sdk/jsx';

describe('onRpcRequest', () => {
it('throws an error if the requested method does not exist', async () => {
Expand Down Expand Up @@ -38,6 +39,34 @@ describe('onRpcRequest', () => {
});
});

describe('inApp-expanded', () => {
it('sends an expanded view notification', async () => {
const { request } = await installSnap();

const response = await request({
method: 'inApp-expanded',
origin: 'Jest',
});

expect(response).toRespondWith(null);
expect(response).toSendNotification(
'Hello from MetaMask, click here for an expanded view!',
NotificationType.InApp,
'Hello World!',
<Box>
<Row
label="From"
variant="warning"
tooltip="This address has been deemed dangerous."
>
<Address address="0x1234567890123456789012345678901234567890" />
</Row>
</Box>,
{ text: 'Go home', href: 'metamask://client/' },
);
});
});

describe('native', () => {
it('sends a native notification', async () => {
const { request } = await installSnap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MethodNotFoundError, NotificationType } from '@metamask/snaps-sdk';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import { Box, Row, Address } from '@metamask/snaps-sdk/jsx';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
Expand Down Expand Up @@ -39,6 +40,28 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
},
});

case 'inApp-expanded':
return await snap.request({
method: 'snap_notify',
params: {
type: NotificationType.InApp,
message: 'Hello from MetaMask, click here for an expanded view!',
title: 'Hello World!',
content: (
<Box>
<Row
label="From"
variant="warning"
tooltip="This address has been deemed dangerous."
>
<Address address="0x1234567890123456789012345678901234567890" />
</Row>
</Box>
),
footerLink: { text: 'Go home', href: 'metamask://client/' },
},
});

default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/packages/notifications/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"baseUrl": "./"
"baseUrl": "./",
"composite": false
},
"include": ["src", "snap.config.ts"]
}
12 changes: 9 additions & 3 deletions packages/snaps-jest/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
NotificationType,
ComponentOrElement,
} from '@metamask/snaps-sdk';
import type { JSXElement } from '@metamask/snaps-sdk/jsx';

interface SnapsMatchers {
/**
Expand Down Expand Up @@ -42,16 +43,21 @@ interface SnapsMatchers {
* `expect(response.notifications).toContainEqual({ message, type })`.
*
* @param message - The expected notification message.
* @param type - The expected notification type, i.e., 'inApp' or 'native'. If
* not provided, the type will be ignored.
* @param type - The expected notification type, i.e., 'inApp' or 'native'.
* @param title - The title of an expanded notification.
* @param content - The content of an expanded notification.
* @param footerLink - The footer link of an expanded notification (if it exists).
* @throws If the snap did not send a notification with the expected message.
* @example
* const response = await request({ method: 'foo' });
* expect(response).toSendNotification('bar', NotificationType.InApp);
*/
toSendNotification(
message: string,
type?: EnumToUnion<NotificationType>,
type: EnumToUnion<NotificationType>,
title?: string,
content?: JSXElement,
footerLink?: { text: string; href: string },
): void;

/**
Expand Down
Loading

0 comments on commit e68e282

Please sign in to comment.