apiRequestService.test.ts 4.8 KB

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