Skip to content

Commit be80782

Browse files
committed
test: add configure axios test
1 parent fb605cb commit be80782

3 files changed

Lines changed: 200 additions & 0 deletions

File tree

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
"wrapper"
1919
],
2020
"author": "Duye Chen",
21+
"contributors": [
22+
{
23+
"name": "Duye Chen",
24+
"email": "k078264@gmail.com"
25+
},
26+
{
27+
"name": "Richy Yang",
28+
"email": "w4567892015@gmail.com"
29+
}
30+
],
2131
"license": "MIT",
2232
"dependencies": {
2333
"axios": "^1.1.3"

test/configure-axios.test.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import should from 'should';
2+
import ApiWrapper from '../src/index';
3+
4+
describe('ApiWrapper with `configureAxios`', () => {
5+
let api: ApiWrapper;
6+
let reqInterceptorReqData;
7+
let resInterceptorResData;
8+
9+
it('should create an instance of ApiWrapper', () => {
10+
api = new ApiWrapper([
11+
{
12+
name: 'newPost',
13+
path: '/posts',
14+
method: 'post',
15+
},
16+
{
17+
name: 'getPosts',
18+
path: '/posts',
19+
method: 'get',
20+
},
21+
{
22+
name: 'getPostById',
23+
path: '/posts/:postId',
24+
method: 'get',
25+
},
26+
{
27+
name: 'updatePost',
28+
path: '/posts/:postId',
29+
method: 'patch',
30+
},
31+
{
32+
name: 'deletePost',
33+
path: '/posts/:postId',
34+
method: 'delete',
35+
},
36+
], {
37+
baseUrl: 'https://jsonplaceholder.typicode.com',
38+
configureAxios: (axios) => {
39+
axios.interceptors.request.use((request) => {
40+
reqInterceptorReqData = request.data;
41+
return request;
42+
});
43+
44+
axios.interceptors.response.use((response) => {
45+
resInterceptorResData = response.data;
46+
return response;
47+
});
48+
}
49+
});
50+
});
51+
52+
it('should do newPost', async () => {
53+
const reqData = {
54+
title: 'foo!!!!!!',
55+
body: 'bar!!',
56+
userId: 1
57+
};
58+
const resp = await api.newPost({
59+
data: reqData,
60+
});
61+
should(resp).be.an.Object();
62+
should(reqInterceptorReqData).be.have.containEql(reqData);
63+
should(resInterceptorResData).be.have.containEql(resp.data);
64+
});
65+
66+
it('should do updatePost', async () => {
67+
const resp = await api.updatePost({
68+
pathParams: {
69+
postId: '1',
70+
}
71+
});
72+
should(resp).be.an.Object();
73+
should(resp.config.url).be.equal('https://jsonplaceholder.typicode.com/posts/1');
74+
});
75+
76+
it('should do deletePost', async () => {
77+
const resp = await api.deletePost({
78+
pathParams: {
79+
postId: '1',
80+
}
81+
});
82+
should(resp).be.an.Object();
83+
should(resp.config.url).be.equal('https://jsonplaceholder.typicode.com/posts/1');
84+
});
85+
86+
it('should do getPosts', async () => {
87+
const resp = await api.getPosts();
88+
should(resp).be.an.Object();
89+
});
90+
91+
it('should do getPosts (with queryString)', async () => {
92+
const resp = await api.getPosts({
93+
queryString: {
94+
userId: '1'
95+
}
96+
});
97+
should(resp.data).be.an.Array();
98+
should(resp.config.url).be.equal('https://jsonplaceholder.typicode.com/posts?userId=1');
99+
});
100+
101+
it('should do getPostById', async () => {
102+
const resp = await api.getPostById({
103+
pathParams: {
104+
postId: '1',
105+
},
106+
});
107+
should(resp).be.an.Object();
108+
should(resp.config.url).be.equal('https://jsonplaceholder.typicode.com/posts/1');
109+
});
110+
111+
it('should do getPostById (array path params)', async () => {
112+
const resp = await api.getPostById({
113+
pathParams: [
114+
{field: 'postId', value: '1'},
115+
],
116+
});
117+
should(resp).be.an.Object();
118+
should(resp.config.url).be.equal('https://jsonplaceholder.typicode.com/posts/1');
119+
});
120+
});

test/index.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,51 @@ import ApiWrapper from '../src/index';
44
describe('ApiWrapper', () => {
55
let api: ApiWrapper;
66

7+
it('should thow an error of ApiWrapper due to missing baseUrl', () => {
8+
should(() =>
9+
new ApiWrapper([
10+
{
11+
name: 'newPost',
12+
path: '/posts',
13+
method: 'post',
14+
},
15+
], {})
16+
).throwError();
17+
});
18+
19+
it('should thow an error of ApiWrapper: "name only allow certain words and digits"', () => {
20+
should(() =>
21+
new ApiWrapper([
22+
{
23+
name: 'newPost.+',
24+
path: '/posts',
25+
method: 'post',
26+
},
27+
], {
28+
baseUrl: 'https://jsonplaceholder.typicode.com',
29+
})
30+
).throwError('name only allow certain words and digits');
31+
});
32+
33+
it('should thow an error of ApiWrapper: "Duplicated API: newPost"', () => {
34+
should(() =>
35+
new ApiWrapper([
36+
{
37+
name: 'newPost',
38+
path: '/posts',
39+
method: 'post',
40+
},
41+
{
42+
name: 'newPost',
43+
path: '/posts',
44+
method: 'post',
45+
},
46+
], {
47+
baseUrl: 'https://jsonplaceholder.typicode.com',
48+
})
49+
).throwError('Duplicated API: newPost');
50+
});
51+
752
it('should create an instance of ApiWrapper', () => {
853
api = new ApiWrapper([
954
{
@@ -47,6 +92,25 @@ describe('ApiWrapper', () => {
4792
should(resp).be.an.Object();
4893
});
4994

95+
it('should do newPost with auth', async () => {
96+
const resp = await api.newPost({
97+
data: {
98+
title: 'foo!!!!!!',
99+
body: 'bar!!',
100+
userId: 1
101+
},
102+
auth: {
103+
username: 'user',
104+
password: 'pass'
105+
}
106+
});
107+
should(resp).be.an.Object();
108+
should(resp.config.auth).be.containEql({
109+
username: 'user',
110+
password: 'pass'
111+
});
112+
});
113+
50114
it('should do updatePost', async () => {
51115
const resp = await api.updatePost({
52116
pathParams: {
@@ -72,6 +136,12 @@ describe('ApiWrapper', () => {
72136
should(resp).be.an.Object();
73137
});
74138

139+
it('should do getPosts but got error', async () => {
140+
await should(api.getPostById({
141+
pathParams: [{} as any]
142+
})).be.rejectedWith('pathParams is invalid');
143+
});
144+
75145
it('should do getPosts (with queryString)', async () => {
76146
const resp = await api.getPosts({
77147
queryString: {

0 commit comments

Comments
 (0)