You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

  1. import MockAdapter from 'axios-mock-adapter'
  2. import ApiService from '@/services/api.service'
  3. describe('Request', () => {
  4. const r = new ApiService('')
  5. const mockAxios = new MockAdapter(r.instance)
  6. test('can get resources', async () => {
  7. const data = [
  8. {
  9. id: 1,
  10. title: 'title',
  11. body: 'body'
  12. }
  13. ]
  14. mockAxios.onGet('/posts').reply(200, data)
  15. const response = await r.get('/posts')
  16. expect(response).toEqual(data)
  17. })
  18. test('can create a resource', async () => {
  19. const data = {
  20. title: 'foo',
  21. body: 'bar'
  22. }
  23. mockAxios.onPost('/posts').reply(201, data)
  24. const response = await r.post('/posts', data)
  25. expect(response.title).toEqual(data.title)
  26. })
  27. test('can update a resource', async () => {
  28. const data = {
  29. id: 1,
  30. title: 'foo',
  31. body: 'bar'
  32. }
  33. mockAxios.onPut('/posts/1').reply(204, data)
  34. const response = await r.put('/posts/1', data)
  35. expect(response.title).toEqual(data.title)
  36. })
  37. test('can partially update a resource', async () => {
  38. const data = {
  39. title: 'foo'
  40. }
  41. mockAxios.onPatch('/posts/1').reply(200, data)
  42. const response = await r.patch('/posts/1', data)
  43. expect(response.title).toEqual(data.title)
  44. })
  45. test('can delete a resource', async () => {
  46. mockAxios.onDelete('/posts/1').reply(204, {})
  47. const response = await r.delete('/posts/1')
  48. expect(response).toEqual({})
  49. })
  50. })