From 39e714567de6537967879b8cf10e2176485637db Mon Sep 17 00:00:00 2001 From: techyNonso Date: Tue, 12 Sep 2023 20:22:25 +0100 Subject: [PATCH] payment test done --- contracts/Payment.sol | 2 +- test/Payment.ts | 76 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/Payment.ts diff --git a/contracts/Payment.sol b/contracts/Payment.sol index 746ea74..24f276f 100644 --- a/contracts/Payment.sol +++ b/contracts/Payment.sol @@ -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 diff --git a/test/Payment.ts b/test/Payment.ts new file mode 100644 index 0000000..c71451c --- /dev/null +++ b/test/Payment.ts @@ -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); + }); + }); + }); +});