|
|
@@ -1,4 +1,5 @@
|
|
|
import { describe, expect, test, beforeEach } from 'vitest'
|
|
|
+import type { $Fetch } from 'nitropack'
|
|
|
import ApiRequestService from '~/services/data/apiRequestService'
|
|
|
import { HTTP_METHOD } from '~/types/enum/data'
|
|
|
import type { AssociativeArray } from '~/types/data'
|
|
|
@@ -27,9 +28,7 @@ describe('get', () => {
|
|
|
// @ts-ignore
|
|
|
apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
|
|
|
- const result = await apiRequestService.get('https://myapi.com/api/item', {
|
|
|
- a: 1,
|
|
|
- })
|
|
|
+ const result = await apiRequestService.get('https://myapi.com/api/item')
|
|
|
|
|
|
expect(result).toEqual('a_response')
|
|
|
// @ts-ignore
|
|
|
@@ -37,8 +36,30 @@ describe('get', () => {
|
|
|
HTTP_METHOD.GET,
|
|
|
'https://myapi.com/api/item',
|
|
|
null,
|
|
|
- { a: 1 },
|
|
|
null,
|
|
|
+ null,
|
|
|
+ )
|
|
|
+ })
|
|
|
+ test('with query and headers', async () => {
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
+
|
|
|
+ const result = await apiRequestService.get(
|
|
|
+ 'https://myapi.com/api/item',
|
|
|
+ {
|
|
|
+ a: 1,
|
|
|
+ },
|
|
|
+ { b: 2 },
|
|
|
+ )
|
|
|
+
|
|
|
+ expect(result).toEqual('a_response')
|
|
|
+ // @ts-ignore
|
|
|
+ expect(apiRequestService.request).toHaveBeenCalledWith(
|
|
|
+ HTTP_METHOD.GET,
|
|
|
+ 'https://myapi.com/api/item',
|
|
|
+ null,
|
|
|
+ { a: 1 },
|
|
|
+ { b: 2 },
|
|
|
)
|
|
|
})
|
|
|
})
|
|
|
@@ -48,10 +69,30 @@ describe('post', () => {
|
|
|
// @ts-ignore
|
|
|
apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
|
|
|
+ const result = await apiRequestService.post(
|
|
|
+ 'https://myapi.com/api/item',
|
|
|
+ 'request_body',
|
|
|
+ )
|
|
|
+
|
|
|
+ expect(result).toEqual('a_response')
|
|
|
+ // @ts-ignore
|
|
|
+ expect(apiRequestService.request).toHaveBeenCalledWith(
|
|
|
+ HTTP_METHOD.POST,
|
|
|
+ 'https://myapi.com/api/item',
|
|
|
+ 'request_body',
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ )
|
|
|
+ })
|
|
|
+ test('with query and headers', async () => {
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
+
|
|
|
const result = await apiRequestService.post(
|
|
|
'https://myapi.com/api/item',
|
|
|
'request_body',
|
|
|
{ a: 1 },
|
|
|
+ { b: 2 },
|
|
|
)
|
|
|
|
|
|
expect(result).toEqual('a_response')
|
|
|
@@ -61,7 +102,7 @@ describe('post', () => {
|
|
|
'https://myapi.com/api/item',
|
|
|
'request_body',
|
|
|
{ a: 1 },
|
|
|
- null,
|
|
|
+ { b: 2 },
|
|
|
)
|
|
|
})
|
|
|
})
|
|
|
@@ -71,10 +112,30 @@ describe('put', () => {
|
|
|
// @ts-ignore
|
|
|
apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
|
|
|
+ const result = await apiRequestService.put(
|
|
|
+ 'https://myapi.com/api/item',
|
|
|
+ 'request_body'
|
|
|
+ )
|
|
|
+
|
|
|
+ expect(result).toEqual('a_response')
|
|
|
+ // @ts-ignore
|
|
|
+ expect(apiRequestService.request).toHaveBeenCalledWith(
|
|
|
+ HTTP_METHOD.PUT,
|
|
|
+ 'https://myapi.com/api/item',
|
|
|
+ 'request_body',
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ )
|
|
|
+ })
|
|
|
+ test('with query and headers', async () => {
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
+
|
|
|
const result = await apiRequestService.put(
|
|
|
'https://myapi.com/api/item',
|
|
|
'request_body',
|
|
|
{ a: 1 },
|
|
|
+ { b: 2 },
|
|
|
)
|
|
|
|
|
|
expect(result).toEqual('a_response')
|
|
|
@@ -84,7 +145,7 @@ describe('put', () => {
|
|
|
'https://myapi.com/api/item',
|
|
|
'request_body',
|
|
|
{ a: 1 },
|
|
|
- null,
|
|
|
+ { b: 2 },
|
|
|
)
|
|
|
})
|
|
|
})
|
|
|
@@ -94,9 +155,28 @@ describe('delete', () => {
|
|
|
// @ts-ignore
|
|
|
apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
|
|
|
+ const result = await apiRequestService.delete(
|
|
|
+ 'https://myapi.com/api/item'
|
|
|
+ )
|
|
|
+
|
|
|
+ expect(result).toEqual('a_response')
|
|
|
+ // @ts-ignore
|
|
|
+ expect(apiRequestService.request).toHaveBeenCalledWith(
|
|
|
+ HTTP_METHOD.DELETE,
|
|
|
+ 'https://myapi.com/api/item',
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ )
|
|
|
+ })
|
|
|
+ test('with query and headers', async () => {
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.request = vi.fn(mockedRequestMethod)
|
|
|
+
|
|
|
const result = await apiRequestService.delete(
|
|
|
'https://myapi.com/api/item',
|
|
|
{ a: 1 },
|
|
|
+ { b: 2 },
|
|
|
)
|
|
|
|
|
|
expect(result).toEqual('a_response')
|
|
|
@@ -106,7 +186,7 @@ describe('delete', () => {
|
|
|
'https://myapi.com/api/item',
|
|
|
null,
|
|
|
{ a: 1 },
|
|
|
- null,
|
|
|
+ { b: 2 },
|
|
|
)
|
|
|
})
|
|
|
})
|