mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-06 17:27:59 +00:00
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:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
37
test/integration/onConnect.ts
Normal file
37
test/integration/onConnect.ts
Normal 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())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user