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

84 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-04-27 08:40:00 +00:00
import { Notify } from 'quasar';
2023-03-16 09:54:19 +00:00
import { onRequest, onResponseError } from 'src/boot/axios';
2023-04-27 08:40:00 +00:00
import { describe, expect, it, vi } from 'vitest';
2023-03-16 09:54:19 +00:00
vi.mock('src/composables/useSession', () => ({
useSession: () => ({
2023-04-27 08:40:00 +00:00
getToken: () => 'DEFAULT_TOKEN',
isLoggedIn: () => vi.fn(),
destroy: () => vi.fn(),
2024-10-18 09:03:43 +00:00
}),
2023-03-16 09:54:19 +00:00
}));
2024-10-18 09:03:43 +00:00
vi.mock('src/stores/useStateQueryStore', () => ({
useStateQueryStore: () => ({
add: () => vi.fn(),
remove: () => vi.fn(),
}),
}));
2023-03-16 09:54:19 +00:00
2024-10-18 09:03:43 +00:00
describe('Axios boot', () => {
2023-03-16 09:54:19 +00:00
describe('onRequest()', async () => {
it('should set the "Authorization" property on the headers', async () => {
const config = { headers: {} };
const resultConfig = onRequest(config);
2024-10-18 09:03:43 +00:00
expect(resultConfig).toEqual(
expect.objectContaining({
headers: {
Authorization: 'DEFAULT_TOKEN',
},
})
);
2023-03-16 09:54:19 +00:00
});
2024-10-18 09:03:43 +00:00
});
2023-03-16 09:54:19 +00:00
describe('onResponseError()', async () => {
it('should call to the Notify plugin with a message error for an status code "500"', async () => {
2024-10-18 09:03:43 +00:00
Notify.create = vi.fn();
2023-03-16 09:54:19 +00:00
const error = {
response: {
2024-10-18 09:03:43 +00:00
status: 500,
},
2023-03-16 09:54:19 +00:00
};
const result = onResponseError(error);
2024-10-18 09:03:43 +00:00
expect(result).rejects.toEqual(expect.objectContaining(error));
2023-03-16 09:54:19 +00:00
expect(Notify.create).toHaveBeenCalledWith(
expect.objectContaining({
message: 'An internal server error has ocurred',
type: 'negative',
})
);
});
it('should call to the Notify plugin with a message from the response property', async () => {
2024-10-18 09:03:43 +00:00
Notify.create = vi.fn();
2023-03-16 09:54:19 +00:00
const error = {
response: {
status: 401,
data: {
error: {
2024-10-18 09:03:43 +00:00
message: 'Invalid user or password',
},
},
},
2023-03-16 09:54:19 +00:00
};
const result = onResponseError(error);
2024-10-18 09:03:43 +00:00
expect(result).rejects.toEqual(expect.objectContaining(error));
2023-03-16 09:54:19 +00:00
expect(Notify.create).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Invalid user or password',
type: 'negative',
})
);
});
2024-10-18 09:03:43 +00:00
});
2023-03-16 09:54:19 +00:00
});