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

Expand testing for expanded-view notifications #2956

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
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": "BCoBVWLL9kEMS4mzBTMcdS5vPQvj8L9ghKBJLng9mg0=",
"shasum": "Eb+BA76BVAepPArIg/rG5eUOOHDm4mbiHE/GzpFOeHo=",
"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':
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
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
5 changes: 3 additions & 2 deletions packages/examples/packages/notifications/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"baseUrl": "./"
"baseUrl": "./",
"jsx": "react-jsxdev",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this? jsxdev has additional validation which we don't really need in Snaps, since JSX is validated by the client.

"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>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional? If so, it's a breaking change.

Suggested change
type: EnumToUnion<NotificationType>,
type?: EnumToUnion<NotificationType>,

title?: string,
content?: JSXElement,
footerLink?: { text: string; href: string },
Comment on lines +57 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The devex of this API isn't great, but I don't have good ideas to improve it either right now. We can revisit this in the future.

): void;

/**
Expand Down
Loading
Loading