-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created election-factory test script * Removed peer independencies * removed peer dependencies * Changes to merge PR * Removing ethers as deps --------- Co-authored-by: mickeymond <[email protected]>
- Loading branch information
1 parent
fd4dac3
commit 20fe18b
Showing
4 changed files
with
124 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ ignition/deployments/chain-31337 | |
|
||
# Others | ||
package-lock.json | ||
yarn.lock | ||
yarn.lock | ||
pnpm-lock.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; | ||
import { expect } from "chai"; | ||
import hre from "hardhat"; | ||
|
||
|
||
describe("ElectionFactory Contract", function () { | ||
async function deployElectionFactoryFixture() { | ||
// Get the ContractFactory and Signers here. | ||
const [owner, addr1] = await hre.ethers.getSigners(); | ||
|
||
const ElectionFactory = await hre.ethers.getContractFactory("ElectionFactory"); | ||
const electionFactory = await ElectionFactory.deploy(); | ||
|
||
// await electionFactory.deployed(); | ||
|
||
// Return the contract and accounts to be used in the tests | ||
return { electionFactory, owner, addr1 }; | ||
} | ||
|
||
it("Should deploy the contract and set the owner correctly", async function () { | ||
const { electionFactory, owner } = await loadFixture(deployElectionFactoryFixture); | ||
|
||
expect(await electionFactory.getOwner()).to.equal(owner.address); | ||
}); | ||
|
||
it("Should create a new election", async function () { | ||
const { electionFactory, addr1 } = await loadFixture(deployElectionFactoryFixture); | ||
|
||
const newElection = await electionFactory.createElection( | ||
"Election Title", | ||
"Election Description", | ||
true, | ||
1234567890, | ||
1234567899 | ||
); | ||
|
||
// Wait for the transaction to be mined | ||
await newElection.wait(); | ||
|
||
const elections = await electionFactory.getElections(); | ||
expect(elections.length).to.equal(1); | ||
}); | ||
|
||
it("Should emit an event when a new election is created", async function () { | ||
const { electionFactory } = await loadFixture(deployElectionFactoryFixture); | ||
|
||
await expect(electionFactory.createElection( | ||
"Election Title", | ||
"Election Description", | ||
true, | ||
1234567890, | ||
1234567899 | ||
)).to.emit(electionFactory, "ElectionCreated"); | ||
}); | ||
|
||
it("Should only allow the owner to delete an election", async function () { | ||
const { electionFactory, owner, addr1 } = await loadFixture(deployElectionFactoryFixture); | ||
|
||
// Create an election first | ||
const newElection = await electionFactory.createElection( | ||
"Election Title", | ||
"Election Description", | ||
true, | ||
1234567890, | ||
1234567899 | ||
); | ||
|
||
await newElection.wait(); | ||
|
||
// Attempt to delete the election with a non-owner account | ||
await expect(electionFactory.connect(addr1).deleteElection(0)).to.be.rejectedWith( | ||
"You are not the owner" | ||
); | ||
|
||
// Delete the election with the owner account | ||
await electionFactory.connect(owner).deleteElection(0); | ||
|
||
const elections = await electionFactory.getElections(); | ||
expect(elections[0]).to.equal(ethers.ZeroAddress); // Use ethers.constants.AddressZero to check the zero address | ||
}); | ||
|
||
it("Should return the correct total number of elections", async function () { | ||
const { electionFactory } = await loadFixture(deployElectionFactoryFixture); | ||
|
||
// Check initial total elections count | ||
expect(await electionFactory.getTotalElections()).to.equal(0); | ||
|
||
// Create an election | ||
const newElection = await electionFactory.createElection( | ||
"Election Title", | ||
"Election Description", | ||
true, | ||
1234567890, | ||
1234567899 | ||
); | ||
|
||
await newElection.wait(); | ||
|
||
// Check total elections count after creating one | ||
expect(await electionFactory.getTotalElections()).to.equal(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ next-env.d.ts | |
|
||
package-lock.json | ||
yarn.lock | ||
pnpm-lock.yaml |