Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Last active January 28, 2019 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DWboutin/1e3c8c04fd082c965f5d70ef325ce362 to your computer and use it in GitHub Desktop.
Save DWboutin/1e3c8c04fd082c965f5d70ef325ce362 to your computer and use it in GitHub Desktop.
const axios = {
get: jest.fn(() => Promise.resolve({ data: {} })),
post: jest.fn(() => Promise.resolve({ data: {} })),
put: jest.fn(() => Promise.resolve({ data: {} })),
create: jest.fn(() => axios),
defaults: {
adapter: {},
},
}
export default axios
import mockAxios from 'axios'
import AxiosClient from '../../src/Clients/AxiosClient'
describe('AxiosClient', () => {
test('calls axios.create on new instance', done => {
new AxiosClient({})
expect(mockAxios.create).toBeCalled()
done()
})
describe('AxiosClient mocked', () => {
test('on GET request, calls get() from axios correctly', done => {
const req = { url: '/', params: { test: 1234 } }
const client = new AxiosClient({})
client.get = jest
.fn()
.mockImplementationOnce(({ url, params }) =>
mockAxios.get(url, { params })
)
client.get(req)
expect(client.get).toBeCalled()
expect(client.get).toBeCalledWith({
url: req.url,
params: req.params,
})
expect(mockAxios.create).toBeCalled()
expect(mockAxios.get).toBeCalled()
expect(mockAxios.get).toBeCalledWith(req.url, { params: req.params })
done()
})
test('on POST request, calls post() from axios correctly', done => {
const req = { url: '/', data: { test: 1234 } }
const client = new AxiosClient({})
client.post = jest
.fn()
.mockImplementationOnce(({ url, data }) => mockAxios.post(url, { data }))
client.post(req)
expect(client.post).toBeCalled()
expect(client.post).toBeCalledWith({
url: req.url,
data: req.data,
})
expect(mockAxios.create).toBeCalled()
expect(mockAxios.post).toBeCalled()
expect(mockAxios.post).toBeCalledWith(req.url, { data: req.data })
done()
})
test('on PUT request, calls put() from axios correctly', done => {
const req = { url: '/', data: { test: 1234 } }
const client = new AxiosClient({})
client.put = jest
.fn()
.mockImplementationOnce(({ url, data }) => mockAxios.put(url, { data }))
client.put(req)
expect(client.put).toBeCalled()
expect(client.put).toBeCalledWith({
url: req.url,
data: req.data,
})
expect(mockAxios.create).toBeCalled()
expect(mockAxios.put).toBeCalled()
expect(mockAxios.put).toBeCalledWith(req.url, { data: req.data })
done()
})
})
describe('AxiosClient straight', () => {
test('on GET request, calls get() from axios correctly', done => {
const req = { url: '/', params: { test: 1234 } }
const client = new AxiosClient({})
client.get(req)
expect(mockAxios.create).toBeCalled()
expect(mockAxios.get).toBeCalled()
expect(mockAxios.get).toBeCalledWith(req.url, { params: req.params })
done()
})
test('on POST request, calls post() from axios correctly', done => {
const req = { url: '/', data: { test: 1234 } }
const client = new AxiosClient({})
client.post(req)
expect(mockAxios.create).toBeCalled()
expect(mockAxios.post).toBeCalled()
expect(mockAxios.post).toBeCalledWith(req.url, { data: req.data })
done()
})
test('on PUT request, calls put() from axios correctly', done => {
const req = { url: '/', data: { test: 1234 } }
const client = new AxiosClient({})
client.put(req)
expect(mockAxios.create).toBeCalled()
expect(mockAxios.put).toBeCalled()
expect(mockAxios.put).toBeCalledWith(req.url, { data: req.data })
done()
})
})
})
import axios, { AxiosInstance, AxiosPromise, AxiosRequestConfig } from 'axios'
import HttpRequestClient from '../Interfaces/HttpRequestClient'
class AxiosClient implements HttpRequestClient {
private engine: AxiosInstance
constructor(requestConfig: AxiosRequestConfig = {}) {
this.engine = axios.create(requestConfig)
}
get({ url, params = {} }: { url: string; params?: any }): AxiosPromise {
return this.engine.get(url, { params })
}
post({ url, data = {} }: { url: string; data?: any }): AxiosPromise {
return this.engine.post(url, { ...data })
}
put({ url, data = {} }: { url: string; data?: any }): AxiosPromise {
return this.engine.put(url, { ...data })
}
}
export default AxiosClient
----------------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------------------------|----------|----------|----------|----------|-------------------|
All files | 100 | 75 | 100 | 100 | |
Clients | 100 | 25 | 100 | 100 | |
AxiosClient.ts | 100 | 25 | 100 | 100 | 8,16,20 |Orange
Controllers | 100 | 100 | 100 | 100 | |
LeadsController.ts | 100 | 100 | 100 | 100 | |
MailingController.ts | 100 | 100 | 100 | 100 | |
Exceptions | 100 | 100 | 100 | 100 | |
MissingParameterException.ts | 100 | 100 | 100 | 100 | |
Factories | 100 | 100 | 100 | 100 | |
ErrorFactory.ts | 100 | 100 | 100 | 100 | |
ResponseFactory.ts | 100 | 100 | 100 | 100 | |
Guards | 100 | 100 | 100 | 100 | |
GuardAgainstMissingParameter.ts | 100 | 100 | 100 | 100 | |
Helpers | 100 | 100 | 100 | 100 | |
regexp.ts | 100 | 100 | 100 | 100 | |
Models | 100 | 100 | 100 | 100 | |
LeadsModel.ts | 100 | 100 | 100 | 100 | |
MailchimpModel.ts | 100 | 100 | 100 | 100 | |
Transformers | 100 | 100 | 100 | 100 | |
BodyParserTransformer.ts | 100 | 100 | 100 | 100 | |
LeadCreationTransformer.ts | 100 | 100 | 100 | 100 | |
MailchimpTransformer.ts | 100 | 100 | 100 | 100 | |
ValidationSchemas | 100 | 100 | 100 | 100 | |
LeadCreationSchema.ts | 100 | 100 | 100 | 100 | |
Validators | 100 | 100 | 100 | 100 | |
DataValidator.ts | 100 | 100 | 100 | 100 | |
----------------------------------|----------|----------|----------|----------|-------------------|
interface HttpRequestClient {
get: ({ url, params }: { url: string; params?: any }) => Promise<any>
post: ({ url, data }: { url: string; data?: any }) => Promise<any>
put: ({ url, data }: { url: string; data?: any }) => Promise<any>
}
export default HttpRequestClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment