| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import {describe, expect, test} from 'vitest'
- import ApiRequestService from "~/services/data/apiRequestService";
- import {$Fetch, FetchOptions} from "ohmyfetch";
- import {HTTP_METHOD} from "~/types/enum/data";
- import {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', { a: 1 })
- expect(result).toEqual('a_response')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.GET, 'https://myapi.com/api/item', null, { a: 1 }
- )
- })
- })
- 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',
- { a: 1 }
- )
- expect(result).toEqual('a_response')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.POST,
- 'https://myapi.com/api/item',
- 'request_body',
- { a: 1 }
- )
- })
- })
- 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',
- { a: 1 }
- )
- expect(result).toEqual('a_response')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.PUT,
- 'https://myapi.com/api/item',
- 'request_body',
- { a: 1 }
- )
- })
- })
- describe('delete', () => {
- test('simple call', async () => {
- // @ts-ignore
- apiRequestService.request = vi.fn(mockedRequestMethod)
- const result = await apiRequestService.delete(
- 'https://myapi.com/api/item',
- { a: 1 }
- )
- expect(result).toEqual('a_response')
- // @ts-ignore
- expect(apiRequestService.request).toHaveBeenCalledWith(
- HTTP_METHOD.DELETE,
- 'https://myapi.com/api/item',
- null,
- { a: 1 }
- )
- })
- })
- 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'})
- })
- 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'})
- })
- 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'})
- })
- 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'})
- })
- 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 },
- }
- )
- })
- })
|