Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/__tests__/blockchain-url.test.ts
Original file line number Diff line number Diff line change
@@ -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`)
})
})
6 changes: 4 additions & 2 deletions src/classes/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export class Blockchain {
* getBlockHeaders by block height
*/
public async getBlockHeaders(blockHeight: number, count?: number): Promise<BlockHeader> {
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
}
Expand Down Expand Up @@ -162,4 +164,4 @@ export class Blockchain {
}
}

}
}