Skip to content

Commit

Permalink
Add unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed May 19, 2016
1 parent 368c92d commit e88f4ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ $ npm install --save wait-transaction
## Usage

```js
import waitTransaction from 'wait-transaction'
const wait = waitTransaction(web3)
// pass a valid web3 instance that the providers set
const waitTransaction = require('wait-transaction')(web3)

it('should add funds sent by the seller to sellerFunds', () => {
const contract = MyContract.deployed()
return wait({ from: web3.eth.accounts[0], to: contract.address, value: 1000 })
.then(() => web3.eth.getBalance(contract.address))
.then(balance => assert.equal(1000, balance))
})
waitTransaction({ from: web3.eth.accounts[0], to: emptyAccount.address, value: 1000 })
.then(() => web3.eth.getBalance(emptyAccount.address).valueOf())
.then(balance => assert.equal(1000, balance))
```

## License
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"name": "wait-transaction",
"description": "A promisified web3.eth.sendTransaction that waits for confirmation ",
"scripts": {
"lint": "standard \"test/**/*.js\" \"src/**/*.js\"",
"lint": "standard \"test.js\" \"src/**/*.js\"",
"build": "babel src -d lib",
"watch": "chokidar \"test/**/*.js\" \"src/**/*.js\" -c \"npm run lint ; npm run build && npm run test\""
"test": "npm run lint && npm run testrunner",
"testrunner": "mocha --compilers js:babel-register",
"watch": "chokidar \"test.js\" \"src/**/*.js\" -c \"npm run lint ; npm run build && npm run testrunner\""
},
"main": "lib/index.js",
"private": true,
Expand All @@ -17,6 +19,7 @@
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-register": "^6.9.0",
"chai": "^3.5.0",
"chokidar": "^1.5.0",
"mocha": "^2.4.5",
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* global describe, it */

const { assert } = require('chai')
const Web3 = require('web3')
const web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
const waitTransaction = require('./lib')(web3)

/** Returns true if the difference between the two values is less than the given margin of error */
const within = (margin, a, b) => Math.abs(a - b) < margin

describe('wait-transaction', () => {
it('should send ether from one account to another', () => {
const [account1, account2] = web3.eth.accounts
const balance1 = +web3.eth.getBalance(account1).valueOf()
const balance2 = +web3.eth.getBalance(account2).valueOf()

return waitTransaction({ from: account1, to: account2, value: balance1 / 2 }).then(() => {
const newBalance1 = +web3.eth.getBalance(account1).valueOf()
const newBalance2 = +web3.eth.getBalance(account2).valueOf()
assert(within(balance1 / 1e12, newBalance1, balance1 / 2))
assert(within(balance2 / 1e12, newBalance2, balance2 + balance1 / 2))
})
})
})

0 comments on commit e88f4ba

Please sign in to comment.