fix: add connected/disconnected overrides to client.on (#1758)

* add `on` handlers for connected/disconnected

* add tests

* fix tests

* remove all listeners in teardown

* fix disconnect handler
This commit is contained in:
Mayukha Vadari
2021-10-19 17:09:46 -04:00
committed by GitHub
parent 4110c18301
commit f29e79eb68
4 changed files with 40 additions and 4 deletions

View File

@@ -422,6 +422,8 @@ class Client extends EventEmitter {
*
* @category Network
*/
public on(event: 'connected', listener: () => void): this
public on(event: 'disconnected', listener: (code: number) => void): this
public on(
event: 'ledgerClosed',
listener: (ledger: LedgerStream) => void,

View File

@@ -3,15 +3,11 @@ import _ from 'lodash'
import { Client } from 'xrpl-local'
import { setupClient, teardownClient } from './setupClient'
// how long before each test case times out
const TIMEOUT = 20000
describe('Client', function () {
this.timeout(TIMEOUT)
beforeEach(setupClient)
afterEach(teardownClient)
it('Client - implicit server port', function () {
// eslint-disable-next-line no-new -- Need to test constructor

View File

@@ -0,0 +1,37 @@
import { assert } from 'chai'
import { Client } from 'xrpl-local'
import serverUrl from './serverUrl'
// how long before each test case times out
const TIMEOUT = 20000
describe('on handlers', function () {
this.timeout(TIMEOUT)
it('on connect', async function () {
const client = new Client(serverUrl)
return new Promise<void>(function (resolve) {
client.on('connected', function () {
client.removeAllListeners()
client.disconnect()
resolve()
})
client.connect()
})
})
it('on disconnect', async function () {
const client = new Client(serverUrl)
return new Promise<void>(function (resolve) {
client.on('disconnected', function (code: number) {
// should be the normal disconnect code
assert.equal(code, 1000)
client.removeAllListeners()
resolve()
})
client.connect().then(async () => client.disconnect())
})
})
})

View File

@@ -4,6 +4,7 @@ import serverUrl from './serverUrl'
import { fundAccount } from './utils'
export async function teardownClient(this: Mocha.Context): Promise<void> {
this.client.removeAllListeners()
this.client.disconnect()
}