mirror of https://github.com/doccano/doccano.git
Hironsan
5 years ago
4 changed files with 109 additions and 0 deletions
Split View
Diff Options
-
1frontend/package.json
-
40frontend/services/request.js
-
56frontend/test/request.spec.js
-
12frontend/yarn.lock
@ -0,0 +1,40 @@ |
|||
import axios from 'axios' |
|||
|
|||
export default class Request { |
|||
constructor(baseURL) { |
|||
this.instance = axios.create({ |
|||
baseURL |
|||
}) |
|||
} |
|||
|
|||
request(method, url, data = {}, config = {}) { |
|||
return this.instance({ |
|||
method, |
|||
url, |
|||
data, |
|||
...config |
|||
}) |
|||
.then(response => response.data) |
|||
.catch(error => error) |
|||
} |
|||
|
|||
get(url, config = {}) { |
|||
return this.request('GET', url, config) |
|||
} |
|||
|
|||
post(url, data, config = {}) { |
|||
return this.request('POST', url, data, config) |
|||
} |
|||
|
|||
put(url, data, config = {}) { |
|||
return this.request('PUT', url, data, config) |
|||
} |
|||
|
|||
patch(url, data, config = {}) { |
|||
return this.request('PATCH', url, data, config) |
|||
} |
|||
|
|||
delete(url, config = {}) { |
|||
return this.request('DELETE', url, {}, config) |
|||
} |
|||
} |
@ -0,0 +1,56 @@ |
|||
import MockAdapter from 'axios-mock-adapter' |
|||
import Request from '@/services/request.js' |
|||
|
|||
describe('Request', () => { |
|||
const r = new Request('') |
|||
const mockAxios = new MockAdapter(r.instance) |
|||
|
|||
test('can get resources', async () => { |
|||
const data = [ |
|||
{ |
|||
id: 1, |
|||
title: 'title', |
|||
body: 'body' |
|||
} |
|||
] |
|||
mockAxios.onGet('/posts').reply(200, data) |
|||
const response = await r.get('/posts') |
|||
expect(response).toEqual(data) |
|||
}) |
|||
|
|||
test('can create a resource', async () => { |
|||
const data = { |
|||
title: 'foo', |
|||
body: 'bar' |
|||
} |
|||
mockAxios.onPost('/posts').reply(201, data) |
|||
const response = await r.post('/posts', data) |
|||
expect(response.title).toEqual(data.title) |
|||
}) |
|||
|
|||
test('can update a resource', async () => { |
|||
const data = { |
|||
id: 1, |
|||
title: 'foo', |
|||
body: 'bar' |
|||
} |
|||
mockAxios.onPut('/posts/1').reply(204, data) |
|||
const response = await r.put('/posts/1', data) |
|||
expect(response.title).toEqual(data.title) |
|||
}) |
|||
|
|||
test('can partially update a resource', async () => { |
|||
const data = { |
|||
title: 'foo' |
|||
} |
|||
mockAxios.onPatch('/posts/1').reply(200, data) |
|||
const response = await r.patch('/posts/1', data) |
|||
expect(response.title).toEqual(data.title) |
|||
}) |
|||
|
|||
test('can delete a resource', async () => { |
|||
mockAxios.onDelete('/posts/1').reply(204, {}) |
|||
const response = await r.delete('/posts/1') |
|||
expect(response).toEqual({}) |
|||
}) |
|||
}) |
Write
Preview
Loading…
Cancel
Save