|
1 | | -import { type AxiosResponse } from 'axios' |
2 | 1 | import { |
3 | 2 | describe, |
4 | | - it, |
5 | 3 | expect, |
| 4 | + it, |
6 | 5 | vi as jest, |
7 | 6 | } from 'vitest' |
| 7 | +import { useApi } from './global' |
8 | 8 | import { |
9 | | - type RequestHook, |
10 | | - type ResponseHook, |
11 | | - createApi, |
12 | | - createLazyton, |
| 9 | + copyHook, |
13 | 10 | onError, |
14 | 11 | onRequest, |
15 | 12 | onRequestError, |
16 | 13 | onResponse, |
17 | 14 | onResponseError, |
18 | 15 | removeHook, |
19 | 16 | resetHook, |
20 | | - setApi, |
21 | | - useApi, |
22 | | - copyHook, |
23 | | - QueuePriority, |
24 | | -} from '.' |
25 | | - |
26 | | -describe('useApi', () => { |
27 | | - it('should return same instance and same instance (singleton)', () => { |
28 | | - const a = useApi() |
29 | | - const b = useApi() |
30 | | - |
31 | | - expect(a).toStrictEqual(b) |
32 | | - }) |
33 | | -}) |
34 | | - |
35 | | -describe('setApi', () => { |
36 | | - it('should be able to replace global instance', () => { |
37 | | - const old = useApi() |
38 | | - const fresh = createApi() |
39 | | - |
40 | | - setApi(fresh) |
41 | | - |
42 | | - const last = useApi() |
43 | | - |
44 | | - expect(last).toStrictEqual(fresh) |
45 | | - expect(last).not.toStrictEqual(old) |
46 | | - }) |
47 | | -}) |
| 17 | +} from './hooks' |
| 18 | +import type { AxiosResponse } from 'axios' |
| 19 | +import type { RequestHook, ResponseHook } from './types' |
| 20 | +import { createApi } from './instance' |
48 | 21 |
|
49 | 22 | describe('Hooks utils', () => { |
50 | 23 | it('should be able to registering on-request hook using onRequest()', async () => { |
@@ -178,104 +151,3 @@ describe('copyHook', () => { |
178 | 151 | expect(fn).toBeCalledWith(expected) |
179 | 152 | }) |
180 | 153 | }) |
181 | | - |
182 | | -describe('Inherit instance', () => { |
183 | | - it('should be able to create new instance with same config', async () => { |
184 | | - const a = createApi({ baseURL: `${process.env.BASE_URL as string}/v1`, headers: { foo: 'bar' } }) |
185 | | - const b = a.create({ baseURL: `${process.env.BASE_URL as string}/v2` }) |
186 | | - |
187 | | - expect(b).not.toStrictEqual(a) |
188 | | - |
189 | | - const response = await b.get('/api/ping') |
190 | | - |
191 | | - expect(b.parent).toBe(a) |
192 | | - expect(response.config.headers?.foo).toBe('bar') |
193 | | - expect(response.data.data).toStrictEqual({ version: 'v2' }) |
194 | | - }) |
195 | | - |
196 | | - it('should be copy hook to new instance', async () => { |
197 | | - const a = createApi({ baseURL: `${process.env.BASE_URL as string}/v1`, headers: { foo: 'bar' } }) |
198 | | - const fn = jest.fn((config) => config) |
199 | | - |
200 | | - onRequest(fn, a) |
201 | | - |
202 | | - const b = a.create({ baseURL: `${process.env.BASE_URL as string}/v2` }) |
203 | | - const expected = expect.objectContaining({ |
204 | | - baseURL: `${process.env.BASE_URL as string}/v2`, |
205 | | - headers: expect.objectContaining({ foo: 'bar' }), |
206 | | - }) |
207 | | - |
208 | | - await b.get('/api/ping') |
209 | | - |
210 | | - expect(fn).toBeCalled() |
211 | | - expect(fn).toBeCalledWith(expected) |
212 | | - }) |
213 | | - |
214 | | - it('should prefixing baseUrl if prefixURL is present', async () => { |
215 | | - const a = createApi({ baseURL: process.env.BASE_URL, headers: { foo: 'bar' } }) |
216 | | - const b = a.create({ prefixURL: 'api' }) |
217 | | - const response = await b.get('user') |
218 | | - |
219 | | - expect(response.status).toBe(200) |
220 | | - expect(response.data.data).toBe('data-user') |
221 | | - }) |
222 | | - |
223 | | - it('should share queue in same parent instance', async () => { |
224 | | - const a = createApi({ baseURL: process.env.BASE_URL, queue: { worker: 1 } }) |
225 | | - const b = a.create({ prefixURL: 'v1/api' }) |
226 | | - const c = a.create({ prefixURL: 'v2/api' }) |
227 | | - |
228 | | - const result: number[] = [] |
229 | | - |
230 | | - await Promise.all([ |
231 | | - a.get('api/ping').then(() => { result.push(99) }), |
232 | | - b.get('ping').then(() => { result.push(1) }), |
233 | | - b.get('ping', { priority: QueuePriority.LOW }).then(() => { result.push(2) }), |
234 | | - c.get('ping').then(() => { result.push(3) }), |
235 | | - c.get('ping', { priority: QueuePriority.HIGH }).then(() => { result.push(4) }), |
236 | | - ]) |
237 | | - |
238 | | - expect(result).toStrictEqual([ |
239 | | - 4, |
240 | | - 99, |
241 | | - 1, |
242 | | - 3, |
243 | | - 2, |
244 | | - ]) |
245 | | - }) |
246 | | -}) |
247 | | - |
248 | | -describe('lazyton', () => { |
249 | | - it('should create lazy instance', () => { |
250 | | - const useLazy = createLazyton({ baseURL: '/v1' }) |
251 | | - |
252 | | - const a = useLazy() |
253 | | - const b = useLazy() |
254 | | - |
255 | | - expect(typeof useLazy).toBe('function') |
256 | | - expect(a).toStrictEqual(b) |
257 | | - }) |
258 | | - |
259 | | - it('should inherit hook from global', async () => { |
260 | | - const useLazy = createLazyton({ prefixURL: '/api' }) |
261 | | - const fn = jest.fn((config) => config) |
262 | | - |
263 | | - onRequest(fn) |
264 | | - |
265 | | - await useLazy().get('/ping') |
266 | | - |
267 | | - expect(fn).toBeCalled() |
268 | | - }) |
269 | | - |
270 | | - it('should create fresh instace if parameter fresh set to true', async () => { |
271 | | - const useLazy = createLazyton({ baseURL: `${process.env.BASE_URL as string}/api` }, true) |
272 | | - const fn = jest.fn((config) => config) |
273 | | - const a = useLazy() |
274 | | - |
275 | | - onRequest(fn) |
276 | | - |
277 | | - await a.get('/ping') |
278 | | - |
279 | | - expect(fn).not.toBeCalled() |
280 | | - }) |
281 | | -}) |
0 commit comments