diff --git a/src/boot/axios.js b/src/boot/axios.js index aee38e887..5da26a404 100644 --- a/src/boot/axios.js +++ b/src/boot/axios.js @@ -4,9 +4,8 @@ import { Router } from 'src/router'; import useNotify from 'src/composables/useNotify.js'; import { useStateQueryStore } from 'src/stores/useStateQueryStore'; -const session = useSession(); -const { notify } = useNotify(); -const stateQuery = useStateQueryStore(); +let session, notify, stateQuery; + const baseUrl = '/api/'; axios.defaults.baseURL = baseUrl; @@ -51,9 +50,15 @@ const onResponseError = (error) => { return Promise.reject(error); }; -axios.interceptors.request.use(onRequest, onRequestError); -axios.interceptors.response.use(onResponse, onResponseError); -axiosNoError.interceptors.request.use(onRequest); -axiosNoError.interceptors.response.use(onResponse); +export function setupAxios() { + session = useSession(); + notify = useNotify().notify; + stateQuery = useStateQueryStore(); + + axios.interceptors.request.use(onRequest, onRequestError); + axios.interceptors.response.use(onResponse, onResponseError); + axiosNoError.interceptors.request.use(onRequest); + axiosNoError.interceptors.response.use(onResponse); +} export { onRequest, onResponseError, axiosNoError }; diff --git a/test/vitest/__tests__/boot/axios.spec.js b/test/vitest/__tests__/boot/axios.spec.js index 19d396ec5..dc321a658 100644 --- a/test/vitest/__tests__/boot/axios.spec.js +++ b/test/vitest/__tests__/boot/axios.spec.js @@ -1,23 +1,53 @@ -import { Notify } from 'quasar'; -import { onRequest, onResponseError } from 'src/boot/axios'; -import { describe, expect, it, vi } from 'vitest'; +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', () => ({ - useSession: () => ({ - getToken: () => 'DEFAULT_TOKEN', - isLoggedIn: () => vi.fn(), - destroy: () => vi.fn(), - }), -})); +vi.mock('src/composables/useSession'); +vi.mock('src/composables/useNotify'); +vi.mock('src/stores/useStateQueryStore'); -vi.mock('src/stores/useStateQueryStore', () => ({ - useStateQueryStore: () => ({ - add: () => vi.fn(), - remove: () => vi.fn(), - }), -})); +// vi.mock('src/composables/useSession', () => ({ +// useSession: () => ({ +// getToken: () => 'DEFAULT_TOKEN', +// isLoggedIn: () => vi.fn(), +// destroy: () => vi.fn(), +// }), +// })); + +// vi.mock('src/stores/useStateQueryStore', () => ({ +// useStateQueryStore: () => ({ +// add: () => vi.fn(), +// remove: () => vi.fn(), +// }), +// })); 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: {} };