|
| 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 | +}); |
0 commit comments