From 012a1ec77f5f934d67eaa056a89ad89a3eef7043 Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 16 Mar 2023 10:54:19 +0100 Subject: [PATCH 1/2] Added axios unit test Refs #5420 --- src/boot/axios.js | 5 ++ test/vitest/__tests__/App.spec.js | 84 ------------------- test/vitest/__tests__/boot/axios.spec.js | 81 ++++++++++++++++++ .../__tests__/pages/Login/Login.spec.js | 1 - 4 files changed, 86 insertions(+), 85 deletions(-) delete mode 100644 test/vitest/__tests__/App.spec.js create mode 100644 test/vitest/__tests__/boot/axios.spec.js diff --git a/src/boot/axios.js b/src/boot/axios.js index 076191d28..2f454188e 100644 --- a/src/boot/axios.js +++ b/src/boot/axios.js @@ -73,3 +73,8 @@ const onResponseError = (error) => { axios.interceptors.request.use(onRequest, onRequestError); axios.interceptors.response.use(onResponse, onResponseError); + +export { + onRequest, + onResponseError +} diff --git a/test/vitest/__tests__/App.spec.js b/test/vitest/__tests__/App.spec.js deleted file mode 100644 index c04962c58..000000000 --- a/test/vitest/__tests__/App.spec.js +++ /dev/null @@ -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(); - }); -}); diff --git a/test/vitest/__tests__/boot/axios.spec.js b/test/vitest/__tests__/boot/axios.spec.js new file mode 100644 index 000000000..a95c57fba --- /dev/null +++ b/test/vitest/__tests__/boot/axios.spec.js @@ -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', + }) + ); + }); + }) +}); diff --git a/test/vitest/__tests__/pages/Login/Login.spec.js b/test/vitest/__tests__/pages/Login/Login.spec.js index fff021144..fadfc898f 100644 --- a/test/vitest/__tests__/pages/Login/Login.spec.js +++ b/test/vitest/__tests__/pages/Login/Login.spec.js @@ -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; From f4dc6a241e4fa74dceeb412ed39f92d904b839ce Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 16 Mar 2023 11:46:41 +0100 Subject: [PATCH 2/2] refactor(DescriptorProxy): renamed DescriptorPopover to DescriptorProxy Refs #4874 --- src/pages/Claim/Card/ClaimDescriptor.vue | 7 +- src/pages/Claim/Card/ClaimSummary.vue | 35 +- src/pages/Claim/ClaimList.vue | 7 +- ...opover.vue => CustomerDescriptorProxy.vue} | 6 +- .../InvoiceOut/Card/InvoiceOutDescriptor.vue | 8 +- ...over.vue => InvoiceOutDescriptorProxy.vue} | 6 +- src/pages/Ticket/Card/TicketDescriptor.vue | 6 +- ...rPopover.vue => TicketDescriptorProxy.vue} | 6 +- src/pages/Ticket/Card/TicketSummary.vue | 331 +++++++++++++----- 9 files changed, 294 insertions(+), 118 deletions(-) rename src/pages/Customer/Card/{CustomerDescriptorPopover.vue => CustomerDescriptorProxy.vue} (66%) rename src/pages/InvoiceOut/Card/{InvoiceOutDescriptorPopover.vue => InvoiceOutDescriptorProxy.vue} (66%) rename src/pages/Ticket/Card/{TicketDescriptorPopover.vue => TicketDescriptorProxy.vue} (66%) diff --git a/src/pages/Claim/Card/ClaimDescriptor.vue b/src/pages/Claim/Card/ClaimDescriptor.vue index 84b924c2f..aa78776a9 100644 --- a/src/pages/Claim/Card/ClaimDescriptor.vue +++ b/src/pages/Claim/Card/ClaimDescriptor.vue @@ -4,7 +4,7 @@ import { useRoute } from 'vue-router'; import { useI18n } from 'vue-i18n'; import { toDate } from 'src/filters'; -import TicketDescriptorPopover from 'pages/Ticket/Card/TicketDescriptorPopover.vue'; +import TicketDescriptorProxy from 'pages/Ticket/Card/TicketDescriptorProxy.vue'; import ClaimDescriptorMenu from 'pages/Claim/Card/ClaimDescriptorMenu.vue'; import CardDescriptor from 'components/ui/CardDescriptor.vue'; @@ -86,9 +86,8 @@ function stateColor(code) { {{ entity.ticketFk }} - - - + + diff --git a/src/pages/Claim/Card/ClaimSummary.vue b/src/pages/Claim/Card/ClaimSummary.vue index 98027b4ad..77b9a6b27 100644 --- a/src/pages/Claim/Card/ClaimSummary.vue +++ b/src/pages/Claim/Card/ClaimSummary.vue @@ -4,6 +4,7 @@ import { useRoute } from 'vue-router'; import { useI18n } from 'vue-i18n'; import { toDate, toCurrency } from 'src/filters'; import CardSummary from 'components/ui/CardSummary.vue'; +import WorkerDescriptorProxy from 'pages/Worker/Card/WorkerDescriptorProxy.vue'; const route = useRoute(); const { t } = useI18n(); @@ -109,22 +110,30 @@ function stateColor(code) { - {{ - t('claim.summary.assignedTo') - }} - {{ - claim.worker.user.nickname - }} + + {{ t('claim.summary.assignedTo') }} + + + + {{ claim.worker.user.nickname }} + + + - {{ - t('claim.summary.attendedBy') - }} - {{ - claim.client.salesPersonUser.name - }} + + {{ t('claim.summary.attendedBy') }} + + + + {{ claim.client.salesPersonUser.name }} + + + @@ -169,4 +178,4 @@ function stateColor(code) { - \ No newline at end of file + diff --git a/src/pages/Claim/ClaimList.vue b/src/pages/Claim/ClaimList.vue index dbc3aa1c1..05c074ee0 100644 --- a/src/pages/Claim/ClaimList.vue +++ b/src/pages/Claim/ClaimList.vue @@ -6,7 +6,7 @@ import { useStateStore } from 'stores/useStateStore'; import { toDate } from 'filters/index'; import Paginate from 'components/PaginateData.vue'; import ClaimSummaryDialog from './Card/ClaimSummaryDialog.vue'; -import CustomerDescriptorPopover from 'pages/Customer/Card/CustomerDescriptorPopover.vue'; +import CustomerDescriptorProxy from 'pages/Customer/Card/CustomerDescriptorProxy.vue'; import VnSearchbar from 'components/ui/VnSearchbar.vue'; import ClaimFilter from './ClaimFilter.vue'; @@ -173,9 +173,8 @@ function viewSummary(id) { {{ t('components.smartCard.viewDescription') }} - - - + + diff --git a/src/pages/Customer/Card/CustomerDescriptorPopover.vue b/src/pages/Customer/Card/CustomerDescriptorProxy.vue similarity index 66% rename from src/pages/Customer/Card/CustomerDescriptorPopover.vue rename to src/pages/Customer/Card/CustomerDescriptorProxy.vue index 78d1c5801..6e69ca1a0 100644 --- a/src/pages/Customer/Card/CustomerDescriptorPopover.vue +++ b/src/pages/Customer/Card/CustomerDescriptorProxy.vue @@ -9,7 +9,7 @@ const $props = defineProps({ }); diff --git a/src/pages/InvoiceOut/Card/InvoiceOutDescriptor.vue b/src/pages/InvoiceOut/Card/InvoiceOutDescriptor.vue index 31252f3e3..2e13b1438 100644 --- a/src/pages/InvoiceOut/Card/InvoiceOutDescriptor.vue +++ b/src/pages/InvoiceOut/Card/InvoiceOutDescriptor.vue @@ -3,8 +3,8 @@ import { ref, computed } from 'vue'; import { useRoute } from 'vue-router'; import { useI18n } from 'vue-i18n'; import { toCurrency, toDate } from 'src/filters'; -import CardDescriptor from 'src/components/ui/CardDescriptor.vue'; -import CustomerDescriptorPopover from 'src/pages/Customer/Card/CustomerDescriptorPopover.vue'; +import CardDescriptor from 'components/ui/CardDescriptor.vue'; +import CustomerDescriptorProxy from 'pages/Customer/Card/CustomerDescriptorProxy.vue'; const $props = defineProps({ id: { @@ -80,9 +80,7 @@ function ticketFilter(invoice) { {{ entity.client.name }} - - - + diff --git a/src/pages/InvoiceOut/Card/InvoiceOutDescriptorPopover.vue b/src/pages/InvoiceOut/Card/InvoiceOutDescriptorProxy.vue similarity index 66% rename from src/pages/InvoiceOut/Card/InvoiceOutDescriptorPopover.vue rename to src/pages/InvoiceOut/Card/InvoiceOutDescriptorProxy.vue index 30d27669d..a3389545b 100644 --- a/src/pages/InvoiceOut/Card/InvoiceOutDescriptorPopover.vue +++ b/src/pages/InvoiceOut/Card/InvoiceOutDescriptorProxy.vue @@ -9,7 +9,7 @@ const $props = defineProps({ }); diff --git a/src/pages/Ticket/Card/TicketDescriptor.vue b/src/pages/Ticket/Card/TicketDescriptor.vue index 33bd9614b..bdf796893 100644 --- a/src/pages/Ticket/Card/TicketDescriptor.vue +++ b/src/pages/Ticket/Card/TicketDescriptor.vue @@ -3,7 +3,7 @@ import { computed } from 'vue'; import { useRoute } from 'vue-router'; import { useI18n } from 'vue-i18n'; import { toDate } from 'src/filters'; -import CustomerDescriptorPopover from 'src/pages/Customer/Card/CustomerDescriptorPopover.vue'; +import CustomerDescriptorProxy from 'pages/Customer/Card/CustomerDescriptorProxy.vue'; import CardDescriptor from 'components/ui/CardDescriptor.vue'; import TicketDescriptorMenu from './TicketDescriptorMenu.vue'; @@ -109,9 +109,7 @@ function stateColor(state) { {{ entity.clientFk }} - - - + diff --git a/src/pages/Ticket/Card/TicketDescriptorPopover.vue b/src/pages/Ticket/Card/TicketDescriptorProxy.vue similarity index 66% rename from src/pages/Ticket/Card/TicketDescriptorPopover.vue rename to src/pages/Ticket/Card/TicketDescriptorProxy.vue index 891ffb3a8..0a7a353bb 100644 --- a/src/pages/Ticket/Card/TicketDescriptorPopover.vue +++ b/src/pages/Ticket/Card/TicketDescriptorProxy.vue @@ -9,7 +9,7 @@ const $props = defineProps({ }); diff --git a/src/pages/Ticket/Card/TicketSummary.vue b/src/pages/Ticket/Card/TicketSummary.vue index 111a1e957..320437596 100644 --- a/src/pages/Ticket/Card/TicketSummary.vue +++ b/src/pages/Ticket/Card/TicketSummary.vue @@ -7,6 +7,8 @@ import { dashIfEmpty, toDate, toCurrency } from 'src/filters'; import SkeletonSummary from 'components/ui/SkeletonSummary.vue'; import FetchData from 'components/FetchData.vue'; import FetchedTags from 'components/ui/FetchedTags.vue'; +import InvoiceOutDescriptorProxy from 'pages/InvoiceOut/Card/InvoiceOutDescriptorProxy.vue'; +import WorkerDescriptorProxy from 'pages/Worker/Card/WorkerDescriptorProxy.vue'; onMounted(() => fetch()); onUpdated(() => fetch()); @@ -79,14 +81,20 @@ async function changeState(value) {