Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 9836831

Browse files
committed
Add tests
1 parent 92f628f commit 9836831

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/index.test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const test = require('ava')
22
const nock = require('nock')
33
const fromString = require('from2-string')
4-
const { TextHTTPError } = require('micro-api-client')
4+
const { TextHTTPError, JSONHTTPError } = require('micro-api-client')
55

66
const NetlifyAPI = require('./index')
77

@@ -310,7 +310,7 @@ test('Can parse text responses', async t => {
310310
t.true(scope.isDone())
311311
})
312312

313-
test('Handle error JSON responses', async t => {
313+
test('Handle error empty responses', async t => {
314314
const account_id = '8'
315315
const status = 404
316316
const scope = nock(origin)
@@ -347,6 +347,44 @@ test('Handle error text responses', async t => {
347347
t.true(scope.isDone())
348348
})
349349

350+
test('Handle error text responses on JSON endpoints', async t => {
351+
const account_id = '9'
352+
const status = 404
353+
const expectedResponse = 'test'
354+
const scope = nock(origin)
355+
.get(`${pathPrefix}/accounts/${account_id}`)
356+
.reply(status, expectedResponse, { 'Content-Type': 'application/json' })
357+
358+
const client = getClient()
359+
const error = await t.throwsAsync(client.getAccount({ account_id }))
360+
361+
t.is(error.status, status)
362+
t.is(error.message, 'Not Found')
363+
t.is(error.data, expectedResponse)
364+
t.true(error instanceof TextHTTPError)
365+
t.true(error.stack !== undefined)
366+
t.true(scope.isDone())
367+
})
368+
369+
test('Handle error JSON responses', async t => {
370+
const account_id = '8'
371+
const status = 404
372+
const errorJson = { error: true }
373+
const scope = nock(origin)
374+
.get(`${pathPrefix}/accounts/${account_id}`)
375+
.reply(status, errorJson)
376+
377+
const client = getClient()
378+
const error = await t.throwsAsync(client.getAccount({ account_id }))
379+
380+
t.is(error.status, status)
381+
t.is(error.message, 'Not Found')
382+
t.deepEqual(error.json, errorJson)
383+
t.true(error instanceof JSONHTTPError)
384+
t.true(error.stack !== undefined)
385+
t.true(scope.isDone())
386+
})
387+
350388
test('Handle network errors', async t => {
351389
const account_id = '10'
352390
const expectedResponse = 'test'

0 commit comments

Comments
 (0)