Skip to content

Commit

Permalink
payment test done
Browse files Browse the repository at this point in the history
  • Loading branch information
codypharm committed Sep 12, 2023
1 parent fac7951 commit 39e7145
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/Payment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract Payment {
*@param _amount The amount of eth deposited
*/
function deposit(uint _amount) external payable{
require(msg.value == _amount, "invalid deposit amount");
require(msg.value == _amount, "Invalid deposit amount");
//deposit to treasury
(bool success, ) = address(wallet).call{value: msg.value}("");
//record deposit
Expand Down
76 changes: 76 additions & 0 deletions test/Payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai";
import { ethers } from "hardhat";

describe("Payment", function () {
async function deployOneYearLockFixture() {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners();

const Payment = await ethers.getContractFactory("Payment");
const payment = await Payment.deploy();

return { payment, owner, otherAccount };
}

describe("Deposit", function () {
describe("Validations", function () {
it("Should revert with the right error ", async function () {
const { payment } = await loadFixture(deployOneYearLockFixture);
const depositAmount = ethers.parseUnits("1", "ether");
const expectedAmount = ethers.parseUnits("2", "ether");

await expect(
payment.deposit(expectedAmount, { value: depositAmount })
).to.be.revertedWith("Invalid deposit amount");
});
});

describe("Payment", function () {
it("Should deposit funds", async function () {
const { payment, owner } = await loadFixture(deployOneYearLockFixture);
const depositAmount = ethers.parseUnits("1", "ether");
const expectedAmount = ethers.parseUnits("1", "ether");
await payment.deposit(expectedAmount, { value: depositAmount });
const balance = await payment.depositRecord(owner);
expect(balance).to.equal(depositAmount);
});
});

describe("Events", function () {
it("Should emit an event on deposit", async function () {
const { payment, owner } = await loadFixture(deployOneYearLockFixture);

const depositAmount = ethers.parseUnits("1", "ether");
const expectedAmount = ethers.parseUnits("1", "ether");

await expect(payment.deposit(expectedAmount, { value: depositAmount }))
.to.emit(payment, "Deposit")
.withArgs(owner.address, depositAmount, true); // We accept any value as `when` arg
});
});
});

describe("Zero", function () {
describe("Removal", function () {
it("Should zero deposit record", async function () {
const { payment, owner } = await loadFixture(deployOneYearLockFixture);

await payment.zeroDepositRecord(owner);
const balance = await payment.depositRecord(owner);
expect(balance).to.equal(0);
});
});

describe("Events", function () {
it("Should emit an event on removal", async function () {
const { payment, owner } = await loadFixture(deployOneYearLockFixture);

await expect(payment.zeroDepositRecord(owner))
.to.emit(payment, "Erase")
.withArgs(owner.address);
});
});
});
});

0 comments on commit 39e7145

Please sign in to comment.