apiRequestService.test.ts 5.1 KB

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