Skip to content

Commit 6f1d5c8

Browse files
committed
test: ⚡ test post proxy
1 parent 82ff6c5 commit 6f1d5c8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/__tests__/post-proxy.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Axios from 'axios'
2+
import BaseProxy from '../core/BaseProxy'
3+
import PostProxy from '../core/PostPorxy'
4+
import MockAdapter from 'axios-mock-adapter'
5+
import BaseTransformer from '../core/BaseTransformer'
6+
import PaginationTransformer from '../core/PaginationTransformer'
7+
8+
let proxy: PostProxy
9+
let mockAdapter: MockAdapter
10+
11+
describe('PostProxy', () => {
12+
beforeEach(() => {
13+
const axios = Axios.create({ baseURL: 'https://mock-api.test' })
14+
BaseProxy.$http = axios
15+
proxy = new PostProxy()
16+
mockAdapter = new MockAdapter(axios)
17+
mockAdapter.reset()
18+
})
19+
20+
it('it should get tags by post id', async () => {
21+
const items = {
22+
data: [{ name: 'Chantouch', post_id: 1 }],
23+
meta: {
24+
pagination: { count: 1, current_page: 1, perPage: 20 },
25+
include: [],
26+
},
27+
}
28+
mockAdapter.onGet('/posts/1/tags').reply(200, items)
29+
const { data, meta = {} } = await proxy.tags(1)
30+
const all = {
31+
items: BaseTransformer.fetchCollection(data),
32+
pagination: PaginationTransformer.fetch(meta),
33+
}
34+
expect(meta).toHaveProperty('pagination')
35+
expect(data.length).toEqual(1)
36+
expect(all.pagination.page).toEqual(1)
37+
})
38+
})

src/core/PostPorxy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class PostProxy extends BaseProxy {
44
constructor(parameters = {}) {
55
super('posts', parameters)
66
}
7+
8+
tags<T>(id: string | number) {
9+
return this.submit<T>('get', `${id}/tags`)
10+
}
711
}
812

913
export default PostProxy

0 commit comments

Comments
 (0)