2021-02-05 09:01:37 -05:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
import {
|
|
|
|
deleteJSON,
|
2021-02-10 04:36:54 -05:00
|
|
|
FetchError,
|
2021-02-05 09:01:37 -05:00
|
|
|
getJSON,
|
|
|
|
postJSON,
|
|
|
|
putJSON
|
|
|
|
} from '../../../frontend/js/infrastructure/fetch-json'
|
|
|
|
|
|
|
|
describe('fetchJSON', function() {
|
|
|
|
before(function() {
|
|
|
|
fetchMock.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
fetchMock.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
const headers = {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
|
|
|
|
it('handles GET requests', function() {
|
|
|
|
fetchMock.once(
|
|
|
|
{ method: 'GET', url: '/test', headers },
|
|
|
|
{ status: 200, body: { result: 'success' } }
|
|
|
|
)
|
|
|
|
|
|
|
|
return expect(getJSON('/test')).to.eventually.deep.equal({
|
|
|
|
result: 'success'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles 4xx responses', function() {
|
|
|
|
fetchMock.get('/test', {
|
|
|
|
status: 400,
|
|
|
|
body: { message: 'The request was invalid' }
|
|
|
|
})
|
|
|
|
|
|
|
|
return expect(getJSON('/test'))
|
|
|
|
.to.eventually.be.rejectedWith('Bad Request')
|
2021-02-10 04:36:54 -05:00
|
|
|
.and.be.an.instanceOf(FetchError)
|
2021-02-05 09:01:37 -05:00
|
|
|
.to.nested.include({
|
2021-02-10 04:36:54 -05:00
|
|
|
message: 'Bad Request',
|
|
|
|
'data.message': 'The request was invalid',
|
|
|
|
'response.status': 400,
|
|
|
|
'info.statusCode': 400
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles 5xx responses', async function() {
|
|
|
|
fetchMock.get('/test', { status: 500 })
|
|
|
|
|
|
|
|
return expect(getJSON('/test'))
|
|
|
|
.to.eventually.be.rejectedWith('Internal Server Error')
|
2021-02-10 04:36:54 -05:00
|
|
|
.and.be.an.instanceOf(FetchError)
|
2021-02-05 09:01:37 -05:00
|
|
|
.to.nested.include({
|
2021-02-10 04:36:54 -05:00
|
|
|
'response.status': 500,
|
|
|
|
'info.statusCode': 500
|
2021-02-05 09:01:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles POST requests', function() {
|
|
|
|
const body = { example: true }
|
|
|
|
|
|
|
|
fetchMock.once(
|
|
|
|
{ method: 'POST', url: '/test', headers, body },
|
|
|
|
{ status: 200, body: { result: 'success' } }
|
|
|
|
)
|
|
|
|
|
|
|
|
return expect(postJSON('/test', { body })).to.eventually.deep.equal({
|
|
|
|
result: 'success'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles PUT requests', function() {
|
|
|
|
const body = { example: true }
|
|
|
|
|
|
|
|
fetchMock.once(
|
|
|
|
{ method: 'PUT', url: '/test', headers, body },
|
|
|
|
{ status: 200, body: { result: 'success' } }
|
|
|
|
)
|
|
|
|
|
|
|
|
return expect(putJSON('/test', { body })).to.eventually.deep.equal({
|
|
|
|
result: 'success'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles DELETE requests', function() {
|
|
|
|
fetchMock.once({ method: 'DELETE', url: '/test', headers }, { status: 204 })
|
|
|
|
|
|
|
|
return expect(deleteJSON('/test')).to.eventually.deep.equal({})
|
|
|
|
})
|
|
|
|
})
|