Skip to content

Instantly share code, notes, and snippets.

@neysimoes
Created June 2, 2020 13:50
Show Gist options
  • Save neysimoes/e1a84fc16e2ddfc7fef802f58f972282 to your computer and use it in GitHub Desktop.
Save neysimoes/e1a84fc16e2ddfc7fef802f58f972282 to your computer and use it in GitHub Desktop.
import axios from 'axios'
const DEFAULT_AXIOS_CONFIG = {
validateStatus: status => status < 400,
}
const createMethods = instance => {
const METHODS = ['request', 'get', 'delete', 'head', 'options', 'post', 'put', 'patch']
const apiMethods = {}
METHODS.forEach(method => {
apiMethods[method] = async (...args) => {
try {
return await instance[method](...args)
} catch (error) {
return { error }
}
}
})
return apiMethods
}
export const create = (config = {}) => {
const apiInstance = axios.create({
...DEFAULT_AXIOS_CONFIG,
...config,
})
const apiMethods = createMethods(apiInstance)
return { ...apiInstance, ...apiMethods }
}
export const api = create()
import adapter from 'axios/lib/adapters/http'
import nock from 'nock'
import { api, create } from './'
const METHODS = ['request', 'get', 'delete', 'head', 'options', 'post', 'put', 'patch']
describe('create', () => {
const baseUrl = 'http://localhost'
const instance = create({ host: baseUrl, baseUrl, adapter })
afterAll(() => {
nock.restore()
})
beforeAll(() => {
if (!nock.isActive()) {
nock.activate()
}
})
it('has all methods', () => {
METHODS.forEach(method => expect(instance[method]).toBeDefined())
})
it('has baseUrl', async () => {
expect(instance.defaults.baseUrl).toEqual(baseUrl)
})
it('request GET with success', async () => {
nock(baseUrl)
.get('/test')
.reply(200, { message: 'ok' })
const { data } = await instance.get('/test')
expect(data).toEqual({ message: 'ok' })
})
it('request POST with success', async () => {
nock(baseUrl)
.post('/login', {
user: 'test',
password: '1234',
})
.reply(200, { message: 'success' })
const { data } = await instance.post('/login', {
user: 'test',
password: '1234',
})
expect(data).toEqual({ message: 'success' })
})
it('request with error', async () => {
nock(baseUrl)
.get('/error')
.replyWithError('Error message')
const { error } = await instance.get('/error')
expect(error.message).toEqual('Error message')
})
it('with interceptor', async () => {
nock(baseUrl)
.post('/login', {
user: 'test',
password: '1234',
})
.reply(200, { message: 'success' })
instance.interceptors.response.use(
function(response) {
expect(response.status).toBe(200)
expect(response.data).toEqual({ message: 'success' })
return response
},
function(error) {
return Promise.reject(error)
},
)
instance.interceptors.request.use(
function(request) {
expect(request.url).toBe('/login')
expect(request.method).toBe('post')
expect(request.data).toBe({
user: 'test',
password: '1234',
})
return request
},
function(error) {
return Promise.reject(error)
},
)
await instance.post('/login', {
user: 'test',
password: '1234',
})
})
})
describe('api', () => {
const baseUrl = 'http://localhost'
api.defaults.adapter = adapter
afterAll(() => {
nock.restore()
})
beforeAll(() => {
if (!nock.isActive()) {
nock.activate()
}
})
it('has all methods', () => {
METHODS.forEach(method => expect(api[method]).toBeDefined())
})
it('request GET with success', async () => {
nock(baseUrl)
.get('/api-get')
.reply(200, { message: 'success API GET' })
const { data } = await api.get(baseUrl + '/api-get')
expect(data).toEqual({ message: 'success API GET' })
})
it('request POST with success', async () => {
nock(baseUrl)
.post('/api-post', {
user: 'test',
password: '1234',
})
.reply(200, { message: 'success API POST' })
const { data } = await api.post(baseUrl + '/api-post', {
user: 'test',
password: '1234',
})
expect(data).toEqual({ message: 'success API POST' })
})
it('request with error', async () => {
nock(baseUrl)
.get('/api-error')
.replyWithError('Error message')
const { error } = await api.get(baseUrl + '/api-error')
expect(error.message).toEqual('Error message')
})
it('with interceptor', async () => {
nock(baseUrl)
.post('/api-interceptor', {
user: 'test',
password: '1234',
})
.reply(200, { message: 'success API INTERCEPTOR' })
api.interceptors.response.use(
function(response) {
expect(response.status).toBe(200)
expect(response.data).toEqual({ message: 'success API INTERCEPTOR' })
return response
},
function(error) {
return Promise.reject(error)
},
)
api.interceptors.request.use(
function(request) {
expect(request.url).toBe('/api-interceptor')
expect(request.method).toBe('post')
expect(request.data).toBe({
user: 'test',
password: '1234',
})
return request
},
function(error) {
return Promise.reject(error)
},
)
await api.post(baseUrl + '/api-interceptor', {
user: 'test',
password: '1234',
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment