apiRequestService.test.ts 4.9 KB

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