apiRequestService.test.ts 4.7 KB

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