|
| 1 | +import { api, apiRaw } from '@/services/apiClient'; |
| 2 | +import { appAxios } from '@/services/interceptors'; |
| 3 | + |
| 4 | +import type { Mock } from 'vitest'; |
| 5 | + |
| 6 | +vi.mock('@/services/interceptors', () => ({ |
| 7 | + appAxios: vi.fn() |
| 8 | +})); |
| 9 | + |
| 10 | +const mockAxios = { |
| 11 | + get: vi.fn(), |
| 12 | + post: vi.fn(), |
| 13 | + put: vi.fn(), |
| 14 | + patch: vi.fn(), |
| 15 | + delete: vi.fn() |
| 16 | +}; |
| 17 | + |
| 18 | +describe('api wrapper', () => { |
| 19 | + beforeEach(() => { |
| 20 | + vi.clearAllMocks(); |
| 21 | + (appAxios as unknown as Mock).mockReturnValue(mockAxios); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('api (unwrapped .data)', () => { |
| 25 | + it('get unwraps response data', async () => { |
| 26 | + mockAxios.get.mockResolvedValue({ data: 'result' }); |
| 27 | + |
| 28 | + const res = await api.get<string>('/test'); |
| 29 | + |
| 30 | + expect(mockAxios.get).toHaveBeenCalledWith('/test', undefined); |
| 31 | + expect(res).toBe('result'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('get forwards config', async () => { |
| 35 | + mockAxios.get.mockResolvedValue({ data: 'ok' }); |
| 36 | + |
| 37 | + const config = { params: { a: 1 } }; |
| 38 | + |
| 39 | + await api.get('/test', config); |
| 40 | + |
| 41 | + expect(mockAxios.get).toHaveBeenCalledWith('/test', config); |
| 42 | + }); |
| 43 | + |
| 44 | + it('post unwraps response data', async () => { |
| 45 | + mockAxios.post.mockResolvedValue({ data: { id: 1 } }); |
| 46 | + |
| 47 | + const res = await api.post<{ id: number }>('/test', { foo: 'bar' }); |
| 48 | + |
| 49 | + expect(mockAxios.post).toHaveBeenCalledWith('/test', { foo: 'bar' }, undefined); |
| 50 | + expect(res).toEqual({ id: 1 }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('put unwraps response data', async () => { |
| 54 | + mockAxios.put.mockResolvedValue({ data: true }); |
| 55 | + |
| 56 | + const res = await api.put<boolean>('/test', { x: 1 }); |
| 57 | + |
| 58 | + expect(res).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('patch unwraps response data', async () => { |
| 62 | + mockAxios.patch.mockResolvedValue({ data: 'patched' }); |
| 63 | + |
| 64 | + const res = await api.patch<string>('/test', { x: 1 }); |
| 65 | + |
| 66 | + expect(res).toBe('patched'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('delete unwraps response data', async () => { |
| 70 | + mockAxios.delete.mockResolvedValue({ data: 'deleted' }); |
| 71 | + |
| 72 | + const res = await api.delete<string>('/test'); |
| 73 | + |
| 74 | + expect(mockAxios.delete).toHaveBeenCalledWith('/test', undefined); |
| 75 | + expect(res).toBe('deleted'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('apiRaw (no transformation)', () => { |
| 80 | + it('get returns raw axios response', async () => { |
| 81 | + const axiosResponse = { data: 'raw', status: 200 }; |
| 82 | + |
| 83 | + mockAxios.get.mockResolvedValue(axiosResponse); |
| 84 | + |
| 85 | + const res = await apiRaw.get('/test'); |
| 86 | + |
| 87 | + expect(res).toBe(axiosResponse); |
| 88 | + }); |
| 89 | + |
| 90 | + it('post returns raw axios response', async () => { |
| 91 | + const axiosResponse = { data: 123 }; |
| 92 | + |
| 93 | + mockAxios.post.mockResolvedValue(axiosResponse); |
| 94 | + |
| 95 | + const res = await apiRaw.post('/test', { a: 1 }); |
| 96 | + |
| 97 | + expect(res).toBe(axiosResponse); |
| 98 | + }); |
| 99 | + |
| 100 | + it('put returns raw axios response', async () => { |
| 101 | + const axiosResponse = { data: true }; |
| 102 | + |
| 103 | + mockAxios.put.mockResolvedValue(axiosResponse); |
| 104 | + |
| 105 | + const res = await apiRaw.put('/test', { a: 1 }); |
| 106 | + |
| 107 | + expect(res).toBe(axiosResponse); |
| 108 | + }); |
| 109 | + |
| 110 | + it('patch returns raw axios response', async () => { |
| 111 | + const axiosResponse = { data: 'ok' }; |
| 112 | + |
| 113 | + mockAxios.patch.mockResolvedValue(axiosResponse); |
| 114 | + |
| 115 | + const res = await apiRaw.patch('/test', { a: 1 }); |
| 116 | + |
| 117 | + expect(res).toBe(axiosResponse); |
| 118 | + }); |
| 119 | + |
| 120 | + it('delete returns raw axios response', async () => { |
| 121 | + const axiosResponse = { data: 'done' }; |
| 122 | + |
| 123 | + mockAxios.delete.mockResolvedValue(axiosResponse); |
| 124 | + |
| 125 | + const res = await apiRaw.delete('/test'); |
| 126 | + |
| 127 | + expect(res).toBe(axiosResponse); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('edge cases', () => { |
| 132 | + it('handles undefined body in post', async () => { |
| 133 | + mockAxios.post.mockResolvedValue({ data: 'ok' }); |
| 134 | + |
| 135 | + await api.post('/test'); |
| 136 | + |
| 137 | + expect(mockAxios.post).toHaveBeenCalledWith('/test', undefined, undefined); |
| 138 | + }); |
| 139 | + |
| 140 | + it('handles config passed to delete', async () => { |
| 141 | + mockAxios.delete.mockResolvedValue({ data: 'ok' }); |
| 142 | + |
| 143 | + const config = { params: { force: true } }; |
| 144 | + |
| 145 | + await api.delete('/test', config); |
| 146 | + |
| 147 | + expect(mockAxios.delete).toHaveBeenCalledWith('/test', config); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments