Skip to content

Commit

Permalink
Wrapper: handle SetApp for updated apps (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
sohkai authored Apr 13, 2019
1 parent 51b18bf commit 244190f
Show file tree
Hide file tree
Showing 8 changed files with 649 additions and 75 deletions.
8 changes: 5 additions & 3 deletions packages/aragon-wrapper/src/core/proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Proxy {
}

// TODO: Make this a hot observable
events (eventNames) {
events (eventNames, options = { fromBlock: this.initializationBlock }) {
// Get all events
if (!eventNames) {
eventNames = ['allEvents']
Expand All @@ -29,12 +29,14 @@ export default class Proxy {
if (eventNames.length === 1) {
// Get a specific event
eventSource = fromEvent(
this.contract.events[eventNames[0]]({ fromBlock: this.initializationBlock }), 'data'
this.contract.events[eventNames[0]](options),
'data'
)
} else {
// Get multiple events
eventSource = fromEvent(
this.contract.events.allEvents({ fromBlock: this.initializationBlock }), 'data'
this.contract.events.allEvents(options),
'data'
).pipe(
filter((event) => eventNames.includes(event.event))
)
Expand Down
118 changes: 112 additions & 6 deletions packages/aragon-wrapper/src/core/proxy/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('should get all the events', (t) => {
const eventEmitter = new EventEmitter()
const contract = {
events: {
'allEvents': () => eventEmitter
allEvents: () => eventEmitter
}
}

Expand All @@ -40,7 +40,7 @@ test('should get only the requested events', (t) => {
const eventEmitter = new EventEmitter()
const contract = {
events: {
'allEvents': () => eventEmitter
allEvents: () => eventEmitter
}
}

Expand All @@ -51,13 +51,119 @@ test('should get only the requested events', (t) => {
}
const instance = new Proxy(null, null, web3Stub)
// act
const events = instance.events(['pay_fee', 'pay_service'])
const events = instance.events(['PayFee', 'PayService'])
// assert
events.subscribe(event => {
t.deepEqual(event.amount, 5)
})

eventEmitter.emit('data', { event: 'pay_fee', amount: 5 })
eventEmitter.emit('data', { event: 'pay_something_else', amount: 10 })
eventEmitter.emit('data', { event: 'pay_service', amount: 5 })
eventEmitter.emit('data', { event: 'PayFee', amount: 5 })
eventEmitter.emit('data', { event: 'PaySomethingElse', amount: 10 })
eventEmitter.emit('data', { event: 'PayService', amount: 5 })
})

test('should get only request the single event', (t) => {
t.plan(4)
// arrange
const allEventEmitter = new EventEmitter()
const payFeeEventEmitter = new EventEmitter()
const allEventsStub = sinon.stub().returns(allEventEmitter)
const payFeeEventStub = sinon.stub().returns(payFeeEventEmitter)
const contract = {
events: {
allEvents: allEventsStub,
PayFee: payFeeEventStub
}
}

const web3Stub = {
eth: {
Contract: sinon.stub().returns(contract)
}
}
const instance = new Proxy(null, null, web3Stub)
// act
const events = instance.events('PayFee')
// assert
t.true(payFeeEventStub.calledOnce)
t.true(allEventsStub.notCalled)

let eventCount = 0
events.subscribe(event => {
if (eventCount === 0) {
t.deepEqual(event.amount, 5)
} else if (eventCount === 1) {
t.deepEqual(event.amount, 10)
} else {
// Should only see it twice
t.fail()
}
++eventCount
})

// Emit on both specific and all event emitters
allEventEmitter.emit('data', { event: 'PaySomethingElse', amount: 10 })
allEventEmitter.emit('data', { event: 'PayFee', amount: 5 })
payFeeEventEmitter.emit('data', { event: 'PayFee', amount: 5 })
allEventEmitter.emit('data', { event: 'PayFee', amount: 10 })
payFeeEventEmitter.emit('data', { event: 'PayFee', amount: 10 })
allEventEmitter.emit('data', { event: 'PayService', amount: 5 })
})

test('should default the fromBlock to initializationBlock for requested events', (t) => {
t.plan(2)
// arrange
const initializationBlock = 5
const eventEmitter = new EventEmitter()
const allEventsStub = sinon.stub().returns(eventEmitter)
const contract = {
events: {
allEvents: allEventsStub
}
}

const web3Stub = {
eth: {
Contract: sinon.stub().returns(contract)
}
}
const instance = new Proxy(null, null, web3Stub, initializationBlock)
// act
const events = instance.events()
// assert
t.true(allEventsStub.calledOnceWith({ fromBlock: initializationBlock }))
events.subscribe(event => {
t.deepEqual(event, { foo: 'bar' })
})

eventEmitter.emit('data', { foo: 'bar' })
})

test('should use the correct options for requested events', (t) => {
t.plan(2)
// arrange
const fromBlock = 10
const eventEmitter = new EventEmitter()
const allEventsStub = sinon.stub().returns(eventEmitter)
const contract = {
events: {
allEvents: allEventsStub
}
}

const web3Stub = {
eth: {
Contract: sinon.stub().returns(contract)
}
}
const instance = new Proxy(null, null, web3Stub)
// act
const events = instance.events(null, { fromBlock })
// assert
t.true(allEventsStub.calledOnceWith({ fromBlock }))
events.subscribe(event => {
t.deepEqual(event, { foo: 'bar' })
})

eventEmitter.emit('data', { foo: 'bar' })
})
Loading

0 comments on commit 244190f

Please sign in to comment.