| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- import { describe, expect, test, beforeEach } from 'vitest'
- import type { $Fetch } from 'nitropack'
- import type { FetchOptions } from 'ofetch'
- import ApiRequestService from '~/services/data/apiRequestService'
- import { HTTP_METHOD } from '~/types/enum/data'
- import type { AssociativeArray } from '~/types/data'
- let fetcher: $Fetch
- let apiRequestService: ApiRequestService
- beforeEach(() => {
- // @ts-ignore
- fetcher = vi.fn(
- (url: string, config: FetchOptions) => 'fetch_response',
- ) as $Fetch
- apiRequestService = new ApiRequestService(fetcher)
- })
- const mockedRequestMethod = (
- method: HTTP_METHOD,
- url: string,
- body: string | null = null,
- params: AssociativeArray | null = null,
- query: AssociativeArray | null = null,
- ) => 'a_response'
- describe('get', () => {
- test('simple call', async () => {
- // @ts-ignore
- apiRequestService.request = vi.fn(mockedRequestMethod)
- const result = await apiRequestService.get('https://myapi.com/api/item')
- expect(result).toEqual('a_response')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.GET,
- '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.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 },
- )
- })
- })
- describe('post', () => {
- test('simple call', async () => {
- // @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')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.POST,
- 'https://myapi.com/api/item',
- 'request_body',
- { a: 1 },
- { b: 2 },
- )
- })
- })
- describe('put', () => {
- test('simple call', async () => {
- // @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')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.PUT,
- 'https://myapi.com/api/item',
- 'request_body',
- { a: 1 },
- { b: 2 },
- )
- })
- })
- describe('delete', () => {
- test('simple call', async () => {
- // @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')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.DELETE,
- 'https://myapi.com/api/item',
- null,
- { a: 1 },
- { b: 2 },
- )
- })
- })
- describe('request', () => {
- test('simple call', async () => {
- // @ts-ignore
- const result = await apiRequestService.request(
- HTTP_METHOD.GET,
- 'https://myapi.com/api/item',
- )
- expect(result).toEqual('fetch_response')
- // @ts-ignore
- expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
- method: 'GET',
- headers: {
- Accept: 'application/ld+json',
- 'Content-Type': 'application/ld+json',
- },
- })
- })
- test('post with body', async () => {
- // @ts-ignore
- const result = await apiRequestService.request(
- HTTP_METHOD.POST,
- 'https://myapi.com/api/item',
- 'a_body',
- )
- expect(result).toEqual('fetch_response')
- // @ts-ignore
- expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
- method: 'POST',
- body: 'a_body',
- headers: {
- Accept: 'application/ld+json',
- 'Content-Type': 'application/ld+json',
- },
- })
- })
- test('put with body', async () => {
- // @ts-ignore
- const result = await apiRequestService.request(
- HTTP_METHOD.PUT,
- 'https://myapi.com/api/item',
- 'a_body',
- )
- expect(result).toEqual('fetch_response')
- // @ts-ignore
- expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
- method: 'PUT',
- body: 'a_body',
- headers: {
- Accept: 'application/ld+json',
- 'Content-Type': 'application/ld+json',
- },
- })
- })
- test('patch with body', async () => {
- // @ts-ignore
- const result = await apiRequestService.request(
- HTTP_METHOD.PATCH,
- 'https://myapi.com/api/item',
- 'a_body',
- )
- expect(result).toEqual('fetch_response')
- // @ts-ignore
- expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
- method: 'PATCH',
- body: 'a_body',
- headers: {
- Accept: 'application/ld+json',
- 'Content-Type': 'application/merge-patch+json',
- },
- })
- })
- test('get : body must be ignored even if provided', async () => {
- // @ts-ignore
- const result = await apiRequestService.request(
- HTTP_METHOD.GET,
- 'https://myapi.com/api/item',
- 'a_body',
- )
- expect(result).toEqual('fetch_response')
- // @ts-ignore
- expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
- method: 'GET',
- headers: {
- Accept: 'application/ld+json',
- 'Content-Type': 'application/ld+json',
- },
- })
- })
- test('with query', async () => {
- // @ts-ignore
- const result = await apiRequestService.request(
- HTTP_METHOD.PUT,
- 'https://myapi.com/api/item',
- 'a_body',
- { a: 1 },
- )
- expect(result).toEqual('fetch_response')
- // @ts-ignore
- expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
- method: 'PUT',
- body: 'a_body',
- query: { a: 1 },
- headers: {
- Accept: 'application/ld+json',
- 'Content-Type': 'application/ld+json',
- },
- })
- })
- })
|