apiRequestService.test.ts 7.5 KB

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