Skip to content

Commit

Permalink
[TESTS] TimeNode + Util (#149)
Browse files Browse the repository at this point in the history
* Add more unit tests

* Util tests
  • Loading branch information
josipbagaric committed Jul 23, 2018
1 parent 983eca1 commit 81ad563
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class W3Util {
});
}

public getBlock(blockNumber = 'latest'): Promise<IBlock> {
public getBlock(blockNumber: string | number = 'latest'): Promise<IBlock> {
return new Promise((resolve, reject) => {
this.web3.eth.getBlock(blockNumber, (err: any, block: IBlock) => {
if (!err) {
Expand Down
42 changes: 32 additions & 10 deletions test/unit/UnitTestTimeNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai';
import { expect, assert } from 'chai';
import { TimeNode, Config } from '../../src/index';
import { mockConfig } from '../helpers';

Expand All @@ -11,14 +11,36 @@ describe('TimeNode Unit Tests', () => {
expect(timenode).to.exist;
});

it('starts scanning', async () => {
const result: Boolean = await timenode.startScanning();
expect(result).to.be.true;
expect(timenode.scanner.scanning).to.be.true;
}).timeout(5000);
describe('startScanning()', () => {
it('returns true when started scanning', async () => {
assert.isTrue(await timenode.startScanning());
assert.isTrue(timenode.scanner.scanning);
}).timeout(5000);

it('stops scanning', async () => {
await timenode.stopScanning();
expect(timenode.scanner.scanning).to.be.false;
}).timeout(5000);
it('hard resets the scanner module when already scanning', async () => {
timenode.scanner.scanning = true;
assert.isTrue(await timenode.startScanning());
assert.isTrue(timenode.scanner.scanning);
}).timeout(5000);
});

describe('startClaiming()', () => {
it('returns false when stopped scanning', async () => {
assert.isFalse(await timenode.stopScanning());
}).timeout(5000);
});

describe('startClaiming()', () => {
it('returns true when started claiming', async () => {
assert.isTrue(timenode.startClaiming());
assert.isTrue(timenode.config.claiming);
});
});

describe('stopClaiming()', () => {
it('returns false when stopped claiming', async () => {
assert.isFalse(timenode.stopClaiming());
assert.isFalse(timenode.config.claiming);
});
});
});
57 changes: 57 additions & 0 deletions test/unit/UnitTestUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect, assert } from 'chai';
import { Config } from '../../src/index';
import { mockConfig } from '../helpers';
import W3Util from '../../src/Util';

describe('Util Unit Tests', () => {
const config: Config = mockConfig();
const util: W3Util = new W3Util(config.web3);

describe('estimateGas()', () => {
it('returns a number', async () => {
const gas = await util.estimateGas({});
assert.isTrue(typeof gas === 'number');
});
});

describe('networkGasPrice()', () => {
it('returns a number', async () => {
const networkGasPrice = await util.networkGasPrice();
assert.isTrue(networkGasPrice.greaterThan(0));
});
});

describe('getBlockNumber()', () => {
it('returns a block number', async () => {
const blockNum = await util.getBlockNumber();
assert.isTrue(typeof blockNum === 'number');
});
});

describe('getBlock()', () => {
it('returns a block', async () => {
const block = await util.getBlock();
assert.isTrue(block.number === (await util.getBlockNumber()));
});

it('returns a error when no block found for block number', async () => {
const blockNumber = 1000000000;
let err = '';

try {
await util.getBlock(blockNumber);
} catch (e) {
err = e;
}

expect(err).to.equal(`Returned block ${blockNumber} is null`);
});
});

describe('isWatchingEnabled()', () => {
it('returns true when watching', async () => {
const watching = await util.isWatchingEnabled();
assert.isTrue(watching);
});
});
});

0 comments on commit 81ad563

Please sign in to comment.