From b76249013fd063fa0b8a5a710708785e1c09e648 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Thu, 31 Oct 2024 08:22:12 +0700 Subject: [PATCH] Create test_ERC20Token.js --- .../pi_network/tests/test_ERC20Token.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 blockchain_integration/pi_network/tests/test_ERC20Token.js diff --git a/blockchain_integration/pi_network/tests/test_ERC20Token.js b/blockchain_integration/pi_network/tests/test_ERC20Token.js new file mode 100644 index 000000000..a3e74de26 --- /dev/null +++ b/blockchain_integration/pi_network/tests/test_ERC20Token.js @@ -0,0 +1,44 @@ +// tests/test_ERC20Token.js +const { expect } = require("chai"); +const { ethers } = require("hardhat"); + +describe("ERC20Token", function () { + let Token; + let token; + let owner; + let addr1; + let addr2; + + beforeEach(async function () { + Token = await ethers.getContractFactory("ERC20Token"); + [owner, addr1, addr2] = await ethers.getSigners(); + token = await Token.deploy("Test Token", "TTK", 1000); + await token.deployed(); + }); + + it("Should have the correct name and symbol", async function () { + expect(await token.name()).to.equal("Test Token"); + expect(await token.symbol()).to.equal("TTK"); + }); + + it("Should assign the total supply to the owner", async function () { + const ownerBalance = await token.balanceOf(owner.address); + expect(await token.totalSupply()).to.equal(ownerBalance); + }); + + it("Should transfer tokens between accounts", async function () { + await token.transfer(addr1.address, 100); + const addr1Balance = await token.balanceOf(addr1.address); + expect(addr1Balance).to.equal(100); + + await token.connect(addr1).transfer(addr2.address, 50); + const addr2Balance = await token.balanceOf(addr2.address); + expect(addr2Balance).to.equal(50); + }); + + it("Should fail if sender doesn’t have enough tokens", async function () { + const initialOwnerBalance = await token.balanceOf(owner.address); + await expect(token.connect(addr1).transfer(owner.address, 1)).to.be.revertedWith("ERC20: transfer amount exceeds balance"); + expect(await token.balanceOf(owner.address)).to.equal(initialOwnerBalance); + }); +});