This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
contract.spec.ts
80 lines (65 loc) · 2.12 KB
/
contract.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { expect } from "chai";
import Web3 from "web3";
import { Contract } from "web3-eth-contract";
import { Dappeteer, DappeteerPage } from "../src";
import { ACCOUNT_ADDRESS, EXAMPLE_WEBSITE, TestContext } from "./constant";
import { ContractInfo } from "./contract/contractInfo";
import { TestContract } from "./deploy";
import { requestAccounts, sendTx } from "./testPageFunctions";
import { isUserDataTest } from "./utils/utils";
describe("contract interactions", function () {
let contract: TestContract;
let testPage: DappeteerPage;
let metamask: Dappeteer;
before(async function (this: TestContext) {
if (isUserDataTest()) {
this.skip();
}
testPage = await this.browser.newPage();
await testPage.goto(EXAMPLE_WEBSITE, { waitUntil: "networkidle" });
metamask = this.metaMask;
contract = this.contract;
try {
const connectionPromise = testPage.evaluate(requestAccounts);
await metamask.approve();
await connectionPromise;
} catch (e) {
//ignored
}
await metamask.switchAccount(1);
await metamask.switchNetwork("localhost");
});
after(async function (this: TestContext) {
if (testPage) await testPage.close();
});
it("should have increased count", async () => {
const web3Instance = new Web3();
const counterContract: TestContract = new Contract(
ContractInfo.abi,
web3Instance
);
const contractData = counterContract.methods.increase().encodeABI();
const txToSend = {
from: ACCOUNT_ADDRESS,
to: ContractInfo.address,
data: contractData,
};
const increasePromise = testPage.evaluate(sendTx, {
tx: txToSend,
});
const counterBefore = await getCounterNumber(contract);
await metamask.confirmTransaction();
const tx = await increasePromise;
expect(tx).to.not.be.undefined;
const counterAfter = await getCounterNumber(contract);
expect(counterAfter).to.be.equal(counterBefore + 1);
});
});
function getCounterNumber(contract: TestContract): Promise<number> {
return contract.methods
.count()
.call()
.then((res) => {
return Number(res);
});
}