Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed Jun 29, 2021
1 parent 6d716dd commit 873053f
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions packages/bitcore-node/test/unit/modules/ripple/csp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { expect } from 'chai';
import request from 'request';
import * as sinon from 'sinon';
import { XRP } from '../../../../src/modules/ripple/api/csp';

describe('XRP Chain State Provider', () => {
const sandbox = sinon.createSandbox();

beforeEach(() => {
XRP.config = { testnet: { provider: { dataHost: 'http://whatevs.yo' } } };
});

afterEach(() => {
sandbox.restore();
});

describe('getBlockBeforeTime', () => {
let requestStub;
let validBody = {
ledger: {
ledger_hash: 'abc123',
ledger_index: 12,
parent_hash: 'abc122',
close_time: 1624990512
}
};

let invalidBody = {};

beforeEach(() => {
requestStub = sandbox.stub(request, 'get');
});

it('should return block', async () => {
requestStub.callsArgWith(1, null, null, validBody);
const res = await XRP.getBlockBeforeTime({ chain: 'XRP', network: 'testnet' });
expect(res).to.deep.equal({
...validBody.ledger,
chain: 'XRP',
network: 'testnet',
hash: validBody.ledger.ledger_hash,
height: validBody.ledger.ledger_index,
previousBlockHash: validBody.ledger.parent_hash,
timeNormalized: new Date(validBody.ledger.close_time *1000)
});
});

it('should throw on invalid time', async () => {
try {
await XRP.getBlockBeforeTime({ chain: 'XRP', network: 'testnet', time: 'not-a-time' });
throw new Error('should have thrown');
} catch (err) {
expect(err.message).to.equal('Invalid time value')
}
});

it('should throw on response error', async () => {
requestStub.callsArgWith(1, 'Unresponsive server', null, validBody);
try {
await XRP.getBlockBeforeTime({ chain: 'XRP', network: 'testnet' });
throw new Error('should have thrown');
} catch (err) {
expect(err).to.equal('Unresponsive server')
}
});

it('should throw on invalid response', async () => {
requestStub.callsArgWith(1, null, null, invalidBody);
try {
await XRP.getBlockBeforeTime({ chain: 'XRP', network: 'testnet' });
throw new Error('should have thrown');
} catch (err) {
expect(err.message).to.equal('Cannot read property \'ledger_hash\' of undefined')
}
});

it('should throw on mis-configuration', async () => {
requestStub.callsArgWith(1, null, null, validBody);
XRP.config = {};
try {
await XRP.getBlockBeforeTime({ chain: 'XRP', network: 'testnet' });
throw new Error('should have thrown');
} catch (err) {
expect(err.message).to.equal('Cannot read property \'provider\' of undefined')
}
});
});
});

0 comments on commit 873053f

Please sign in to comment.