-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from unchain-tech/feature/refactor-unit-test
Feature/refactor unit test
- Loading branch information
Showing
2 changed files
with
101 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,109 @@ | ||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
const hre = require('hardhat'); | ||
const { expect } = require('chai'); | ||
const { ethers } = require('hardhat'); | ||
|
||
describe('Wave Contract', function () { | ||
it('test if wave and token are sent', async function () { | ||
const waveContractFactory = await hre.ethers.getContractFactory( | ||
'WavePortal', | ||
); | ||
/* | ||
* デプロイする際0.1ETHをコントラクトに提供する | ||
*/ | ||
const waveContract = await waveContractFactory.deploy({ | ||
describe('WavePortal', function () { | ||
// すべてのテストで同じセットアップを再利用するためにフィクスチャーを定義します。 | ||
async function deployProjectFixture() { | ||
const wavePortalFactory = await ethers.getContractFactory('WavePortal'); | ||
|
||
// コントラクトは、デフォルトで最初の署名者/アカウント(ここではuser1)を使用してデプロイされます。 | ||
const [user1, user2] = await ethers.getSigners(); | ||
|
||
const wavePortal = await wavePortalFactory.deploy({ | ||
value: hre.ethers.utils.parseEther('0.1'), | ||
}); | ||
await waveContract.deployed(); | ||
/* | ||
* コントラクトの残高を取得(0.1ETH) | ||
*/ | ||
const contractBalanceBefore = hre.ethers.utils.formatEther( | ||
await hre.ethers.provider.getBalance(waveContract.address), | ||
|
||
await wavePortal.deployed(); | ||
|
||
// 現在のコントラクトの残高を取得します。 | ||
const wavePortalBalance = hre.ethers.utils.formatEther( | ||
await hre.ethers.provider.getBalance(wavePortal.address), | ||
); | ||
|
||
/* | ||
* 2回 waves を送るシミュレーションを行う | ||
*/ | ||
const waveTxn = await waveContract.wave('This is wave #1'); | ||
await waveTxn.wait(); | ||
// waveを2回実行する関数を定義します。 | ||
const sendTwoWaves = async () => { | ||
// user1, user2がそれぞれwaveを送ります。 | ||
await wavePortal.connect(user1).wave('This is wave #1'); | ||
await wavePortal.connect(user2).wave('This is wave #2'); | ||
}; | ||
|
||
const waveTxn2 = await waveContract.wave('This is wave #2'); | ||
await waveTxn2.wait(); | ||
return { wavePortal, wavePortalBalance, sendTwoWaves, user1, user2 }; | ||
} | ||
|
||
/* | ||
* コントラクトの残高を取得し、Waveを取得した後の結果を出力 | ||
*/ | ||
const contractBalanceAfter = hre.ethers.utils.formatEther( | ||
await hre.ethers.provider.getBalance(waveContract.address), | ||
); | ||
// テストケース | ||
describe('getTotalWaves', function () { | ||
it('should return total waves', async function () { | ||
const { wavePortal, sendTwoWaves } = await loadFixture( | ||
deployProjectFixture, | ||
); | ||
|
||
await sendTwoWaves(); | ||
|
||
const totalWaves = await wavePortal.getTotalWaves(); | ||
|
||
expect(totalWaves).to.equal(2); | ||
}); | ||
}); | ||
|
||
describe('getAllWaves', function () { | ||
it('should return all waves', async function () { | ||
const { wavePortal, sendTwoWaves, user1, user2 } = await loadFixture( | ||
deployProjectFixture, | ||
); | ||
|
||
await sendTwoWaves(); | ||
|
||
const allWaves = await wavePortal.getAllWaves(); | ||
|
||
expect(allWaves[0].waver).to.equal(user1.address); | ||
expect(allWaves[0].message).to.equal('This is wave #1'); | ||
expect(allWaves[1].waver).to.equal(user2.address); | ||
expect(allWaves[1].message).to.equal('This is wave #2'); | ||
}); | ||
}); | ||
|
||
describe('wave', function () { | ||
context('when user waved', function () { | ||
it('should send tokens at random.', async function () { | ||
const { wavePortal, wavePortalBalance, sendTwoWaves } = | ||
await loadFixture(deployProjectFixture); | ||
|
||
await sendTwoWaves(); | ||
|
||
// wave後のコントラクトの残高を取得します。 | ||
const wavePortalBalanceAfter = hre.ethers.utils.formatEther( | ||
await hre.ethers.provider.getBalance(wavePortal.address), | ||
); | ||
|
||
// 勝利した回数に応じてコントラクトから出ていくトークンを計算します。 | ||
const allWaves = await wavePortal.getAllWaves(); | ||
let cost = 0; | ||
for (let i = 0; i < allWaves.length; i++) { | ||
if (allWaves[i].seed <= 50) { | ||
cost += 0.0001; | ||
} | ||
} | ||
|
||
// コントラクトのトークン残高がwave時の勝負による減少に連動しているかテストします。 | ||
expect(parseFloat(wavePortalBalanceAfter)).to.equal( | ||
wavePortalBalance - cost, | ||
); | ||
}); | ||
}); | ||
context( | ||
'when user1 tried to resubmit without waiting 15 mitutes', | ||
function () { | ||
it('reverts', async function () { | ||
const { wavePortal, user1 } = await loadFixture(deployProjectFixture); | ||
|
||
/* | ||
*勝利した回数に応じてコントラクトから出ていくトークンを計算 | ||
*/ | ||
const allWaves = await waveContract.getAllWaves(); | ||
let cost = 0; | ||
for (let i = 0; i < allWaves.length; i++) { | ||
if (allWaves[i].seed <= 50) { | ||
cost += 0.0001; | ||
} | ||
} | ||
|
||
/* | ||
*メッセージの送信をテスト | ||
*/ | ||
expect(allWaves[0].message).to.equal('This is wave #1'); | ||
expect(allWaves[1].message).to.equal('This is wave #2'); | ||
|
||
/* | ||
*コントラクトのトークン残高がwave時の勝負による減少に連動しているかテスト | ||
*/ | ||
expect(parseFloat(contractBalanceAfter)).to.equal( | ||
contractBalanceBefore - cost, | ||
await wavePortal.connect(user1).wave('This is wave #1'); | ||
await expect( | ||
wavePortal.connect(user1).wave('This is wave #2'), | ||
).to.be.revertedWith('Wait 15m'); | ||
}); | ||
}, | ||
); | ||
}); | ||
}); |