Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ETHBankContract): validate transactions #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions test/ETHBankContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,77 @@ describe("ETHBank Test Suite", function () {

describe("Post Deployment State Variables", async () => {
it("Should return state variables", async () => {
// extract loadFixture variables
// extract loadFixture variables
const { ETHBankContract, owner, addr1, addr2 } = await loadFixture(deployTokenFixture);
expect(await ETHBankContract.owner()).to.eq(owner.address)
expect(await ETHBankContract.ethBalances(owner.address)).to.eq(0)
expect(await ETHBankContract.owner()).to.eq(owner.address);
expect(await ETHBankContract.ethBalances(owner.address)).to.eq(0);
});
});

describe("Transactions", async () => {
it("Should deposit ETH", async () => {
// extract loadFixture variables
const { ETHBankContract, owner } = await loadFixture(deployTokenFixture);
await ETHBankContract.connect(owner).depositETH({ value: ethers.utils.parseEther("1") });
expect(await ETHBankContract.ethBalances(owner.address)).to.eq(ethers.utils.parseEther("1"));
});

it("Should withdraw ETH", async () => {
// extract loadFixture variables
const { ETHBankContract, owner, addr1 } = await loadFixture(deployTokenFixture);

// deposit ETH to contract
await ETHBankContract.connect(addr1).depositETH({ value: ethers.utils.parseEther("1") });

// withdraw ETH from contract
await ETHBankContract.connect(addr1).withdrawETH(ethers.utils.parseEther("0.5"));

// assert withdrawal transaction
expect(await ETHBankContract.ethBalances(addr1.address)).to.eq(ethers.utils.parseEther("0.5"));
});

it("Should withdraw all ETH", async () => {
// extract loadFixture variables
const { ETHBankContract, addr2 } = await loadFixture(deployTokenFixture);

// deposit ETH to contract
await ETHBankContract.connect(addr2).depositETH({ value: ethers.utils.parseEther("1") });

// withdraw all ETH from contract
await ETHBankContract.connect(addr2).withdrawAllETH();

// assert owner balance
expect(await ETHBankContract.ethBalances(addr2.address)).to.eq(0);
});

it("Should allow owner to withdraw all ETH", async () => {
// extract loadFixture variables
const { ETHBankContract, owner } = await loadFixture(deployTokenFixture);

// deposit ETH to contract
await ETHBankContract.connect(owner).depositETH({ value: ethers.utils.parseEther("1") });

// call emergency owner withdraw
await ETHBankContract.connect(owner).ownerOnlyWithdraw();

// assert owner balance
expect(await ETHBankContract.ethBalances(owner.address)).to.eq(ethers.utils.parseEther("1"));


// assert contract balance
expect(await ETHBankContract.getContractEthBalance()).to.eq(ethers.utils.parseEther("0"));
});

it("Should not allow non-owner to withdraw all ETH", async () => {
// extract loadFixture variables
const { ETHBankContract, owner, addr1 } = await loadFixture(deployTokenFixture);

// deposit ETH to contract
await ETHBankContract.connect(owner).depositETH({ value: ethers.utils.parseEther("1") });

// asssert only owner can call ownerOnlyWithdraw
await expect(ETHBankContract.connect(addr1).ownerOnlyWithdraw()).to.be.revertedWith(
"only owner can call emergency withdraw"
);
});
});
});