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

Merged
merged 25 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4a134df
update notification example snap and test-snaps
hmalik88 Dec 14, 2024
eb9a0aa
update struct
hmalik88 Dec 16, 2024
c2d9d06
update notification validator
hmalik88 Dec 16, 2024
0297ea9
add snaps jest support for expanded notifications, update example snap
hmalik88 Dec 17, 2024
f694dfe
Merge branch 'main' into hm/update-notification-example
hmalik88 Dec 17, 2024
2320d5c
update matchers & test
hmalik88 Dec 17, 2024
5038962
fix test
hmalik88 Dec 17, 2024
7b415bc
update manifest
hmalik88 Dec 17, 2024
2cb571f
fix matchers to also include param for content
hmalik88 Dec 18, 2024
b19d893
clean up notification matcher
hmalik88 Dec 19, 2024
18b7ec1
Merge branch 'main' into hm/update-notification-example
hmalik88 Dec 19, 2024
2e483dc
more fixes
hmalik88 Dec 23, 2024
692bb09
fix type
hmalik88 Dec 23, 2024
3251730
fix another type
hmalik88 Dec 23, 2024
da600f0
Merge branch 'main' into hm/update-notification-example
hmalik88 Dec 23, 2024
30a2a51
fix type once more
hmalik88 Dec 23, 2024
d6ecb1e
update formatting and types
hmalik88 Dec 23, 2024
8a4f74a
use correct struct
hmalik88 Dec 23, 2024
b5c8a6c
Merge branch 'main' into hm/update-notification-example
hmalik88 Jan 6, 2025
3314fcd
Merge branch 'main' into hm/update-notification-example
hmalik88 Jan 8, 2025
3c366a7
apply code review
hmalik88 Jan 8, 2025
e01fe8d
Merge branch 'main' into hm/update-notification-example
hmalik88 Jan 10, 2025
4886c48
update per comments
hmalik88 Jan 10, 2025
9ccf7b6
Merge branch 'main' into hm/update-notification-example
hmalik88 Jan 10, 2025
ca6b3dc
fix type
hmalik88 Jan 10, 2025
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": "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':
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
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>,
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
title?: string,
content?: JSXElement,
footerLink?: { text: string; href: string },
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
): void;

/**
Expand Down
Loading
Loading