Smartcontract-AdelToken/test/Counter.js

38 lines
1.1 KiB
JavaScript

const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Counter", function () {
it("Should emit the Increment event when calling the inc() function", async function () {
const counter = await ethers.deployContract("Counter");
// Di ethers v6, gunakan BigInt untuk literal angka
await expect(counter.inc())
.to.emit(counter, "Increment")
.withArgs(1n); // pakai 1n (BigInt)
});
it("The sum of the Increment events should match the current value", async function () {
const counter = await ethers.deployContract("Counter");
const deploymentBlockNumber = await ethers.provider.getBlockNumber();
// run a series of increments
for (let i = 1; i <= 10; i++) {
await counter.incBy(i);
}
const events = await counter.queryFilter(
counter.filters.Increment(),
deploymentBlockNumber,
"latest"
);
// check that the aggregated events match the current value
let total = 0n;
for (const event of events) {
total += event.args.by; // event.args.by udah BigInt di ethers v6
}
expect(await counter.x()).to.equal(total);
});
});