From ef415d080adcd580d92634778bb8a135c9af0b38 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 14 Nov 2024 08:19:19 +0100 Subject: [PATCH 1/3] feat: refs #7308 #7308 remove warnings related to useSession --- src/boot/quasar.js | 2 ++ src/composables/useSession.js | 8 ++++++-- src/router/index.js | 8 +++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/boot/quasar.js b/src/boot/quasar.js index 01fe68d8b..1cc2e82cf 100644 --- a/src/boot/quasar.js +++ b/src/boot/quasar.js @@ -1,6 +1,7 @@ import { boot } from 'quasar/wrappers'; import qFormMixin from './qformMixin'; import keyShortcut from './keyShortcut'; +import { setupAxios } from 'src/boot/axios'; import useNotify from 'src/composables/useNotify.js'; import { CanceledError } from 'axios'; @@ -48,4 +49,5 @@ export default boot(({ app }) => { notify(message ?? 'globals.error', 'negative', 'error'); }; + setupAxios(); }); diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 633a30bb0..5097a1fee 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -8,9 +8,13 @@ import useNotify from './useNotify'; import { useTokenConfig } from './useTokenConfig'; const TOKEN_MULTIMEDIA = 'tokenMultimedia'; const TOKEN = 'token'; - +let router; +export default { + setup() { + router = useRouter(); + }, +}; export function useSession() { - const router = useRouter(); const { notify } = useNotify(); let isCheckingToken = false; let intervalId = null; diff --git a/src/router/index.js b/src/router/index.js index 18541c0b2..5ed8ad9ed 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -14,8 +14,8 @@ import { useUserConfig } from 'src/composables/useUserConfig'; import { useTokenConfig } from 'src/composables/useTokenConfig'; import { useAcl } from 'src/composables/useAcl'; -const state = useState(); -const session = useSession(); +let state, session; + const { t, te } = i18n.global; const createHistory = process.env.SERVER @@ -43,8 +43,10 @@ const Router = createRouter({ * with the Router instance. */ export { Router }; -export default route(function (/* { store, ssrContext } */) { +export default route((/* { store, ssrContext } */) => { Router.beforeEach(async (to, from, next) => { + state = useState(); + session = useSession(); const { isLoggedIn } = session; const outLayout = Router.options.routes[0].children.map((r) => r.name); if (!isLoggedIn() && !outLayout.includes(to.name)) { From 590d495dbddfeb6b252e2967c259394ef80f6b2a Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 14 Nov 2024 08:19:41 +0100 Subject: [PATCH 2/3] test: refs #7308 #7308 axios.spec.js --- src/boot/axios.js | 19 +++++--- test/vitest/__tests__/boot/axios.spec.js | 62 ++++++++++++++++++------ 2 files changed, 58 insertions(+), 23 deletions(-) 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: {} }; From b0bb09e9a66f619fee6621f633577f76566e4440 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 14 Nov 2024 09:03:18 +0100 Subject: [PATCH 3/3] perf: refs #7308 #7308 remove comments --- test/vitest/__tests__/boot/axios.spec.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/vitest/__tests__/boot/axios.spec.js b/test/vitest/__tests__/boot/axios.spec.js index dc321a658..3c59dbc48 100644 --- a/test/vitest/__tests__/boot/axios.spec.js +++ b/test/vitest/__tests__/boot/axios.spec.js @@ -8,21 +8,6 @@ vi.mock('src/composables/useSession'); vi.mock('src/composables/useNotify'); vi.mock('src/stores/useStateQueryStore'); -// 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;