From f29e79eb689a84914fdc7014700f33a7fe729b16 Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Tue, 19 Oct 2021 17:09:46 -0400 Subject: [PATCH] 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 --- src/client/index.ts | 2 ++ test/client.ts | 4 ---- test/integration/onConnect.ts | 37 +++++++++++++++++++++++++++++++++++ test/integration/setup.ts | 1 + 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/integration/onConnect.ts diff --git a/src/client/index.ts b/src/client/index.ts index c6a88f1a..9e4f0a8f 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -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, diff --git a/test/client.ts b/test/client.ts index 5f9664b1..c729ae5f 100644 --- a/test/client.ts +++ b/test/client.ts @@ -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 diff --git a/test/integration/onConnect.ts b/test/integration/onConnect.ts new file mode 100644 index 00000000..fdada250 --- /dev/null +++ b/test/integration/onConnect.ts @@ -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(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(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()) + }) + }) +}) diff --git a/test/integration/setup.ts b/test/integration/setup.ts index 519d9653..0fd011f9 100644 --- a/test/integration/setup.ts +++ b/test/integration/setup.ts @@ -4,6 +4,7 @@ import serverUrl from './serverUrl' import { fundAccount } from './utils' export async function teardownClient(this: Mocha.Context): Promise { + this.client.removeAllListeners() this.client.disconnect() }