From b995df3248f594eaf608943c845a1145617cfb4d Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 11 Apr 2023 14:36:12 +0200 Subject: [PATCH] Added unit tests --- src/pages/Claim/Card/ClaimLines.vue | 8 +- src/pages/Claim/Card/ClaimLinesImport.vue | 2 +- .../__tests__/pages/Claims/ClaimLines.spec.js | 94 +++++++++++++++++++ .../pages/Claims/ClaimLinesImport.spec.js | 50 ++++++++++ 4 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 test/vitest/__tests__/pages/Claims/ClaimLines.spec.js create mode 100644 test/vitest/__tests__/pages/Claims/ClaimLinesImport.spec.js diff --git a/src/pages/Claim/Card/ClaimLines.vue b/src/pages/Claim/Card/ClaimLines.vue index 6bd8dc21f..ea87aa314 100644 --- a/src/pages/Claim/Card/ClaimLines.vue +++ b/src/pages/Claim/Card/ClaimLines.vue @@ -4,13 +4,13 @@ import { ref, computed } from 'vue'; import { useI18n } from 'vue-i18n'; import { useQuasar } from 'quasar'; import { useRoute } from 'vue-router'; -import { useArrayData } from 'src/composables/useArrayData'; +import { useArrayData } from 'composables/useArrayData'; import { useStateStore } from 'stores/useStateStore'; -import VnPaginate from 'src/components/ui/VnPaginate.vue'; +import VnPaginate from 'components/ui/VnPaginate.vue'; import FetchData from 'components/FetchData.vue'; -import VnConfirm from 'src/components/ui/VnConfirm.vue'; +import VnConfirm from 'components/ui/VnConfirm.vue'; -import { toDate, toCurrency, toPercentage } from 'src/filters'; +import { toDate, toCurrency, toPercentage } from 'filters/index'; import VnDiscount from 'components/common/vnDiscount.vue'; import ClaimLinesImport from './ClaimLinesImport.vue'; diff --git a/src/pages/Claim/Card/ClaimLinesImport.vue b/src/pages/Claim/Card/ClaimLinesImport.vue index d6f9fe202..fad1ac3af 100644 --- a/src/pages/Claim/Card/ClaimLinesImport.vue +++ b/src/pages/Claim/Card/ClaimLinesImport.vue @@ -4,7 +4,7 @@ import { useQuasar, useDialogPluginComponent } from 'quasar'; import { useI18n } from 'vue-i18n'; import { useRoute } from 'vue-router'; import FetchData from 'components/FetchData.vue'; -import { toDate, toCurrency, toPercentage } from 'filters'; +import { toDate, toCurrency, toPercentage } from 'filters/index'; import axios from 'axios'; defineEmits([...useDialogPluginComponent.emits]); diff --git a/test/vitest/__tests__/pages/Claims/ClaimLines.spec.js b/test/vitest/__tests__/pages/Claims/ClaimLines.spec.js new file mode 100644 index 000000000..dda59a98d --- /dev/null +++ b/test/vitest/__tests__/pages/Claims/ClaimLines.spec.js @@ -0,0 +1,94 @@ +import { vi, describe, expect, it, beforeAll, beforeEach, afterEach } from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; +import ClaimLines from 'pages/Claim/Card/ClaimLines.vue'; + +describe('ClaimLines', () => { + let vm; + + + beforeAll(() => { + vm = createWrapper(ClaimLines, { + global: { + stubs: ['FetchData', 'VnPaginate'], + mocks: { + fetch: vi.fn(), + }, + } + }).vm; + }); + + beforeEach(() => { + vm.claim = { + id: 1, + ticketFk: 1 + } + vm.store.data = [ + { + id: 1, + quantity: 10, + sale: { + id: 1, discount: 0 + } + } + ] + }) + + afterEach(() => { + vi.clearAllMocks(); + }); + + describe('updateDiscount()', () => { + it('should make a POST request to endpoint "updateDiscount"', async () => { + vi.spyOn(axios, 'post').mockResolvedValue({ data: true }); + vi.spyOn(vm.quasar, 'notify'); + + const canceller = new AbortController() + await vm.updateDiscount({ saleFk: 1, discount: 5, canceller }); + + const expectedData = { salesIds: [1], newDiscount: 5 } + expect(axios.post).toHaveBeenCalledWith('Tickets/1/updateDiscount', expectedData, { + signal: canceller.signal + }) + }); + }); + + describe('onUpdateDiscount()', () => { + it('should make a POST request and then set the discount on the original row', async () => { + vi.spyOn(vm.quasar, 'notify'); + + + vm.onUpdateDiscount({ discount: 5, rowIndex: 0 }); + const firstRow = vm.store.data[0] + + expect(firstRow.sale.discount).toEqual(5) + expect(vm.quasar.notify).toHaveBeenCalledWith( + expect.objectContaining({ + message: 'Discount updated', + type: 'positive' + }) + ); + }); + }); + + describe('remove()', () => { + it('should make a POST request and then call to the quasar notify() method', async () => { + vi.spyOn(axios, 'post').mockResolvedValue({ data: true }); + vi.spyOn(vm.quasar, 'notify'); + + await vm.remove({ + rows: [ + { id: 1 } + ] + }); + const expectedData = { deletes: [1] } + + expect(axios.post).toHaveBeenCalledWith('ClaimBeginnings/crud', expectedData) + expect(vm.quasar.notify).toHaveBeenCalledWith( + expect.objectContaining({ + message: 'Row removed', + type: 'positive' + }) + ); + }); + }); +}); diff --git a/test/vitest/__tests__/pages/Claims/ClaimLinesImport.spec.js b/test/vitest/__tests__/pages/Claims/ClaimLinesImport.spec.js new file mode 100644 index 000000000..085118b14 --- /dev/null +++ b/test/vitest/__tests__/pages/Claims/ClaimLinesImport.spec.js @@ -0,0 +1,50 @@ +import { vi, describe, expect, it, beforeAll, beforeEach, afterEach } from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; +import ClaimLinesImport from 'pages/Claim/Card/ClaimLinesImport.vue'; + +describe('ClaimLinesImport', () => { + let vm; + + + beforeAll(() => { + vm = createWrapper(ClaimLinesImport, { + global: { + stubs: ['FetchData'], + mocks: { + fetch: vi.fn(), + }, + } + }).vm; + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + describe('importLines()', () => { + it('should make a POST request and then call to the quasar notify() method', async () => { + vi.spyOn(axios, 'post').mockResolvedValue({ data: true }); + vi.spyOn(vm.quasar, 'notify'); + + vm.selected = [ + { id: 1, saleFk: 1, claimFk: 1, quantity: 10 } + ] + + vm.route.params.id = 1 + + await vm.importLines(); + const expectedData = [{ saleFk: 1, claimFk: 1, quantity: 10 }] + + expect(axios.post).toHaveBeenCalledWith('ClaimBeginnings', expectedData, { + signal: expect.any(Object) + }) + expect(vm.quasar.notify).toHaveBeenCalledWith( + expect.objectContaining({ + message: 'Lines added to claim', + type: 'positive' + }) + ); + expect(vm.canceller).toEqual(null) + }); + }); +});