Browse Source

Refactor time manipulation in TokenTimelock tests

Jakub Wojciechowski 8 years ago
parent
commit
99e0f5b5cb
2 changed files with 9 additions and 8 deletions
  1. 6 7
      test/TokenTimelock.js
  2. 3 1
      test/helpers/increaseTime.js

+ 6 - 7
test/TokenTimelock.js

@@ -5,10 +5,9 @@ require('chai')
   .use(require('chai-bignumber')(BigNumber))
   .should()
 
-import moment from 'moment'
 
 import latestTime from './helpers/latestTime'
-import increaseTime from './helpers/increaseTime'
+import {increaseTimeTo, duration} from './helpers/increaseTime'
 
 const MintableToken = artifacts.require('MintableToken')
 const TokenTimelock = artifacts.require('TokenTimelock')
@@ -19,7 +18,7 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) {
 
   beforeEach(async function () {
     this.token = await MintableToken.new({from: owner})
-    this.releaseTime = latestTime().add(1, 'year').unix()
+    this.releaseTime = latestTime().unix() + duration.years(1)
     this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime)
     await this.token.mint(this.timelock.address, amount, {from: owner})
   })
@@ -29,26 +28,26 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) {
   })
 
   it('cannot be released just before time limit', async function () {
-    await increaseTime(moment.duration(0.99, 'year').asSeconds())
+    await increaseTimeTo(this.releaseTime - duration.seconds(3))
     await this.timelock.release().should.be.rejected
   })
 
   it('can be released just after limit', async function () {
-    await increaseTime(moment.duration(1.01, 'year').asSeconds())
+    await increaseTimeTo(this.releaseTime + duration.seconds(1))
     await this.timelock.release().should.be.fulfilled
     const balance = await this.token.balanceOf(beneficiary)
     balance.should.be.bignumber.equal(amount)
   })
 
   it('can be released after time limit', async function () {
-    await increaseTime(moment.duration(2, 'year').asSeconds())
+    await increaseTimeTo(this.releaseTime + duration.years(1))
     await this.timelock.release().should.be.fulfilled
     const balance = await this.token.balanceOf(beneficiary)
     balance.should.be.bignumber.equal(amount)
   })
 
   it('cannot be released twice', async function () {
-    await increaseTime(moment.duration(2, 'year').asSeconds())
+    await increaseTimeTo(this.releaseTime + duration.years(1))
     await this.timelock.release().should.be.fulfilled
     await this.timelock.release().should.be.rejected
     const balance = await this.token.balanceOf(beneficiary)

+ 3 - 1
test/helpers/increaseTime.js

@@ -43,5 +43,7 @@ export const duration = {
   minutes: function(val) { return val * this.seconds(60) },
   hours:   function(val) { return val * this.minutes(60) },
   days:    function(val) { return val * this.hours(24) },
-  weeks:   function(val) { return val * this.days(7) }
+  weeks:   function(val) { return val * this.days(7) },
+  months:  function(val) { return val * this.days(30)},
+  years:   function(val) { return val * this.days(365)} 
 };