Skip to content

Commit

Permalink
Added more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Feb 22, 2018
1 parent 77f3854 commit 3ca44b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/src/renderer/lcdClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class Client {
let res = await axios[method.toLowerCase()](this.server + path, data)
return res.data
} catch (resError) {
if (!resError.response) throw resError
if (!resError.response || !resError.response.data) {
throw resError
}
let data = resError.response.data
if (!data) throw resError
// server responded with error message, create an Error from that
let error = Error(data.error)
error.code = data.code
Expand Down
49 changes: 49 additions & 0 deletions test/unit/specs/lcdClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,53 @@ describe('LCD Client', () => {
{ abc: 123 }
])
})

it('makes a GET request with an error', async () => {
axios.get = jest.fn()
.mockReturnValueOnce(Promise.reject({
response: {
data: {
error: 'foo',
code: 123
}
}
}))

try {
await client.status()
} catch (err) {
expect(err.message).toBe('foo')
expect(err.code).toBe(123)
}
expect(axios.get.mock.calls[0]).toEqual([
'http://localhost:8998/tendermint/status',
undefined
])
})

it('does not throw error for empty results', async () => {
axios.get = jest.fn()
.mockReturnValueOnce(Promise.reject({
response: {
data: {
error: 'account bytes are empty',
code: 1
}
}
}))
let res = await client.queryAccount('address')
expect(res).toBe(null)

axios.get = jest.fn()
.mockReturnValueOnce(Promise.reject({
response: {
data: {
error: 'nonce empty',
code: 2
}
}
}))
res = await client.queryNonce('address')
expect(res).toBe(0)
})
})

0 comments on commit 3ca44b1

Please sign in to comment.