2018-11-23 12:40:49 +01:00
|
|
|
import {assert} from "chai"
|
|
|
|
import ConfigProvider from "../../src/ConfigProvider"
|
|
|
|
import EventListener from "../../src/keeper/EventListener"
|
|
|
|
import Keeper from "../../src/keeper/Keeper"
|
|
|
|
import Account from "../../src/ocean/Account"
|
|
|
|
import Ocean from "../../src/ocean/Ocean"
|
|
|
|
import config from "../config"
|
|
|
|
import TestContractHandler from "./TestContractHandler"
|
|
|
|
|
|
|
|
let keeper: Keeper
|
|
|
|
let ocean: Ocean
|
|
|
|
let accounts: Account[]
|
|
|
|
|
|
|
|
describe("EventListener", () => {
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
ConfigProvider.setConfig(config)
|
|
|
|
await TestContractHandler.prepareContracts()
|
|
|
|
keeper = await Keeper.getInstance()
|
|
|
|
assert(keeper)
|
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
assert(ocean)
|
|
|
|
accounts = await ocean.getAccounts()
|
|
|
|
assert(accounts)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#listen()", () => {
|
|
|
|
|
|
|
|
it("should listen to an event", (done) => {
|
|
|
|
|
|
|
|
const acc = accounts[1]
|
|
|
|
|
|
|
|
const event = EventListener.subscribe("OceanToken",
|
|
|
|
"Transfer",
|
|
|
|
{
|
|
|
|
to: acc.getId(),
|
|
|
|
})
|
|
|
|
|
|
|
|
event.listen((events) => {
|
|
|
|
|
|
|
|
assert(events)
|
|
|
|
assert(events.length === 2)
|
|
|
|
done()
|
2018-11-23 14:54:22 +01:00
|
|
|
EventListener.unsubscribe(event)
|
2018-11-23 12:40:49 +01:00
|
|
|
})
|
|
|
|
|
2019-01-30 10:51:55 +01:00
|
|
|
const {dispenser} = keeper
|
2018-11-23 12:40:49 +01:00
|
|
|
|
2019-01-30 10:51:55 +01:00
|
|
|
dispenser.requestTokens(400, acc.getId())
|
|
|
|
dispenser.requestTokens(400, acc.getId())
|
2018-11-23 12:40:49 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#listenOnce()", () => {
|
|
|
|
|
2018-11-23 14:28:41 +01:00
|
|
|
it("should listen once", (done) => {
|
2018-11-23 12:40:49 +01:00
|
|
|
|
|
|
|
const acc = accounts[1]
|
|
|
|
|
2018-11-23 14:28:41 +01:00
|
|
|
const countBefore = EventListener.count()
|
2018-11-23 12:40:49 +01:00
|
|
|
const event = EventListener.subscribe("OceanToken",
|
|
|
|
"Transfer",
|
|
|
|
{
|
|
|
|
to: acc.getId(),
|
|
|
|
})
|
|
|
|
|
2018-11-23 14:28:41 +01:00
|
|
|
event.listenOnce(
|
|
|
|
(data: any) => {
|
2018-11-23 12:40:49 +01:00
|
|
|
|
2018-11-23 14:28:41 +01:00
|
|
|
assert(data)
|
|
|
|
assert(data.blockNumber)
|
|
|
|
assert(EventListener.count() === countBefore)
|
|
|
|
done()
|
|
|
|
})
|
2018-11-23 12:40:49 +01:00
|
|
|
|
2019-01-30 10:51:55 +01:00
|
|
|
const {dispenser} = keeper
|
2018-11-23 12:40:49 +01:00
|
|
|
|
2019-01-30 10:51:55 +01:00
|
|
|
dispenser.requestTokens(400, acc.getId())
|
2018-11-23 12:40:49 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|