apiRequestService.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { describe, expect, test, beforeEach } from 'vitest'
  2. import type { $Fetch } from 'nitropack'
  3. import ApiRequestService from '~/services/data/apiRequestService'
  4. import { HTTP_METHOD } from '~/types/enum/data'
  5. import type { AssociativeArray } from '~/types/data'
  6. let fetcher: $Fetch
  7. let apiRequestService: ApiRequestService
  8. beforeEach(() => {
  9. // @ts-ignore
  10. fetcher = vi.fn(
  11. (url: string, config: FetchOptions) => 'fetch_response',
  12. ) as $Fetch
  13. apiRequestService = new ApiRequestService(fetcher)
  14. })
  15. const mockedRequestMethod = (
  16. method: HTTP_METHOD,
  17. url: string,
  18. body: string | null = null,
  19. params: AssociativeArray | null = null,
  20. query: AssociativeArray | null = null,
  21. ) => 'a_response'
  22. describe('get', () => {
  23. test('simple call', async () => {
  24. // @ts-ignore
  25. apiRequestService.request = vi.fn(mockedRequestMethod)
  26. const result = await apiRequestService.get('https://myapi.com/api/item')
  27. expect(result).toEqual('a_response')
  28. // @ts-ignore
  29. expect(apiRequestService.request).toHaveBeenCalledWith(
  30. HTTP_METHOD.GET,
  31. 'https://myapi.com/api/item',
  32. null,
  33. null,
  34. null,
  35. )
  36. })
  37. test('with query and headers', async () => {
  38. // @ts-ignore
  39. apiRequestService.request = vi.fn(mockedRequestMethod)
  40. const result = await apiRequestService.get(
  41. 'https://myapi.com/api/item',
  42. {
  43. a: 1,
  44. },
  45. { b: 2 },
  46. )
  47. expect(result).toEqual('a_response')
  48. // @ts-ignore
  49. expect(apiRequestService.request).toHaveBeenCalledWith(
  50. HTTP_METHOD.GET,
  51. 'https://myapi.com/api/item',
  52. null,
  53. { a: 1 },
  54. { b: 2 },
  55. )
  56. })
  57. })
  58. describe('post', () => {
  59. test('simple call', async () => {
  60. // @ts-ignore
  61. apiRequestService.request = vi.fn(mockedRequestMethod)
  62. const result = await apiRequestService.post(
  63. 'https://myapi.com/api/item',
  64. 'request_body',
  65. )
  66. expect(result).toEqual('a_response')
  67. // @ts-ignore
  68. expect(apiRequestService.request).toHaveBeenCalledWith(
  69. HTTP_METHOD.POST,
  70. 'https://myapi.com/api/item',
  71. 'request_body',
  72. null,
  73. null,
  74. )
  75. })
  76. test('with query and headers', async () => {
  77. // @ts-ignore
  78. apiRequestService.request = vi.fn(mockedRequestMethod)
  79. const result = await apiRequestService.post(
  80. 'https://myapi.com/api/item',
  81. 'request_body',
  82. { a: 1 },
  83. { b: 2 },
  84. )
  85. expect(result).toEqual('a_response')
  86. // @ts-ignore
  87. expect(apiRequestService.request).toHaveBeenCalledWith(
  88. HTTP_METHOD.POST,
  89. 'https://myapi.com/api/item',
  90. 'request_body',
  91. { a: 1 },
  92. { b: 2 },
  93. )
  94. })
  95. })
  96. describe('put', () => {
  97. test('simple call', async () => {
  98. // @ts-ignore
  99. apiRequestService.request = vi.fn(mockedRequestMethod)
  100. const result = await apiRequestService.put(
  101. 'https://myapi.com/api/item',
  102. 'request_body'
  103. )
  104. expect(result).toEqual('a_response')
  105. // @ts-ignore
  106. expect(apiRequestService.request).toHaveBeenCalledWith(
  107. HTTP_METHOD.PUT,
  108. 'https://myapi.com/api/item',
  109. 'request_body',
  110. null,
  111. null,
  112. )
  113. })
  114. test('with query and headers', async () => {
  115. // @ts-ignore
  116. apiRequestService.request = vi.fn(mockedRequestMethod)
  117. const result = await apiRequestService.put(
  118. 'https://myapi.com/api/item',
  119. 'request_body',
  120. { a: 1 },
  121. { b: 2 },
  122. )
  123. expect(result).toEqual('a_response')
  124. // @ts-ignore
  125. expect(apiRequestService.request).toHaveBeenCalledWith(
  126. HTTP_METHOD.PUT,
  127. 'https://myapi.com/api/item',
  128. 'request_body',
  129. { a: 1 },
  130. { b: 2 },
  131. )
  132. })
  133. })
  134. describe('delete', () => {
  135. test('simple call', async () => {
  136. // @ts-ignore
  137. apiRequestService.request = vi.fn(mockedRequestMethod)
  138. const result = await apiRequestService.delete(
  139. 'https://myapi.com/api/item'
  140. )
  141. expect(result).toEqual('a_response')
  142. // @ts-ignore
  143. expect(apiRequestService.request).toHaveBeenCalledWith(
  144. HTTP_METHOD.DELETE,
  145. 'https://myapi.com/api/item',
  146. null,
  147. null,
  148. null,
  149. )
  150. })
  151. test('with query and headers', async () => {
  152. // @ts-ignore
  153. apiRequestService.request = vi.fn(mockedRequestMethod)
  154. const result = await apiRequestService.delete(
  155. 'https://myapi.com/api/item',
  156. { a: 1 },
  157. { b: 2 },
  158. )
  159. expect(result).toEqual('a_response')
  160. // @ts-ignore
  161. expect(apiRequestService.request).toHaveBeenCalledWith(
  162. HTTP_METHOD.DELETE,
  163. 'https://myapi.com/api/item',
  164. null,
  165. { a: 1 },
  166. { b: 2 },
  167. )
  168. })
  169. })
  170. describe('request', () => {
  171. test('simple call', async () => {
  172. // @ts-ignore
  173. const result = await apiRequestService.request(
  174. HTTP_METHOD.GET,
  175. 'https://myapi.com/api/item',
  176. )
  177. expect(result).toEqual('fetch_response')
  178. // @ts-ignore
  179. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  180. method: 'GET',
  181. })
  182. })
  183. test('post with body', async () => {
  184. // @ts-ignore
  185. const result = await apiRequestService.request(
  186. HTTP_METHOD.POST,
  187. 'https://myapi.com/api/item',
  188. 'a_body',
  189. )
  190. expect(result).toEqual('fetch_response')
  191. // @ts-ignore
  192. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  193. method: 'POST',
  194. body: 'a_body',
  195. })
  196. })
  197. test('put with body', async () => {
  198. // @ts-ignore
  199. const result = await apiRequestService.request(
  200. HTTP_METHOD.PUT,
  201. 'https://myapi.com/api/item',
  202. 'a_body',
  203. )
  204. expect(result).toEqual('fetch_response')
  205. // @ts-ignore
  206. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  207. method: 'PUT',
  208. body: 'a_body',
  209. })
  210. })
  211. test('get : body must be ignored even if provided', async () => {
  212. // @ts-ignore
  213. const result = await apiRequestService.request(
  214. HTTP_METHOD.GET,
  215. 'https://myapi.com/api/item',
  216. 'a_body',
  217. )
  218. expect(result).toEqual('fetch_response')
  219. // @ts-ignore
  220. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  221. method: 'GET',
  222. })
  223. })
  224. test('with query', async () => {
  225. // @ts-ignore
  226. const result = await apiRequestService.request(
  227. HTTP_METHOD.PUT,
  228. 'https://myapi.com/api/item',
  229. 'a_body',
  230. { a: 1 },
  231. )
  232. expect(result).toEqual('fetch_response')
  233. // @ts-ignore
  234. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  235. method: 'PUT',
  236. body: 'a_body',
  237. query: { a: 1 },
  238. })
  239. })
  240. })