diff --git a/src/__tests__/blockchain-url.test.ts b/src/__tests__/blockchain-url.test.ts new file mode 100644 index 0000000..dfafc98 --- /dev/null +++ b/src/__tests__/blockchain-url.test.ts @@ -0,0 +1,35 @@ +import axios from 'axios' +import { BitgesellBlockchainSDK } from '../api' + +jest.mock('axios') + +describe('Blockchain URL construction', () => { + const baseAPIURL = 'https://api.bitaps.com/bgl/v1/blockchain' + const mockedAxiosGet = axios.get as jest.Mock + let bitgesellBlockchainSDK: BitgesellBlockchainSDK + + beforeEach(() => { + mockedAxiosGet.mockResolvedValue({ data: { data: [] } }) + bitgesellBlockchainSDK = new BitgesellBlockchainSDK({ + baseAPIURL, + logger: console.log + }) + }) + + afterEach(() => { + mockedAxiosGet.mockReset() + }) + + it('omits the optional block header count segment when count is not provided', async () => { + await bitgesellBlockchainSDK.blockchain.getBlockHeaders(10) + + expect(mockedAxiosGet).toHaveBeenCalledWith(`${baseAPIURL}/block/headers/10`) + expect(mockedAxiosGet.mock.calls[0][0]).not.toContain('undefined') + }) + + it('includes the block header count segment when count is provided', async () => { + await bitgesellBlockchainSDK.blockchain.getBlockHeaders(10, 2) + + expect(mockedAxiosGet).toHaveBeenCalledWith(`${baseAPIURL}/block/headers/10/2`) + }) +}) diff --git a/src/classes/Blockchain.ts b/src/classes/Blockchain.ts index 70eaf2b..c01a3a9 100644 --- a/src/classes/Blockchain.ts +++ b/src/classes/Blockchain.ts @@ -75,7 +75,9 @@ export class Blockchain { * getBlockHeaders by block height */ public async getBlockHeaders(blockHeight: number, count?: number): Promise { - const url = `${this.apiV1URL}/block/headers/${blockHeight}/${count}` + const url = count === undefined + ? `${this.apiV1URL}/block/headers/${blockHeight}` + : `${this.apiV1URL}/block/headers/${blockHeight}/${count}` const data = await this._get(url) return data as BlockHeader } @@ -162,4 +164,4 @@ export class Blockchain { } } -} \ No newline at end of file +}