Skip to content

Commit

Permalink
Move all beforeEach() methods outside of the describe() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
josipbagaric committed Nov 20, 2018
1 parent 633d83d commit 2b9d448
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 200 deletions.
10 changes: 5 additions & 5 deletions test/unit/UnitTestCache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect, assert } from 'chai';
import Cache from '../../src/Cache';

describe('Cache unit tests', () => {
let cache: Cache<any>;
let cache: Cache<any>;

beforeEach(() => {
cache = new Cache(null);
});
beforeEach(() => {
cache = new Cache(null);
});

describe('Cache unit tests', () => {
describe('get()', () => {
it('get the key value', () => {
cache.set('key', 'value');
Expand Down
50 changes: 25 additions & 25 deletions test/unit/UnitTestLedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ import { IStatsDB } from '../../src/Stats/StatsDB';
import { Operation } from '../../src/Types/Operation';
import { ITransactionReceipt, ILog } from '../../src/Types/ITransactionReceipt';

describe('Ledger Unit Tests', async () => {
const account1: string = '0xd0700ed9f4d178adf25b45f7fa8a4ec7c230b098';
const account2: string = '0x0054a7eef4dc5d729115c71cba074151b3d41804';
const account1: string = '0xd0700ed9f4d178adf25b45f7fa8a4ec7c230b098';
const account2: string = '0x0054a7eef4dc5d729115c71cba074151b3d41804';

const tx1: string = '0xaa55bf414ecef0285dcece4ddf78a0ee8beb6707';
const tx1: string = '0xaa55bf414ecef0285dcece4ddf78a0ee8beb6707';

const gas = 100000;
const gasPrice = new BigNumber(100000000);
const requiredDeposit = new BigNumber(10000000);
const opts = {
to: account1,
value: new BigNumber(0),
gas,
gasPrice,
data: '0x0',
operation: Operation.EXECUTE
};
const gas = 100000;
const gasPrice = new BigNumber(100000000);
const requiredDeposit = new BigNumber(10000000);
const opts = {
to: account1,
value: new BigNumber(0),
gas,
gasPrice,
data: '0x0',
operation: Operation.EXECUTE
};

let ledger: ILedger;
let stats: TypeMoq.IMock<IStatsDB>;
let ledger: ILedger;
let stats: TypeMoq.IMock<IStatsDB>;

const txRequest = TypeMoq.Mock.ofType<ITxRequest>();
txRequest.setup(x => x.requiredDeposit).returns(() => requiredDeposit);
txRequest.setup(x => x.address).returns(() => tx1);
const txRequest = TypeMoq.Mock.ofType<ITxRequest>();
txRequest.setup(x => x.requiredDeposit).returns(() => requiredDeposit);
txRequest.setup(x => x.address).returns(() => tx1);

const reset = async () => {
stats = TypeMoq.Mock.ofType<IStatsDB>();
ledger = new Ledger(stats.object);
};
const reset = async () => {
stats = TypeMoq.Mock.ofType<IStatsDB>();
ledger = new Ledger(stats.object);
};

beforeEach(reset);
beforeEach(reset);

describe('Ledger Unit Tests', async () => {
it('should account for required deposit and tx cost when claiming was successful', async () => {
const receipt = TypeMoq.Mock.ofType<ITransactionReceipt>();
receipt.setup(r => r.status).returns(() => '0x1');
Expand Down
146 changes: 73 additions & 73 deletions test/unit/UnitTestRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,83 @@ import { ICachedTxDetails } from '../../src/Cache';
const TIMESTAMP_TX = 'timestamp Tx';
const BLOCK_TX = 'block Tx';

// tslint:disable-next-line:no-big-function
describe('Router Unit Tests', () => {
let config: Config;
let txTimestamp: ITxRequest;
let txBlock: ITxRequest;

let router: Router;
let myAccount: string;

const createRouter = async (claimingEnabled = true) => {
const web3 = {
eth: {
getBlockNumber: (callback: any) => callback(null, 1000)
},
toWei: config.web3.toWei
};

const v3wallet = TypeMoq.Mock.ofType<V3Wallet>();
v3wallet.setup(w => w.getAddressString()).returns(() => myAccount);

const wallet = TypeMoq.Mock.ofType<Wallet>();
wallet
.setup(w => w.isWaitingForConfirmation(TypeMoq.It.isAnyString(), TypeMoq.It.isAny()))
.returns(() => false);
wallet.setup(w => w.nextAccount).returns(() => v3wallet.object);
wallet.setup(w => w.isKnownAddress(myAccount)).returns(() => true);
wallet.setup(w => w.isKnownAddress(TypeMoq.It.isAnyString())).returns(() => false);

const util = TypeMoq.Mock.ofType<W3Util>();
util.setup(u => u.networkGasPrice()).returns(async () => new BigNumber(20000));
util
.setup(u => u.getAdvancedNetworkGasPrice())
.returns(() =>
Promise.resolve({
fastest: new BigNumber(20000)
} as GasPriceEstimation)
);

const economicStrategyManager = TypeMoq.Mock.ofType<IEconomicStrategyManager>();
economicStrategyManager
.setup(e => e.shouldClaimTx(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(async () => EconomicStrategyStatus.CLAIM);

txTimestamp = await mockTxRequest(web3);
txBlock = await mockTxRequest(web3, true);

config.cache.set(txTimestamp.address, {} as ICachedTxDetails);
config.cache.set(txBlock.address, {} as ICachedTxDetails);

const actions = new Actions(
config.wallet,
config.ledger,
config.logger,
config.cache,
util.object,
config.pending
);

return new Router(
claimingEnabled,
config.cache,
config.logger,
actions,
economicStrategyManager.object,
util.object,
wallet.object
);
let config: Config;
let txTimestamp: ITxRequest;
let txBlock: ITxRequest;

let router: Router;
let myAccount: string;

const createRouter = async (claimingEnabled = true) => {
const web3 = {
eth: {
getBlockNumber: (callback: any) => callback(null, 1000)
},
toWei: config.web3.toWei
};

const reset = async () => {
config = await mockConfig();
router = await createRouter();
myAccount = config.wallet.getAddresses()[0];
};
const v3wallet = TypeMoq.Mock.ofType<V3Wallet>();
v3wallet.setup(w => w.getAddressString()).returns(() => myAccount);

const wallet = TypeMoq.Mock.ofType<Wallet>();
wallet
.setup(w => w.isWaitingForConfirmation(TypeMoq.It.isAnyString(), TypeMoq.It.isAny()))
.returns(() => false);
wallet.setup(w => w.nextAccount).returns(() => v3wallet.object);
wallet.setup(w => w.isKnownAddress(myAccount)).returns(() => true);
wallet.setup(w => w.isKnownAddress(TypeMoq.It.isAnyString())).returns(() => false);

const util = TypeMoq.Mock.ofType<W3Util>();
util.setup(u => u.networkGasPrice()).returns(async () => new BigNumber(20000));
util
.setup(u => u.getAdvancedNetworkGasPrice())
.returns(() =>
Promise.resolve({
fastest: new BigNumber(20000)
} as GasPriceEstimation)
);

beforeEach(reset);
const economicStrategyManager = TypeMoq.Mock.ofType<IEconomicStrategyManager>();
economicStrategyManager
.setup(e => e.shouldClaimTx(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(async () => EconomicStrategyStatus.CLAIM);

txTimestamp = await mockTxRequest(web3);
txBlock = await mockTxRequest(web3, true);

config.cache.set(txTimestamp.address, {} as ICachedTxDetails);
config.cache.set(txBlock.address, {} as ICachedTxDetails);

const actions = new Actions(
config.wallet,
config.ledger,
config.logger,
config.cache,
util.object,
config.pending
);

return new Router(
claimingEnabled,
config.cache,
config.logger,
actions,
economicStrategyManager.object,
util.object,
wallet.object
);
};

const reset = async () => {
config = await mockConfig();
router = await createRouter();
myAccount = config.wallet.getAddresses()[0];
};

beforeEach(reset);

// tslint:disable-next-line:no-big-function
describe('Router Unit Tests', () => {
it('initializes the Router', async () => {
expect(router).to.exist;
});
Expand Down
18 changes: 9 additions & 9 deletions test/unit/UnitTestScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import IRouter from '../../src/Router';
import Scanner from '../../src/Scanner';
import { mockConfig } from '../helpers';

describe('Scanner Unit Tests', () => {
let config: Config;
let scanner: Scanner;
let config: Config;
let scanner: Scanner;

const reset = async () => {
const router = TypeMoq.Mock.ofType<IRouter>();
config = await mockConfig();
const reset = async () => {
const router = TypeMoq.Mock.ofType<IRouter>();
config = await mockConfig();

scanner = new Scanner(config, router.object);
};
scanner = new Scanner(config, router.object);
};

beforeEach(reset);
beforeEach(reset);

describe('Scanner Unit Tests', () => {
it('initializes the Scanner', () => {
scanner = new Scanner(config, null);
expect(scanner).to.exist;
Expand Down
34 changes: 17 additions & 17 deletions test/unit/UnitTestStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import * as loki from 'lokijs';
import { StatsDB } from '../../src/Stats';
import { assert } from 'chai';

// tslint:disable-next-line:no-big-function
describe('Stats Unit Tests', () => {
const account1: string = '0xd0700ed9f4d178adf25b45f7fa8a4ec7c230b098';
const account2: string = '0x0054a7eef4dc5d729115c71cba074151b3d41804';
const account1: string = '0xd0700ed9f4d178adf25b45f7fa8a4ec7c230b098';
const account2: string = '0x0054a7eef4dc5d729115c71cba074151b3d41804';

const tx1: string = '0xaa55bf414ecef0285dcece4ddf78a0ee8beb6707';
const tx2: string = '0x9b7b4a8fdafda1b688c22fcb6f4bc97ed29ff676';
const tx3: string = '0x57f1b33b8b44689982ce7b3f560577e89375b2da';
let stats: StatsDB;
let cost: BigNumber;
let bounty: BigNumber;
const tx1: string = '0xaa55bf414ecef0285dcece4ddf78a0ee8beb6707';
const tx2: string = '0x9b7b4a8fdafda1b688c22fcb6f4bc97ed29ff676';
const tx3: string = '0x57f1b33b8b44689982ce7b3f560577e89375b2da';
let stats: StatsDB;
let cost: BigNumber;
let bounty: BigNumber;

const reset = async () => {
stats = new StatsDB(new loki('stats.db'));
await stats.init();
cost = new BigNumber(10);
bounty = new BigNumber(15);
};
const reset = async () => {
stats = new StatsDB(new loki('stats.db'));
await stats.init();
cost = new BigNumber(10);
bounty = new BigNumber(15);
};

beforeEach(reset);
beforeEach(reset);

// tslint:disable-next-line:no-big-function
describe('Stats Unit Tests', () => {
describe('getFailedExecutions()', () => {
// tslint:disable-next-line:no-duplicate-string
it('returns an empty array if none', () => {
Expand Down
44 changes: 22 additions & 22 deletions test/unit/UnitTestTimeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import { mockConfig } from '../helpers';
import { BigNumber } from 'bignumber.js';
import { TxStatus } from '../../src/Enum';

describe('TimeNode Unit Tests', () => {
let config: Config;
let myAccount: string;
let timenode: TimeNode;
const emitEvents = {
emitClose: (self: any) => {
self.emit('close');
},
emitEnd: (self: any) => {
self.connection._client.emit('connectFailed');
},
emitError: (self: any) => {
//Trigger connection failed event
self.connection._client.emit('connectFailed');
}
};

before(async () => {
config = await mockConfig();
myAccount = config.wallet.getAddresses()[0];
timenode = new TimeNode(config);
});
let config: Config;
let myAccount: string;
let timenode: TimeNode;
const emitEvents = {
emitClose: (self: any) => {
self.emit('close');
},
emitEnd: (self: any) => {
self.connection._client.emit('connectFailed');
},
emitError: (self: any) => {
//Trigger connection failed event
self.connection._client.emit('connectFailed');
}
};

before(async () => {
config = await mockConfig();
myAccount = config.wallet.getAddresses()[0];
timenode = new TimeNode(config);
});

describe('TimeNode Unit Tests', () => {
it('initializes a basic timenode', () => {
expect(timenode).to.exist; // tslint:disable-line no-unused-expression
});
Expand Down

0 comments on commit 2b9d448

Please sign in to comment.