Skip to content

Commit 0cdbbd5

Browse files
committed
Changed inTransaction, removed decodeLogs.
1 parent 5e95ef2 commit 0cdbbd5

File tree

5 files changed

+47
-31
lines changed

5 files changed

+47
-31
lines changed

package-lock.json

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@
5656
"truffle": "^4.1.13",
5757
"truffle-hdwallet-provider": "0.0.5",
5858
"web3-utils": "^1.0.0-beta.34"
59+
},
60+
"dependencies": {
61+
"lodash": "^4.17.11"
5962
}
6063
}

test/helpers/decodeLogs.js

-12
This file was deleted.

test/helpers/expectEvent.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { decodeLogs } = require('./decodeLogs');
1+
const SolidityEvent = require('web3/lib/web3/event.js');
2+
const _ = require('lodash');
23

34
const BigNumber = web3.BigNumber;
45
const should = require('chai')
@@ -18,14 +19,13 @@ function inLogs (logs, eventName, eventArgs = {}) {
1819
return event;
1920
}
2021

21-
async function inTransaction (tx, eventName, eventArgs = {}) {
22-
const { logs } = await tx;
23-
return inLogs(logs, eventName, eventArgs);
22+
async function inConstruction (contract, eventName, eventArgs = {}) {
23+
return inTransaction(contract.transactionHash, contract.constructor, eventName, eventArgs);
2424
}
2525

26-
async function inConstruction (contract, eventName, eventArgs = {}) {
27-
const receipt = await web3.eth.getTransactionReceipt(contract.transactionHash);
28-
const logs = decodeLogs(receipt.logs, contract.constructor.events, contract.address);
26+
async function inTransaction (txHash, emitter, eventName, eventArgs = {}) {
27+
const receipt = await web3.eth.getTransactionReceipt(txHash);
28+
const logs = decodeLogs(receipt.logs, emitter.events);
2929

3030
return inLogs(logs, eventName, eventArgs);
3131
}
@@ -44,8 +44,17 @@ function isBigNumber (object) {
4444
(object.constructor && object.constructor.name === 'BigNumber');
4545
}
4646

47+
function decodeLogs (logs, events) {
48+
return _.flatten(logs.map(log =>
49+
log.topics.filter(topic => topic in events).map(topic => {
50+
const event = new SolidityEvent(null, events[topic], 0);
51+
return event.decode(log);
52+
})
53+
));
54+
}
55+
4756
module.exports = {
4857
inLogs,
49-
inTransaction,
5058
inConstruction,
59+
inTransaction,
5160
};

test/helpers/test/expectEvent.test.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const should = require('chai')
77
.use(require('chai-as-promised'))
88
.should();
99

10-
describe('expectEvent', function () {
10+
describe.only('expectEvent', function () {
1111
beforeEach(async function () {
1212
this.constructionValues = {
1313
uint: 42,
@@ -23,15 +23,15 @@ describe('expectEvent', function () {
2323
});
2424

2525
describe('inConstructor', function () {
26-
context('long uint value', function () {
26+
context('short uint value', function () {
2727
it('accepts emitted events with correct number', async function () {
28-
await expectEvent.inConstruction(this.emitter, 'LongUint',
28+
await expectEvent.inConstruction(this.emitter, 'ShortUint',
2929
{ value: this.constructionValues.uint }
3030
);
3131
});
3232

3333
it('throws if an incorrect value is passed', async function () {
34-
return expectEvent.inConstruction(this.emitter, 'LongUint',
34+
return expectEvent.inConstruction(this.emitter, 'ShortUint',
3535
{ value: 23 }
3636
).should.be.rejected;
3737
});
@@ -290,9 +290,26 @@ describe('expectEvent', function () {
290290
});
291291

292292
describe('inTransaction', function () {
293-
it('processes the logs inside a transaction', async function () {
294-
const tx = await this.emitter.emitShortUint(5);
295-
expectEvent.inTransaction(tx, 'ShortInt', { value: 5 });
293+
describe('when emitting from called contract', function () {
294+
context('short uint value', function () {
295+
beforeEach(async function () {
296+
this.value = 42;
297+
const receipt = await this.emitter.emitShortUint(this.value);
298+
this.txHash = receipt.tx;
299+
});
300+
301+
it('accepts emitted events with correct number', async function () {
302+
await expectEvent.inTransaction(this.txHash, EventEmitter, 'ShortUint', { value: this.value });
303+
});
304+
305+
it('throws if an unemitted event is requested', function () {
306+
return expectEvent.inTransaction(this.txHash, EventEmitter, 'UnemittedEvent', { value: this.value }).should.be.rejected;
307+
});
308+
309+
it('throws if an incorrect value is passed', function () {
310+
return expectEvent.inTransaction(this.txHash, EventEmitter, 'ShortUint', { value: 23 }).should.be.rejected;
311+
});
312+
});
296313
});
297314
});
298315
});

0 commit comments

Comments
 (0)