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

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-03-16 09:54:19 +00:00
import { vi, describe, expect, it } from 'vitest';
import { onRequest, onResponseError } from 'src/boot/axios';
import { Notify } from 'quasar'
vi.mock('src/composables/useSession', () => ({
useSession: () => ({
getToken: () => 'DEFAULT_TOKEN'
}),
}));
vi.mock('src/router', () => ({}));
describe('Axios boot', () => {
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 () => {
Notify.create = vi.fn()
const error = {
response: {
status: 500
}
};
const result = onResponseError(error);
expect(result).rejects.toEqual(
expect.objectContaining(error)
);
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 () => {
Notify.create = vi.fn()
const error = {
response: {
status: 401,
data: {
error: {
message: 'Invalid user or password'
}
}
}
};
const result = onResponseError(error);
expect(result).rejects.toEqual(
expect.objectContaining(error)
);
expect(Notify.create).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Invalid user or password',
type: 'negative',
})
);
});
})
});