0
0
Fork 0
salix-front-mindshore-fork2/test/vitest/__tests__/boot/axios.spec.js

81 lines
2.5 KiB
JavaScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { setupAxios, onRequest, onResponseError } from 'src/boot/axios';
import { useSession } from 'src/composables/useSession';
import useNotify from 'src/composables/useNotify';
import { useStateQueryStore } from 'src/stores/useStateQueryStore';
vi.mock('src/composables/useSession');
vi.mock('src/composables/useNotify');
vi.mock('src/stores/useStateQueryStore');
describe('Axios boot', () => {
let sessionMock, notifyMock, stateQueryMock;
beforeEach(() => {
sessionMock = {
getToken: vi.fn().mockReturnValue('DEFAULT_TOKEN'),
isLoggedIn: vi.fn().mockReturnValue(true),
destroy: vi.fn(),
};
notifyMock = {
notify: vi.fn(),
};
stateQueryMock = {
add: vi.fn(),
remove: vi.fn(),
};
useSession.mockReturnValue(sessionMock);
useNotify.mockReturnValue(notifyMock);
useStateQueryStore.mockReturnValue(stateQueryMock);
setupAxios();
});
describe('onRequest()', async () => {
it('should set the "Authorization" property on the headers', async () => {
const config = { headers: {} };
const resultConfig = onRequest(config);
expect(resultConfig).toEqual(
expect.objectContaining({
headers: {
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);
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);
expect(result).rejects.toEqual(expect.objectContaining(error));
});
});
});