Added axios unit test
gitea/salix-front/pipeline/head There was a failure building this commit
Details
gitea/salix-front/pipeline/head There was a failure building this commit
Details
Refs #5420
This commit is contained in:
parent
a876dc0e0c
commit
012a1ec77f
|
@ -73,3 +73,8 @@ const onResponseError = (error) => {
|
|||
|
||||
axios.interceptors.request.use(onRequest, onRequestError);
|
||||
axios.interceptors.response.use(onResponse, onResponseError);
|
||||
|
||||
export {
|
||||
onRequest,
|
||||
onResponseError
|
||||
}
|
||||
|
|
|
@ -1,84 +0,0 @@
|
|||
import { vi, describe, expect, it, beforeAll } from 'vitest';
|
||||
import { createWrapper } from 'app/test/vitest/helper';
|
||||
import App from 'src/App.vue';
|
||||
import { useSession } from 'src/composables/useSession';
|
||||
|
||||
const mockLoggedIn = vi.fn();
|
||||
const mockDestroy = vi.fn();
|
||||
const session = useSession();
|
||||
|
||||
vi.mock('src/composables/useSession', () => ({
|
||||
useSession: () => ({
|
||||
isLoggedIn: mockLoggedIn,
|
||||
destroy: mockDestroy,
|
||||
}),
|
||||
}));
|
||||
|
||||
describe.skip('App', () => {
|
||||
let vm;
|
||||
|
||||
beforeAll(() => {
|
||||
const options = {
|
||||
global: {
|
||||
stubs: ['router-view'],
|
||||
},
|
||||
};
|
||||
vm = createWrapper(App, options).vm;
|
||||
});
|
||||
|
||||
it('should return a login error message', async () => {
|
||||
vi.spyOn(vm.quasar, 'notify');
|
||||
|
||||
session.isLoggedIn.mockReturnValue(false);
|
||||
|
||||
const response = {
|
||||
response: {
|
||||
status: 401,
|
||||
data: {
|
||||
error: {
|
||||
message: 'Invalid username or password',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(vm.onResponseError(response)).rejects.toEqual(
|
||||
expect.objectContaining(response)
|
||||
);
|
||||
expect(vm.quasar.notify).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
message: 'Invalid username or password',
|
||||
type: 'negative',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should return an unauthorized error message', async () => {
|
||||
vi.spyOn(vm.quasar, 'notify');
|
||||
|
||||
session.isLoggedIn.mockReturnValue(true);
|
||||
|
||||
const response = {
|
||||
response: {
|
||||
status: 401,
|
||||
data: {
|
||||
error: {
|
||||
message: 'Access denied',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(vm.responseError(response)).rejects.toEqual(
|
||||
expect.objectContaining(response)
|
||||
);
|
||||
expect(vm.quasar.notify).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
message: 'Access denied',
|
||||
type: 'negative',
|
||||
})
|
||||
);
|
||||
|
||||
expect(session.destroy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
|
@ -0,0 +1,81 @@
|
|||
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',
|
||||
})
|
||||
);
|
||||
});
|
||||
})
|
||||
});
|
|
@ -1,7 +1,6 @@
|
|||
import { vi, describe, expect, it, beforeAll, afterEach } from 'vitest';
|
||||
import { createWrapper, axios } from 'app/test/vitest/helper';
|
||||
import Login from 'pages/Login/LoginMain.vue';
|
||||
import { Notify } from 'quasar';
|
||||
|
||||
describe('Login', () => {
|
||||
let vm;
|
||||
|
|
Loading…
Reference in New Issue