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

feat: add ability to accept dialogs (#138) #164

Merged
merged 9 commits into from
Nov 2, 2022
Merged
6 changes: 6 additions & 0 deletions src/metamask/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Browser, Page } from "puppeteer";

import { Dappeteer } from "..";

import { acceptDialog } from "../snap/acceptDialog";
import { rejectDialog } from "../snap/rejectDialog";
import { addNetwork } from "./addNetwork";
import { addToken } from "./addToken";
import { approve } from "./approve";
Expand Down Expand Up @@ -49,6 +51,10 @@ export const getMetaMask = (page: Page): Promise<Dappeteer> => {
deleteAccount: deleteAccount(page),
deleteNetwork: deleteNetwork(page),
},
snaps: {
acceptDialog: acceptDialog(page),
mpetrunic marked this conversation as resolved.
Show resolved Hide resolved
rejectDialog: rejectDialog(page),
},
page,
})
);
Expand Down
6 changes: 6 additions & 0 deletions src/snap/acceptDialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Page } from "puppeteer";
import { clickOnButton } from "../helpers";

export const acceptDialog = (page: Page) => async (): Promise<void> => {
await clickOnButton(page, "Approve");
};
6 changes: 6 additions & 0 deletions src/snap/rejectDialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Page } from "puppeteer";
import { clickOnButton } from "../helpers";

export const rejectDialog = (page: Page) => async (): Promise<void> => {
await clickOnButton(page, "Reject");
};
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,9 @@ export type Dappeteer = {
deleteAccount: (accountNumber: number) => Promise<void>;
deleteNetwork: (name: string) => Promise<void>;
};
snaps: {
BeroBurny marked this conversation as resolved.
Show resolved Hide resolved
acceptDialog: () => Promise<void>;
rejectDialog: () => Promise<void>;
};
page: puppeteer.Page;
};
64 changes: 41 additions & 23 deletions test/flask/snaps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect } from "chai";
import { Page } from "puppeteer";
import * as dappeteer from "../../src";
import { installSnap, invokeSnap } from "../../src/snap";
import { TestContext } from "../constant";
import { Snaps } from "../deploy";
import { toUrl } from "../utils/utils";
import { clickOnButton } from "../../src/helpers";

function getSnapIdByName(testContext: TestContext, snapName: Snaps): string {
return `local:${toUrl(testContext.snapServers[snapName].address())}`;
Expand All @@ -13,6 +13,27 @@ function getSnapIdByName(testContext: TestContext, snapName: Snaps): string {
describe("snaps", function () {
let metamask: dappeteer.Dappeteer;

async function installPermissionSnap(
BeroBurny marked this conversation as resolved.
Show resolved Hide resolved
testContext: TestContext,
snapAddress: string
): Promise<void> {
await installSnap(
metamask.page,
getSnapIdByName(testContext, Snaps.PERMISSIONS_SNAP),
{
hasPermissions: true,
hasKeyPermissions: false,
},
snapAddress
);
}

async function getTestPage(snapAddress: string): Promise<Page> {
const testPage = await metamask.page.browser().newPage();
await testPage.goto(snapAddress);
return testPage;
}

before(function (this: TestContext) {
metamask = this.metamask;
});
Expand All @@ -32,14 +53,7 @@ describe("snaps", function () {
});

it("should install permissions snap from npm", async function (this: TestContext) {
await installSnap(
metamask.page,
getSnapIdByName(this, Snaps.PERMISSIONS_SNAP),
{
hasPermissions: true,
hasKeyPermissions: false,
}
);
await installPermissionSnap(this, "https://google.com");
});

it("should install keys snap from npm", async function (this: TestContext) {
Expand All @@ -49,21 +63,27 @@ describe("snaps", function () {
});
});

it("should invoke provided snap method", async function (this: TestContext) {
it("should invoke provided snap method and ACCEPT the dialog", async function (this: TestContext) {
const snapAddress = "http://localhost:8545";
await installSnap(
metamask.page,
await installPermissionSnap(this, snapAddress);
const testPage = await getTestPage(snapAddress);
const invokeAction = invokeSnap(
testPage,
getSnapIdByName(this, Snaps.PERMISSIONS_SNAP),
{
hasPermissions: true,
hasKeyPermissions: false,
},
snapAddress
"hello",
{ version: "latest" }
);

const testPage = await metamask.page.browser().newPage();
await testPage.goto(snapAddress);
await metamask.page.bringToFront();
BeroBurny marked this conversation as resolved.
Show resolved Hide resolved
await metamask.snaps.acceptDialog();

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

it("should invoke provided snap method and REJECT the dialog", async function (this: TestContext) {
const snapAddress = "http://localhost:8080";
await installPermissionSnap(this, snapAddress);
const testPage = await getTestPage(snapAddress);
const invokeAction = invokeSnap(
testPage,
getSnapIdByName(this, Snaps.PERMISSIONS_SNAP),
Expand All @@ -72,10 +92,8 @@ describe("snaps", function () {
);

await metamask.page.bringToFront();
await metamask.page.reload();
await metamask.snaps.rejectDialog();

await clickOnButton(metamask.page, "Approve");

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