Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat: added notification snap to methods-snap #137 #166

Merged
merged 8 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/metamask/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dappeteer } from "..";

import { acceptDialog } from "../snap/acceptDialog";
import { rejectDialog } from "../snap/rejectDialog";
import { installSnap, invokeSnap } from "../snap";
import { getAllNotifications, installSnap, invokeSnap } from "../snap";
import { addNetwork } from "./addNetwork";
import { addToken } from "./addToken";
import { approve } from "./approve";
Expand Down Expand Up @@ -53,6 +53,7 @@ export const getMetaMask = (page: Page): Promise<Dappeteer> => {
deleteNetwork: deleteNetwork(page),
},
snaps: {
getAllNotifications: getAllNotifications(page),
acceptDialog: acceptDialog(page),
rejectDialog: rejectDialog(page),
invokeSnap,
Expand Down
17 changes: 17 additions & 0 deletions src/snap/getAllNotifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Page } from "puppeteer";
import { clickOnElement, openProfileDropdown } from "../helpers";
import { NotificationList } from "./types";

export const getAllNotifications =
(page: Page) => async (): Promise<NotificationList> => {
await page.bringToFront();
await openProfileDropdown(page);
await clickOnElement(page, "Notifications");
await page.waitForTimeout(300);
mpetrunic marked this conversation as resolved.
Show resolved Hide resolved

return await page.$$eval(
Lykhoyda marked this conversation as resolved.
Show resolved Hide resolved
".notifications__item__details__message",
(elements) =>
elements.map((element) => ({ message: element.textContent }))
);
};
1 change: 1 addition & 0 deletions src/snap/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { flaskOnly } from "./utils";
export { installSnap } from "./install";
export { invokeSnap } from "./invokeSnap";
export { getAllNotifications } from "./getAllNotifications";
2 changes: 2 additions & 0 deletions src/snap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export interface InstallSnapResult {
};
};
}

export type NotificationList = { message: string }[];
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Page, Serializable } from "puppeteer";
import { Path } from "./setup/metaMaskDownloader";

import { InstallStep } from "./snap/install";
import { InstallSnapResult } from "./snap/types";
import { InstallSnapResult, NotificationList } from "./snap/types";
import { RECOMMENDED_METAMASK_VERSION } from "./index";

declare global {
Expand Down Expand Up @@ -80,6 +80,7 @@ export type Dappeteer = {
deleteNetwork: (name: string) => Promise<void>;
};
snaps: {
getAllNotifications: () => Promise<NotificationList>;
invokeSnap: <R = unknown, P extends Serializable = Serializable>(
page: Page,
snapId: string,
Expand Down
2 changes: 1 addition & 1 deletion test/flask/methods-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "An example Snap written in TypeScript.",
"proposedName": "Methods Snap\n",
"source": {
"shasum": "BCwqoDyQltaL0dh0IrzN1A66D1jPMLAseJOHmqI1faU=",
"shasum": "lHBPubadQ9IHZuFZkp77Vp7lrnZFW0J0OcGZvrn3NHc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
20 changes: 20 additions & 0 deletions test/flask/methods-snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
},
],
});
case "notify_inApp":
return wallet.request({
method: "snap_notify",
params: [
{
type: "inApp",
message: `Hello, in App notification`,
},
],
});
case "notify_inApp_update":
return wallet.request({
method: "snap_notify",
params: [
{
type: "inApp",
message: `Update notification`,
},
],
});
default:
throw new Error("Method not found.");
}
Expand Down
21 changes: 21 additions & 0 deletions test/flask/snaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,26 @@ describe("snaps", function () {

expect(await invokeAction).to.equal(false);
});

it("should invoke IN APP NOTIFICATIONS and check for a text", async function (this: TestContext) {
await metamask.snaps.invokeSnap(
testPage,
getSnapIdByName(this, Snaps.METHODS_SNAP),
"notify_inApp"
);
// Metamask doesn't allow to invoke two notifications in a row,
// so some delay should persist before calling the next notification
await metamask.page.waitForTimeout(5000);
await metamask.snaps.invokeSnap(
testPage,
getSnapIdByName(this, Snaps.METHODS_SNAP),
"notify_inApp_update"
);

const notifications = await metamask.snaps.getAllNotifications();

expect(notifications[0].message).to.equal("Update notification");
expect(notifications[1].message).to.equal("Hello, in App notification");
});
});
});