|
| 1 | +const { assert } = require('chai'); |
| 2 | +describe('Contract', function () { |
| 3 | + const charity = ethers.Wallet.createRandom().address; |
| 4 | + const donation = ethers.utils.parseEther("1"); |
| 5 | + let contract; |
| 6 | + let owner; |
| 7 | + let tipper; |
| 8 | + before(async () => { |
| 9 | + const Contract = await ethers.getContractFactory("Contract"); |
| 10 | + contract = await Contract.deploy(charity); |
| 11 | + await contract.deployed(); |
| 12 | + |
| 13 | + owner = ethers.provider.getSigner(0); |
| 14 | + await owner.sendTransaction({ to: contract.address, value: donation }); |
| 15 | + tipper = ethers.provider.getSigner(1); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should store the owner', async () => { |
| 19 | + const _owner = await contract.owner.call(); |
| 20 | + assert.equal(_owner, await owner.getAddress()); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should receive the initial donation', async () => { |
| 24 | + const balance = await ethers.provider.getBalance(contract.address); |
| 25 | + assert(balance.eq(donation), "expected the ether to be received"); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('after two .25 ether tips', () => { |
| 29 | + const tip = ethers.utils.parseEther("0.25"); |
| 30 | + let balanceBefore; |
| 31 | + before(async () => { |
| 32 | + balanceBefore = await ethers.provider.getBalance(await owner.getAddress()); |
| 33 | + await contract.connect(tipper).tip({ value: tip }); |
| 34 | + await contract.connect(tipper).tip({ value: tip }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should add .5 ether to the owners balance', async () => { |
| 38 | + const balanceAfter = await ethers.provider.getBalance(await owner.getAddress()); |
| 39 | + assert.equal(balanceAfter.sub(balanceBefore).toString(), tip.mul(2).toString()); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('after donating', () => { |
| 44 | + before(async () => { |
| 45 | + await contract.connect(tipper).donate(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should add the donations to the charity balance', async () => { |
| 49 | + const _donation = await ethers.provider.getBalance(charity); |
| 50 | + assert.equal(_donation.toString(), donation.toString()); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments