From 32fabfdbd1b4654d5b5aab8abc327438c38c49ea Mon Sep 17 00:00:00 2001 From: Frantz Arty Date: Thu, 13 Jan 2022 20:28:14 -0500 Subject: [PATCH] feat: clock stubs in unit tests --- tests/integration/unit.spec.js | 203 +++++++++++++++------------------ 1 file changed, 92 insertions(+), 111 deletions(-) diff --git a/tests/integration/unit.spec.js b/tests/integration/unit.spec.js index 399b3546..8b3cad06 100644 --- a/tests/integration/unit.spec.js +++ b/tests/integration/unit.spec.js @@ -2,16 +2,25 @@ import chai from 'chai'; import supertest from 'supertest'; import app from '../../src/server'; import _ from 'lodash'; +import sinon from 'sinon'; import { WAIT_TIME } from '../../src/fullnode/simulator'; const { expect } = chai; describe('Create Unit Integration', () => { + let clock; beforeEach(async () => { + clock = sinon.useFakeTimers({ + toFake: ['setTimeout'], + }); await supertest(app).get(`/v1/staging/clean`); }); + afterEach(() => { + clock.restore(); + }); + it('splits an existing unit end-to-end', async () => { // Get a unit to split const allUnitsResult = await supertest(app).get('/v1/units'); @@ -76,7 +85,7 @@ describe('Create Unit Integration', () => { expect(splitRecord2.unitCount).to.equal(1); // Expect the split unitscounts to add up to the original unit count - expect(splitRecord1.unitCount + splitRecord2.unitCount).to.equal( + expect(splitRecord1.unitCount splitRecord2.unitCount).to.equal( originalRecord.unitCount, ); @@ -123,79 +132,63 @@ describe('Create Unit Integration', () => { // The node simulator runs on an async process, we are importing // the WAIT_TIME constant from the simulator, padding it and waiting for the // appropriate amount of time for the simulator to finish its operations - await new Promise((resolve, reject) => { - setTimeout(async () => { - try { - const warehouseRes = await supertest(app) - .get(`/v1/units`) - .query({ warehouseUnitId: splitRecord1.warehouseUnitId }); - - const newRecord1 = warehouseRes.body; - - expect(newRecord1.warehouseUnitId).to.equal( - splitRecord1.warehouseUnitId, - ); - expect(newRecord1.orgUid).to.equal(splitRecord1.orgUid); - expect(newRecord1.unitOwnerOrgUid).to.equal( - splitRecord1.unitOwnerOrgUid, - ); - expect(newRecord1.serialNumberBlock).to.equal( - splitRecord1.serialNumberBlock, - ); - expect(newRecord1.countryJurisdictionOfOwner).to.equal( - splitRecord1.countryJurisdictionOfOwner, - ); - expect(newRecord1.inCountryJurisdictionOfOwner).to.equal( - splitRecord1.inCountryJurisdictionOfOwner, - ); - expect(newRecord1.tokenIssuanceHash).to.equal( - splitRecord1.tokenIssuanceHash, - ); - - const warehouse2Res = await supertest(app) - .get(`/v1/units`) - .query({ warehouseUnitId: splitRecord2.warehouseUnitId }); - - const newRecord2 = warehouse2Res.body; - - expect(newRecord2.warehouseUnitId).to.equal( - splitRecord2.warehouseUnitId, - ); - expect(newRecord2.orgUid).to.equal(splitRecord2.orgUid); - expect(newRecord2.unitOwnerOrgUid).to.equal( - splitRecord2.unitOwnerOrgUid, - ); - expect(newRecord1.serialNumberBlock).to.equal( - splitRecord1.serialNumberBlock, - ); - expect(newRecord2.countryJurisdictionOfOwner).to.equal( - splitRecord2.countryJurisdictionOfOwner, - ); - expect(newRecord2.inCountryJurisdictionOfOwner).to.equal( - splitRecord2.inCountryJurisdictionOfOwner, - ); - expect(newRecord2.tokenIssuanceHash).to.equal( - splitRecord2.tokenIssuanceHash, - ); - - // make sure the original record was deleted - const warehouse3Res = await supertest(app) - .get(`/v1/units`) - .query({ warehouseUnitId: warehouseUnitIdToSplit }); - expect(warehouse3Res.body).to.equal(null); - - // Make sure the staging table is cleaned up - const stagingRes3 = await supertest(app).get('/v1/staging'); - - // There should be no staging records left - expect(stagingRes3.body.length).to.equal(0); - - resolve(); - } catch (err) { - reject(err); - } - }, WAIT_TIME * 3); - }); + clock.tick(WAIT_TIME * 4); + + const warehouseRes = await supertest(app) + .get(`/v1/units`) + .query({ warehouseUnitId: splitRecord1.warehouseUnitId }); + + const newRecord1 = warehouseRes.body; + + expect(newRecord1.warehouseUnitId).to.equal(splitRecord1.warehouseUnitId); + expect(newRecord1.orgUid).to.equal(splitRecord1.orgUid); + expect(newRecord1.unitOwnerOrgUid).to.equal(splitRecord1.unitOwnerOrgUid); + expect(newRecord1.serialNumberBlock).to.equal( + splitRecord1.serialNumberBlock, + ); + expect(newRecord1.countryJuridictionOfOwner).to.equal( + splitRecord1.countryJuridictionOfOwner, + ); + expect(newRecord1.inCountryJuridictionOfOwner).to.equal( + splitRecord1.inCountryJuridictionOfOwner, + ); + expect(newRecord1.tokenIssuanceHash).to.equal( + splitRecord1.tokenIssuanceHash, + ); + + const warehouse2Res = await supertest(app) + .get(`/v1/units`) + .query({ warehouseUnitId: splitRecord2.warehouseUnitId }); + + const newRecord2 = warehouse2Res.body; + + expect(newRecord2.warehouseUnitId).to.equal(splitRecord2.warehouseUnitId); + expect(newRecord2.orgUid).to.equal(splitRecord2.orgUid); + expect(newRecord2.unitOwnerOrgUid).to.equal(splitRecord2.unitOwnerOrgUid); + expect(newRecord1.serialNumberBlock).to.equal( + splitRecord1.serialNumberBlock, + ); + expect(newRecord2.countryJuridictionOfOwner).to.equal( + splitRecord2.countryJuridictionOfOwner, + ); + expect(newRecord2.inCountryJuridictionOfOwner).to.equal( + splitRecord2.inCountryJuridictionOfOwner, + ); + expect(newRecord2.tokenIssuanceHash).to.equal( + splitRecord2.tokenIssuanceHash, + ); + + // make sure the original record was deleted + const warehouse3Res = await supertest(app) + .get(`/v1/units`) + .query({ warehouseUnitId: warehouseUnitIdToSplit }); + expect(warehouse3Res.body).to.equal(null); + + // Make sure the staging table is cleaned up + const stagingRes3 = await supertest(app).get('/v1/staging'); + + // There should be no staging records left + expect(stagingRes3.body.length).to.equal(0); }); it('creates a new unit end-to-end', async () => { @@ -264,42 +257,30 @@ describe('Create Unit Integration', () => { // The node simulator runs on an async process, we are importing // the WAIT_TIME constant from the simulator, padding it and waiting for the // appropriate amount of time for the simulator to finish its operations - await new Promise((resolve, reject) => { - setTimeout(async () => { - try { - // Make sure the staging table is cleaned up - const stagingRes3 = await supertest(app).get('/v1/staging'); - - // There should be no staging records left - expect(stagingRes3.body.length).to.equal(0); - - const warehouseRes = await supertest(app) - .get(`/v1/units`) - .query({ warehouseUnitId }); - - const newRecord = warehouseRes.body; - - expect(newRecord.warehouseUnitId).to.equal(warehouseUnitId); - expect(newRecord.orgUid).to.equal(orgUid); - expect(newRecord.unitOwnerOrgUid).to.equal(orgUid); - expect(newRecord.serialNumberBlock).to.equal( - payload.serialNumberBlock, - ); - expect(newRecord.countryJurisdictionOfOwner).to.equal( - payload.countryJurisdictionOfOwner, - ); - expect(newRecord.inCountryJurisdictionOfOwner).to.equal( - payload.inCountryJurisdictionOfOwner, - ); - expect(newRecord.tokenIssuanceHash).to.equal( - payload.tokenIssuanceHash, - ); - - resolve(); - } catch (err) { - reject(err); - } - }, WAIT_TIME * 2); - }); + clock.tick(WAIT_TIME * 2); + + // Make sure the staging table is cleaned up + const stagingRes3 = await supertest(app).get('/v1/staging'); + + // There should be no staging records left + expect(stagingRes3.body.length).to.equal(0); + + const warehouseRes = await supertest(app) + .get(`/v1/units`) + .query({ warehouseUnitId }); + + const newRecord = warehouseRes.body; + + expect(newRecord.warehouseUnitId).to.equal(warehouseUnitId); + expect(newRecord.orgUid).to.equal(orgUid); + expect(newRecord.unitOwnerOrgUid).to.equal(orgUid); + expect(newRecord.serialNumberBlock).to.equal(payload.serialNumberBlock); + expect(newRecord.countryJuridictionOfOwner).to.equal( + payload.countryJuridictionOfOwner, + ); + expect(newRecord.inCountryJuridictionOfOwner).to.equal( + payload.inCountryJuridictionOfOwner, + ); + expect(newRecord.tokenIssuanceHash).to.equal(payload.tokenIssuanceHash); }); });