That
parseFloat()
part was tricky
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract("People", async function(accounts){
let instance;
before(async function(){
instance = await People.deployed()
});
it("shouldnt create with age over 150 years", async function(){
await truffleAssert.fails(instance.createPerson("Bob", 200, 190, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT);
});
it("shouldnt create a person without payment", async function(){
await truffleAssert.fails(instance.createPerson("Bob", 50, 190, {value: 1000}), truffleAssert.ErrorType.REVERT);
});
it("it should set senior status correctly", async function(){
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
let result = await instance.getPerson();
assert(result.senior === true, "Senior level not set");
});
it("bobs age is correct", async function(){
let result = await instance.getPerson();
assert(result.age.toNumber() === 65, "Age not set correctly");
});
it("should not allow non owner delete perosn", async function(){
await instance.createPerson("Zala", 25, 180, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[2]}), truffleAssert.ErrorType.REVERT);
});
it("should allow owner to delete person", async function(){
let instance = await People.new();
await instance.createPerson("Zala", 25, 180, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(accounts[2], {from: accounts[0]});
});
it("can withdraw ballance", async function(){
let instance = await People.new();
await instance.createPerson("ZAN", 25, 170, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
});
it("the contract has balance", async function(){
let instance = await People.new();
await instance.createPerson("ZAN", 25, 170, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
let contBalance = parseFloat(await web3.eth.getBalance(accounts[0]));
assert(contBalance > 0, "YES");
});
it("contract balance 0 after withdraw", async function(){
let instance = await People.new();
let contOldBalance = parseFloat(await web3.eth.getBalance(accounts[0]));
await instance.createPerson("ZAN", 25, 170, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await instance.withdrawAll({from: accounts[0]});
let contNewBalance = parseFloat(await web3.eth.getBalance(accounts[0]));
assert(contOldBalance < contNewBalance, "YES");
});
});