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

Commit

Permalink
Cleanup class based emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lykhoyda committed Nov 25, 2022
1 parent f5ac55e commit e8cc963
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 275 deletions.
4 changes: 0 additions & 4 deletions src/metamask/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { DappeteerPage } from "../page";
import { acceptDialog } from "../snap/acceptDialog";
import { rejectDialog } from "../snap/rejectDialog";
import { getAllNotifications, installSnap, invokeSnap } from "../snap";
import { invokeNotification } from "../snap/invokeNotification";
import { waitForNotification } from "../snap/waitForNotification";
import { addNetwork } from "./addNetwork";
import { addToken } from "./addToken";
import { approve } from "./approve";
Expand Down Expand Up @@ -58,8 +56,6 @@ export const getMetaMask = (page: DappeteerPage): Promise<Dappeteer> => {
},
snaps: {
invokeSnap,
waitForNotification,
invokeNotification: invokeNotification(page),
getAllNotifications: getAllNotifications(page),
acceptDialog: acceptDialog(page),
rejectDialog: rejectDialog(page),
Expand Down
33 changes: 14 additions & 19 deletions src/snap/NotificationsEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
import { StrictEventEmitter } from "strict-event-emitter";
// eslint-disable-next-line @typescript-eslint/no-require-imports
import pEvent = require("p-event");
import { DappeteerPage } from "../page";
import * as dappeteer from "../../src";
import { clickOnElement, openProfileDropdown } from "../helpers";
import { NotificationItem, NotificationList } from "./types";
// eslint-disable-next-line @typescript-eslint/no-require-imports
import pEvent = require("p-event");

interface EventsMap {
newNotification: (notification: NotificationItem) => void;
notification: (notification: NotificationItem) => void;
}

class NotificationsEmitter extends StrictEventEmitter<EventsMap> {
private notifications: NotificationList = [];
private notificationsTab: DappeteerPage;
private readonly emitter: StrictEventEmitter<EventsMap>;
private readonly page: DappeteerPage;

constructor(protected metamask: dappeteer.Dappeteer) {
constructor(
protected metamask: dappeteer.Dappeteer,
private notificationTimeout: number = 30000
) {
super();
this.page = metamask.page;
this.emitter = new StrictEventEmitter<EventsMap>();
this.configureEmitterListener();
}

private configureEmitterListener(): void {
this.emitter.on("newNotification", (notification: NotificationItem) => {
this.notifications.push(notification);
});
}

private async exposeEmitNotificationToWindow(): Promise<void> {
await this.notificationsTab.exposeFunction(
"emitNotification",
(notification: NotificationItem) => {
this.emitter.emit("newNotification", notification);
this.emit("notification", notification);
}
);
}
Expand All @@ -46,7 +39,9 @@ class NotificationsEmitter extends StrictEventEmitter<EventsMap> {
const newPage = await this.page.browser().newPage();
await newPage.goto(this.page.url());

await newPage.waitForSelector(".notifications__container");
await newPage.waitForSelector(".notifications__container", {
timeout: this.notificationTimeout,
});
this.notificationsTab = newPage;
}

Expand Down Expand Up @@ -78,11 +73,11 @@ class NotificationsEmitter extends StrictEventEmitter<EventsMap> {
}

public waitForNotification(): pEvent.CancelablePromise<NotificationItem> {
return pEvent<any, NotificationItem>(this.emitter, "newNotification");
return pEvent<any, NotificationItem>(this, "notification");
}

public getAllNotifications(): NotificationList {
return this.notifications;
public async getAllNotifications(): Promise<NotificationList> {
return await this.metamask.snaps.getAllNotifications();
}
}

Expand Down
48 changes: 0 additions & 48 deletions src/snap/invokeNotification.ts

This file was deleted.

46 changes: 0 additions & 46 deletions src/snap/notificationEmitter.ts

This file was deleted.

28 changes: 0 additions & 28 deletions src/snap/waitForNotification.ts

This file was deleted.

16 changes: 1 addition & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,9 @@ export type Dappeteer = {
page: DappeteerPage;
snaps: {
/**
* Returns all notifications in Metamask notifications page
* Returns all notifications in Metamask notifications page and go back to main page
*/
waitForNotification: (page: DappeteerPage) => Promise<NotificationItem>;
getAllNotifications: () => Promise<NotificationList>;
/** Invoke notification snap method. The function will open a notification
page, invoke the snap, and it will wait until notification is rendered
in the DOM. Can be used in pair with getAllNotifications method f.e:
await metamask.snaps.invokeNotification(page, "snapid", "method_name")
const notifications = await metamask.snaps.getAllNotifications()
expect(notifications[0].message).to.equal("Notification message")
*/
invokeNotification: <Params extends Serializable = Serializable>(
page: DappeteerPage,
snapId: string,
method: string,
params?: Params
) => Promise<NotificationItem>;
/**
* Invoke Metamask snap method. Function will throw if there is an error while invoking snap.
* Use generic params to override result and parameter types.
Expand Down
Loading

0 comments on commit e8cc963

Please sign in to comment.