From c0d3968ff6c6187860fab757f914a5484323da2b Mon Sep 17 00:00:00 2001 From: Fernando Boucquez Date: Wed, 30 Oct 2019 12:40:29 +0100 Subject: [PATCH 1/2] fixed unit test --- test/infrastructure/Listener.spec.ts | 59 +++++++++++++++++----------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/test/infrastructure/Listener.spec.ts b/test/infrastructure/Listener.spec.ts index 4bdf22387e..ebc0ad846c 100644 --- a/test/infrastructure/Listener.spec.ts +++ b/test/infrastructure/Listener.spec.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import {expect} from 'chai'; -import {Listener} from '../../src/infrastructure/Listener'; -import {Address} from "../../src/model/account/Address"; -import {deepEqual} from "assert"; -import {UInt64} from "../../src/model/UInt64"; -import {timeout} from "rxjs/operators"; +import { expect } from 'chai'; +import { Listener } from '../../src/infrastructure/Listener'; +import { Address } from "../../src/model/account/Address"; +import { deepEqual } from "assert"; +import { UInt64 } from "../../src/model/UInt64"; +import { timeout } from "rxjs/operators"; +import { TransactionStatusError } from "../../src/model/transaction/TransactionStatusError"; describe('Listener', () => { it('should createComplete a WebSocket instance given url parameter', () => { @@ -64,17 +65,25 @@ describe('Listener', () => { listener.open(); - listener.status(errorAddress).pipe(timeout(2000)).subscribe((transactionStatusError) => { + const reportedStatus = new Array(); + + listener.status(errorAddress).subscribe((transactionStatusError) => { + reportedStatus.push(transactionStatusError); + }, err => { + done(err); + }); + + listener.handleMessage(statusInfoErrorDTO, null); + + setTimeout(() => { + expect(reportedStatus.length).to.be.equal(1); + const transactionStatusError = reportedStatus[0]; expect(transactionStatusError.address).to.deep.equal(errorAddress); expect(transactionStatusError.hash).to.be.equal(statusInfoErrorDTO.hash); expect(transactionStatusError.status).to.be.equal(statusInfoErrorDTO.status); deepEqual(transactionStatusError.deadline.toDTO(), UInt64.fromNumericString(statusInfoErrorDTO.deadline).toDTO()); done(); - }, err => { - done('Should have not timed out!'); - }); - - listener.handleMessage(statusInfoErrorDTO, null); + }, 100) }); @@ -110,15 +119,21 @@ describe('Listener', () => { listener.open(); - listener.status(subscribedAddress).pipe(timeout(100)).subscribe(status => { - done('Should have timed out!'); + const reportedStatus = new Array(); + + listener.status(subscribedAddress).subscribe((transactionStatusError) => { + reportedStatus.push(transactionStatusError); }, err => { - expect(err.name).to.be.eq('TimeoutError'); - done(); + done(err); }); listener.handleMessage(statusInfoErrorDTO, null); + setTimeout(() => { + expect(reportedStatus.length).to.be.equal(0); + done(); + }, 100) + }); }); @@ -127,12 +142,12 @@ describe('Listener', () => { it('should reject because of wrong server url', async () => { const listener = new Listener('https://notcorrecturl:0000'); await listener.open() - .then((result) => { - throw new Error('This should not be called when expecting error'); - }) - .catch((error) => { - expect(error.message.toString()).not.to.be.equal(''); - }); + .then((result) => { + throw new Error('This should not be called when expecting error'); + }) + .catch((error) => { + expect(error.message.toString()).not.to.be.equal(''); + }); }); }); }); From 9012d44bb89adbddd605a715cab78add6198b6e9 Mon Sep 17 00:00:00 2001 From: Fernando Boucquez Date: Wed, 30 Oct 2019 12:44:01 +0100 Subject: [PATCH 2/2] improved unit test --- test/infrastructure/Listener.spec.ts | 30 ++++++++++------------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/test/infrastructure/Listener.spec.ts b/test/infrastructure/Listener.spec.ts index ebc0ad846c..f6aa178479 100644 --- a/test/infrastructure/Listener.spec.ts +++ b/test/infrastructure/Listener.spec.ts @@ -38,7 +38,7 @@ describe('Listener', () => { }); describe('onStatusWhenAddressIsTheSame', () => { - it('Should forward status', (done) => { + it('Should forward status', () => { const errorEncodedAddress = '906415867F121D037AF447E711B0F5E4D52EBBF066D96860EB'; @@ -69,28 +69,23 @@ describe('Listener', () => { listener.status(errorAddress).subscribe((transactionStatusError) => { reportedStatus.push(transactionStatusError); - }, err => { - done(err); }); listener.handleMessage(statusInfoErrorDTO, null); - setTimeout(() => { - expect(reportedStatus.length).to.be.equal(1); - const transactionStatusError = reportedStatus[0]; - expect(transactionStatusError.address).to.deep.equal(errorAddress); - expect(transactionStatusError.hash).to.be.equal(statusInfoErrorDTO.hash); - expect(transactionStatusError.status).to.be.equal(statusInfoErrorDTO.status); - deepEqual(transactionStatusError.deadline.toDTO(), UInt64.fromNumericString(statusInfoErrorDTO.deadline).toDTO()); - done(); - }, 100) + expect(reportedStatus.length).to.be.equal(1); + const transactionStatusError = reportedStatus[0]; + expect(transactionStatusError.address).to.deep.equal(errorAddress); + expect(transactionStatusError.hash).to.be.equal(statusInfoErrorDTO.hash); + expect(transactionStatusError.status).to.be.equal(statusInfoErrorDTO.status); + deepEqual(transactionStatusError.deadline.toDTO(), UInt64.fromNumericString(statusInfoErrorDTO.deadline).toDTO()); }); }); describe('onStatusWhenAddressIsDifferentAddress', () => { - it('Should not forward status', (done) => { + it('Should not forward status', () => { const errorEncodedAddress = '906415867F121D037AF447E711B0F5E4D52EBBF066D96860EB'; @@ -123,16 +118,11 @@ describe('Listener', () => { listener.status(subscribedAddress).subscribe((transactionStatusError) => { reportedStatus.push(transactionStatusError); - }, err => { - done(err); }); listener.handleMessage(statusInfoErrorDTO, null); - - setTimeout(() => { - expect(reportedStatus.length).to.be.equal(0); - done(); - }, 100) + + expect(reportedStatus.length).to.be.equal(0); });