Skip to content

Commit

Permalink
Create DAppContract.test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Nov 20, 2024
1 parent 3fe210f commit 0b544b8
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions smart-contracts/test/DAppContract.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const DAppContract = artifacts.require("DAppContract");

contract("DAppContract", (accounts) => {
let dAppContract;
const [owner, user1] = accounts;

beforeEach(async () => {
dAppContract = await DAppContract.new("My DApp");
});

it("should set the correct name", async () => {
const name = await dAppContract.name();
assert.equal(name, "My DApp", "The name is not set correctly");
});

it("should allow users to deposit funds", async () => {
const depositAmount = web3.utils.toWei("1", "ether");
await dAppContract.deposit({ from: user1, value: depositAmount });

const balance = await dAppContract.balances(user1);
assert.equal(balance.toString(), depositAmount, "The balance is not updated correctly");
});

it("should allow users to withdraw funds", async () => {
const depositAmount = web3.utils.toWei("1", "ether");
await dAppContract.deposit({ from: user1, value: depositAmount });

await dAppContract.withdraw(depositAmount, { from: user1 });
const balance = await dAppContract.balances(user1);
assert.equal(balance.toString(), "0", "The balance should be zero after withdrawal");
});

it("should not allow withdrawal of more than balance", async () => {
try {
await dAppContract.withdraw(web3.utils.toWei("1", "ether"), { from: user1 });
assert.fail("Withdrawal should have failed");
} catch (error) {
assert(error.message.includes("Insufficient balance"), "Expected 'Insufficient balance' error");
}
});
});

0 comments on commit 0b544b8

Please sign in to comment.