import { onRequest, onResponseError } from 'src/boot/axios'; import { describe, expect, it, vi } from 'vitest'; vi.mock('src/composables/useSession', () => ({ useSession: () => ({ getToken: () => 'DEFAULT_TOKEN', isLoggedIn: () => vi.fn(), destroy: () => vi.fn(), }), })); // Mock axios vi.mock('axios', () => ({ default: { create: vi.fn(() => ({ interceptors: { request: { use: vi.fn() }, response: { use: vi.fn() }, }, })), interceptors: { request: { use: vi.fn() }, response: { use: vi.fn() }, }, defaults: { baseURL: '', }, }, })); vi.mock('src/router', () => ({ Router: { push: vi.fn(), }, })); vi.mock('src/stores/useStateQueryStore', () => ({ useStateQueryStore: () => ({ add: () => vi.fn(), remove: () => vi.fn(), }), })); describe('Axios boot', () => { describe('onRequest()', async () => { it('should set the "Authorization" property on the headers', async () => { const config = { headers: {} }; localStorage.setItem('token', 'DEFAULT_TOKEN'); const resultConfig = onRequest(config); expect(resultConfig).toEqual( expect.objectContaining({ headers: { 'Accept-Language': 'en-US', Authorization: 'DEFAULT_TOKEN', }, }), ); }); }); describe('onResponseError()', async () => { it('should call to the Notify plugin with a message error for an status code "500"', async () => { const error = { response: { status: 500, }, }; const result = onResponseError(error); await expect(result).rejects.toEqual(expect.objectContaining(error)); }); it('should call to the Notify plugin with a message from the response property', async () => { const error = { response: { status: 401, data: { error: { message: 'Invalid user or password', }, }, }, }; const result = onResponseError(error); await expect(result).rejects.toEqual(expect.objectContaining(error)); }); }); });