apiRequestService.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { describe, expect, test, beforeEach } from 'vitest'
  2. import ApiRequestService from '~/services/data/apiRequestService'
  3. import { HTTP_METHOD } from '~/types/enum/data'
  4. import type { AssociativeArray } from '~/types/data'
  5. let fetcher: $Fetch
  6. let apiRequestService: ApiRequestService
  7. beforeEach(() => {
  8. // @ts-ignore
  9. fetcher = vi.fn(
  10. (url: string, config: FetchOptions) => 'fetch_response',
  11. ) as $Fetch
  12. apiRequestService = new ApiRequestService(fetcher)
  13. })
  14. const mockedRequestMethod = (
  15. method: HTTP_METHOD,
  16. url: string,
  17. body: string | null = null,
  18. params: AssociativeArray | null = null,
  19. query: AssociativeArray | null = null,
  20. ) => 'a_response'
  21. describe('get', () => {
  22. test('simple call', async () => {
  23. // @ts-ignore
  24. apiRequestService.request = vi.fn(mockedRequestMethod)
  25. const result = await apiRequestService.get('https://myapi.com/api/item', {
  26. a: 1,
  27. })
  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. { a: 1 },
  35. null,
  36. )
  37. })
  38. })
  39. describe('post', () => {
  40. test('simple call', async () => {
  41. // @ts-ignore
  42. apiRequestService.request = vi.fn(mockedRequestMethod)
  43. const result = await apiRequestService.post(
  44. 'https://myapi.com/api/item',
  45. 'request_body',
  46. { a: 1 },
  47. )
  48. expect(result).toEqual('a_response')
  49. // @ts-ignore
  50. expect(apiRequestService.request).toHaveBeenCalledWith(
  51. HTTP_METHOD.POST,
  52. 'https://myapi.com/api/item',
  53. 'request_body',
  54. { a: 1 },
  55. null,
  56. )
  57. })
  58. })
  59. describe('put', () => {
  60. test('simple call', async () => {
  61. // @ts-ignore
  62. apiRequestService.request = vi.fn(mockedRequestMethod)
  63. const result = await apiRequestService.put(
  64. 'https://myapi.com/api/item',
  65. 'request_body',
  66. { a: 1 },
  67. )
  68. expect(result).toEqual('a_response')
  69. // @ts-ignore
  70. expect(apiRequestService.request).toHaveBeenCalledWith(
  71. HTTP_METHOD.PUT,
  72. 'https://myapi.com/api/item',
  73. 'request_body',
  74. { a: 1 },
  75. null,
  76. )
  77. })
  78. })
  79. describe('delete', () => {
  80. test('simple call', async () => {
  81. // @ts-ignore
  82. apiRequestService.request = vi.fn(mockedRequestMethod)
  83. const result = await apiRequestService.delete(
  84. 'https://myapi.com/api/item',
  85. { a: 1 },
  86. )
  87. expect(result).toEqual('a_response')
  88. // @ts-ignore
  89. expect(apiRequestService.request).toHaveBeenCalledWith(
  90. HTTP_METHOD.DELETE,
  91. 'https://myapi.com/api/item',
  92. null,
  93. { a: 1 },
  94. null,
  95. )
  96. })
  97. })
  98. describe('request', () => {
  99. test('simple call', async () => {
  100. // @ts-ignore
  101. const result = await apiRequestService.request(
  102. HTTP_METHOD.GET,
  103. 'https://myapi.com/api/item',
  104. )
  105. expect(result).toEqual('fetch_response')
  106. // @ts-ignore
  107. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  108. method: 'GET',
  109. })
  110. })
  111. test('post with body', async () => {
  112. // @ts-ignore
  113. const result = await apiRequestService.request(
  114. HTTP_METHOD.POST,
  115. 'https://myapi.com/api/item',
  116. 'a_body',
  117. )
  118. expect(result).toEqual('fetch_response')
  119. // @ts-ignore
  120. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  121. method: 'POST',
  122. body: 'a_body',
  123. })
  124. })
  125. test('put with body', async () => {
  126. // @ts-ignore
  127. const result = await apiRequestService.request(
  128. HTTP_METHOD.PUT,
  129. 'https://myapi.com/api/item',
  130. 'a_body',
  131. )
  132. expect(result).toEqual('fetch_response')
  133. // @ts-ignore
  134. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  135. method: 'PUT',
  136. body: 'a_body',
  137. })
  138. })
  139. test('get : body must be ignored even if provided', async () => {
  140. // @ts-ignore
  141. const result = await apiRequestService.request(
  142. HTTP_METHOD.GET,
  143. 'https://myapi.com/api/item',
  144. 'a_body',
  145. )
  146. expect(result).toEqual('fetch_response')
  147. // @ts-ignore
  148. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  149. method: 'GET',
  150. })
  151. })
  152. test('with query', async () => {
  153. // @ts-ignore
  154. const result = await apiRequestService.request(
  155. HTTP_METHOD.PUT,
  156. 'https://myapi.com/api/item',
  157. 'a_body',
  158. { a: 1 },
  159. )
  160. expect(result).toEqual('fetch_response')
  161. // @ts-ignore
  162. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  163. method: 'PUT',
  164. body: 'a_body',
  165. query: { a: 1 },
  166. })
  167. })
  168. })