Skip to content

Commit e68e282

Browse files
authored
Expand testing for expanded-view notifications (#2956)
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.
1 parent f21e859 commit e68e282

File tree

19 files changed

+587
-93
lines changed

19 files changed

+587
-93
lines changed

packages/examples/packages/notifications/snap.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SnapConfig } from '@metamask/snaps-cli';
22

33
const config: SnapConfig = {
4-
input: './src/index.ts',
4+
input: './src/index.tsx',
55
server: {
66
port: 8016,
77
},

packages/examples/packages/notifications/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "jrOzxCG7RKSqVS0TPw4sZiUoB4A00AXqdfkA5iGJ94g=",
10+
"shasum": "DV0AnwJKM1iKxKscfZOjr/y8qAKP0tdp8wU+2PBMxk8=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/notifications/src/index.test.ts renamed to packages/examples/packages/notifications/src/index.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from '@jest/globals';
22
import { installSnap } from '@metamask/snaps-jest';
33
import { NotificationType } from '@metamask/snaps-sdk';
4+
import { Address, Box, Row } from '@metamask/snaps-sdk/jsx';
45

56
describe('onRpcRequest', () => {
67
it('throws an error if the requested method does not exist', async () => {
@@ -38,6 +39,34 @@ describe('onRpcRequest', () => {
3839
});
3940
});
4041

42+
describe('inApp-expanded', () => {
43+
it('sends an expanded view notification', async () => {
44+
const { request } = await installSnap();
45+
46+
const response = await request({
47+
method: 'inApp-expanded',
48+
origin: 'Jest',
49+
});
50+
51+
expect(response).toRespondWith(null);
52+
expect(response).toSendNotification(
53+
'Hello from MetaMask, click here for an expanded view!',
54+
NotificationType.InApp,
55+
'Hello World!',
56+
<Box>
57+
<Row
58+
label="From"
59+
variant="warning"
60+
tooltip="This address has been deemed dangerous."
61+
>
62+
<Address address="0x1234567890123456789012345678901234567890" />
63+
</Row>
64+
</Box>,
65+
{ text: 'Go home', href: 'metamask://client/' },
66+
);
67+
});
68+
});
69+
4170
describe('native', () => {
4271
it('sends a native notification', async () => {
4372
const { request } = await installSnap();

packages/examples/packages/notifications/src/index.ts renamed to packages/examples/packages/notifications/src/index.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MethodNotFoundError, NotificationType } from '@metamask/snaps-sdk';
22
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
3+
import { Box, Row, Address } from '@metamask/snaps-sdk/jsx';
34

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

43+
case 'inApp-expanded':
44+
return await snap.request({
45+
method: 'snap_notify',
46+
params: {
47+
type: NotificationType.InApp,
48+
message: 'Hello from MetaMask, click here for an expanded view!',
49+
title: 'Hello World!',
50+
content: (
51+
<Box>
52+
<Row
53+
label="From"
54+
variant="warning"
55+
tooltip="This address has been deemed dangerous."
56+
>
57+
<Address address="0x1234567890123456789012345678901234567890" />
58+
</Row>
59+
</Box>
60+
),
61+
footerLink: { text: 'Go home', href: 'metamask://client/' },
62+
},
63+
});
64+
4265
default:
4366
// eslint-disable-next-line @typescript-eslint/no-throw-literal
4467
throw new MethodNotFoundError({ method: request.method });
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4-
"composite": false,
5-
"baseUrl": "./"
4+
"baseUrl": "./",
5+
"composite": false
66
},
77
"include": ["src", "snap.config.ts"]
88
}

packages/snaps-jest/src/global.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
NotificationType,
66
ComponentOrElement,
77
} from '@metamask/snaps-sdk';
8+
import type { JSXElement } from '@metamask/snaps-sdk/jsx';
89

910
interface SnapsMatchers {
1011
/**
@@ -42,16 +43,21 @@ interface SnapsMatchers {
4243
* `expect(response.notifications).toContainEqual({ message, type })`.
4344
*
4445
* @param message - The expected notification message.
45-
* @param type - The expected notification type, i.e., 'inApp' or 'native'. If
46-
* not provided, the type will be ignored.
46+
* @param type - The expected notification type, i.e., 'inApp' or 'native'.
47+
* @param title - The title of an expanded notification.
48+
* @param content - The content of an expanded notification.
49+
* @param footerLink - The footer link of an expanded notification (if it exists).
4750
* @throws If the snap did not send a notification with the expected message.
4851
* @example
4952
* const response = await request({ method: 'foo' });
5053
* expect(response).toSendNotification('bar', NotificationType.InApp);
5154
*/
5255
toSendNotification(
5356
message: string,
54-
type?: EnumToUnion<NotificationType>,
57+
type: EnumToUnion<NotificationType>,
58+
title?: string,
59+
content?: JSXElement,
60+
footerLink?: { text: string; href: string },
5561
): void;
5662

5763
/**

0 commit comments

Comments
 (0)