apiRequestService.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. )
  36. })
  37. })
  38. describe('post', () => {
  39. test('simple call', async () => {
  40. // @ts-ignore
  41. apiRequestService.request = vi.fn(mockedRequestMethod)
  42. const result = await apiRequestService.post(
  43. 'https://myapi.com/api/item',
  44. 'request_body',
  45. { a: 1 },
  46. )
  47. expect(result).toEqual('a_response')
  48. // @ts-ignore
  49. expect(apiRequestService.request).toHaveBeenCalledWith(
  50. HTTP_METHOD.POST,
  51. 'https://myapi.com/api/item',
  52. 'request_body',
  53. { a: 1 },
  54. )
  55. })
  56. })
  57. describe('put', () => {
  58. test('simple call', async () => {
  59. // @ts-ignore
  60. apiRequestService.request = vi.fn(mockedRequestMethod)
  61. const result = await apiRequestService.put(
  62. 'https://myapi.com/api/item',
  63. 'request_body',
  64. { a: 1 },
  65. )
  66. expect(result).toEqual('a_response')
  67. // @ts-ignore
  68. expect(apiRequestService.request).toHaveBeenCalledWith(
  69. HTTP_METHOD.PUT,
  70. 'https://myapi.com/api/item',
  71. 'request_body',
  72. { a: 1 },
  73. )
  74. })
  75. })
  76. describe('delete', () => {
  77. test('simple call', async () => {
  78. // @ts-ignore
  79. apiRequestService.request = vi.fn(mockedRequestMethod)
  80. const result = await apiRequestService.delete(
  81. 'https://myapi.com/api/item',
  82. { a: 1 },
  83. )
  84. expect(result).toEqual('a_response')
  85. // @ts-ignore
  86. expect(apiRequestService.request).toHaveBeenCalledWith(
  87. HTTP_METHOD.DELETE,
  88. 'https://myapi.com/api/item',
  89. null,
  90. { a: 1 },
  91. )
  92. })
  93. })
  94. describe('request', () => {
  95. test('simple call', async () => {
  96. // @ts-ignore
  97. const result = await apiRequestService.request(
  98. HTTP_METHOD.GET,
  99. 'https://myapi.com/api/item',
  100. )
  101. expect(result).toEqual('fetch_response')
  102. // @ts-ignore
  103. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  104. method: 'GET',
  105. })
  106. })
  107. test('post with body', async () => {
  108. // @ts-ignore
  109. const result = await apiRequestService.request(
  110. HTTP_METHOD.POST,
  111. 'https://myapi.com/api/item',
  112. 'a_body',
  113. )
  114. expect(result).toEqual('fetch_response')
  115. // @ts-ignore
  116. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  117. method: 'POST',
  118. body: 'a_body',
  119. })
  120. })
  121. test('put with body', async () => {
  122. // @ts-ignore
  123. const result = await apiRequestService.request(
  124. HTTP_METHOD.PUT,
  125. 'https://myapi.com/api/item',
  126. 'a_body',
  127. )
  128. expect(result).toEqual('fetch_response')
  129. // @ts-ignore
  130. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  131. method: 'PUT',
  132. body: 'a_body',
  133. })
  134. })
  135. test('get : body must be ignored even if provided', async () => {
  136. // @ts-ignore
  137. const result = await apiRequestService.request(
  138. HTTP_METHOD.GET,
  139. 'https://myapi.com/api/item',
  140. 'a_body',
  141. )
  142. expect(result).toEqual('fetch_response')
  143. // @ts-ignore
  144. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  145. method: 'GET',
  146. })
  147. })
  148. test('with query', async () => {
  149. // @ts-ignore
  150. const result = await apiRequestService.request(
  151. HTTP_METHOD.PUT,
  152. 'https://myapi.com/api/item',
  153. 'a_body',
  154. { a: 1 },
  155. )
  156. expect(result).toEqual('fetch_response')
  157. // @ts-ignore
  158. expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
  159. method: 'PUT',
  160. body: 'a_body',
  161. query: { a: 1 },
  162. })
  163. })
  164. })