contracts/test/Contribution.js
Râu Cao 67add71a22
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
Add tests for Contribution#add()
2022-08-23 14:41:30 +01:00

107 lines
4.2 KiB
JavaScript

const { expect } = require("chai");
const { ethers, upgrades } = require("hardhat");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
let owner, addr1, addr2, addr3, addr4, addr5, addr6, addr7;
let Contribution, Contributor;
describe("Contribution contract", async function () {
before(async function () {
[owner, addr1, addr2, addr3, addr4, addr5, addr6, addr7] = await ethers.getSigners();
let accounts = [owner, addr1, addr2, addr3, addr4, addr5, addr6, addr7];
const contributorFactory = await ethers.getContractFactory("Contributor");
Contributor = await upgrades.deployProxy(contributorFactory);
for (const account of accounts) {
await Contributor.addContributor(account.address, "0x99b8afd7b266e19990924a8be9099e81054b70c36b20937228a77a5cf75723b8", 18, 32);
}
});
describe("initialize()", function () {
before(async function () {
// const [owner] = await ethers.getSigners();
const contributionFactory = await ethers.getContractFactory("Contribution");
Contribution = await upgrades.deployProxy(contributionFactory, [40321]);
});
it("sets the veto confirmation period", async function () {
expect(await Contribution.blocksToWait()).to.equal(40321);
});
it("sets the data migration flag", async function () {
expect(await Contribution.migrationDone()).to.equal(false);
});
it("sets the deployer address", async function () {
expect(await Contribution.deployer()).to.equal(owner.address);
expect(await Contribution.deployer()).to.not.equal(addr1.address);
});
});
describe("finishMigration()", function () {
before(async function () {
const contributionFactory = await ethers.getContractFactory("Contribution");
Contribution = await upgrades.deployProxy(contributionFactory, [40321]);
await Contribution.setContributorContract(Contributor.address).then(res => res.wait())
});
it("does not allow random accounts to mark the migration as finished", async function () {
await expect(Contribution.connect(addr1).finishMigration()).to.be.revertedWith("Deployer only");
expect(await Contribution.migrationDone()).to.equal(false);
});
it("allows the deployer to mark the migration as finished", async function () {
await Contribution.finishMigration();
expect(await Contribution.migrationDone()).to.equal(true);
});
});
describe("add()", function () {
before(async function () {
const contributionFactory = await ethers.getContractFactory("Contribution");
Contribution = await upgrades.deployProxy(contributionFactory, [40321]);
await Contribution.setContributorContract(Contributor.address).then(res => res.wait())
});
it("does not allow non-contributors to add a contribution", async function () {
await expect(Contribution.connect(addr7).add(
500, 1,
"0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f",
18, 32
)).to.be.revertedWith("requires kredits or core status");
expect(await Contribution.contributionsCount()).to.equal(0);
});
it("allows core contributors to add a contribution", async function () {
await Contribution.connect(addr2).add(
2001, 1,
"0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f",
18, 32
);
expect(await Contribution.contributionsCount()).to.equal(1);
});
it("allows contributors to add a contribution", async function () {
// Issue some kredits for new contributor #8 (addr7)
await Contribution.connect(addr1).add(
5000, 8,
"0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f",
18, 32
);
await Contribution.connect(addr7).add(
1500, 1,
"0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f",
18, 32
);
expect(await Contribution.contributionsCount()).to.equal(3);
});
it("emits a ContributionAdded event", async function () {
await expect(Contribution.connect(addr1).add(
2001, 1,
"0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f",
18, 32
)).to.emit(Contribution, "ContributionAdded").withArgs(anyValue, 1, 2001);
});
});
});