From d916b47f4c8e9b385a2e4a839680471137cad16a Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 5 Dec 2024 10:40:50 +0100 Subject: [PATCH] feat: refs #7936 enhance getTotal fn & add unit tests --- src/composables/getTotal.js | 5 +- .../InvoiceIn/Card/InvoiceInIntrastat.vue | 2 +- .../__tests__/composables/getTotal.spec.js | 55 +++++++++++++++++++ .../InvoiceIn/InvoiceInIntrastat.spec.js | 34 ------------ .../pages/InvoiceIn/InvoiceInVat.spec.js | 38 ------------- 5 files changed, 59 insertions(+), 75 deletions(-) create mode 100644 test/vitest/__tests__/composables/getTotal.spec.js delete mode 100644 test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js delete mode 100644 test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js diff --git a/src/composables/getTotal.js b/src/composables/getTotal.js index 24ac3aa27..91b884bda 100644 --- a/src/composables/getTotal.js +++ b/src/composables/getTotal.js @@ -1,10 +1,11 @@ import { toCurrency } from 'src/filters'; export function getTotal(rows, key, opts = {}) { - const { currency, cb, decimalPlaces } = opts; + const { currency, cb, decimalPlaces, int } = opts; const total = rows.reduce((acc, row) => acc + +(cb ? cb(row) : row[key] || 0), 0); + const decimals = int ? 0 : decimalPlaces ?? 2; return currency ? toCurrency(total, currency == 'default' ? undefined : currency) - : parseFloat(total).toFixed(decimalPlaces ?? 2); + : parseFloat(total).toFixed(decimals); } diff --git a/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue b/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue index 08ce0755d..b821025c2 100644 --- a/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue +++ b/src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue @@ -154,7 +154,7 @@ const formatOpt = (row, { model, options }, prop) => { {{ getTotal(rows, 'net') }} - {{ getTotal(rows, 'stems') }} + {{ getTotal(rows, 'stems', { int: true }) }} diff --git a/test/vitest/__tests__/composables/getTotal.spec.js b/test/vitest/__tests__/composables/getTotal.spec.js new file mode 100644 index 000000000..7081c4334 --- /dev/null +++ b/test/vitest/__tests__/composables/getTotal.spec.js @@ -0,0 +1,55 @@ +import { vi, describe, expect, it } from 'vitest'; +import { getTotal } from 'src/composables/getTotal'; + +vi.mock('src/filters', () => ({ + toCurrency: vi.fn((value, currency) => `${currency} ${value.toFixed(2)}`), +})); + +describe('getTotal()', () => { + const rows = [ + { amount: 10.5, tax: 2.1 }, + { amount: 20.75, tax: 3.25 }, + { amount: 30.25, tax: 4.75 }, + ]; + + it('should calculate the total for a given key', () => { + const total = getTotal(rows, 'amount'); + expect(total).toBe('61.50'); + }); + + it('should calculate the total with a callback function', () => { + const total = getTotal(rows, null, { cb: (row) => row.amount + row.tax }); + expect(total).toBe('71.60'); + }); + + it('should format the total as currency', () => { + const total = getTotal(rows, 'amount', { currency: 'USD' }); + expect(total).toBe('USD 61.50'); + }); + + it('should format the total as currency with default currency', () => { + const total = getTotal(rows, 'amount', { currency: 'default' }); + expect(total).toBe('undefined 61.50'); + }); + + it('should calculate the total with integer formatting', () => { + const total = getTotal(rows, 'amount', { int: true }); + expect(total).toBe('62'); + }); + + it('should calculate the total with custom decimal places', () => { + const total = getTotal(rows, 'amount', { decimalPlaces: 1 }); + expect(total).toBe('61.5'); + }); + + it('should handle rows with missing keys', () => { + const rowsWithMissingKeys = [{ amount: 10.5 }, { amount: 20.75 }, {}]; + const total = getTotal(rowsWithMissingKeys, 'amount'); + expect(total).toBe('31.25'); + }); + + it('should handle empty rows', () => { + const total = getTotal([], 'amount'); + expect(total).toBe('0.00'); + }); +}); diff --git a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js deleted file mode 100644 index adfb054c6..000000000 --- a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js +++ /dev/null @@ -1,34 +0,0 @@ -import { vi, describe, expect, it, beforeAll } from 'vitest'; -import { createWrapper, axios } from 'app/test/vitest/helper'; -import InvoiceInIntrastat from 'src/pages/InvoiceIn/Card/InvoiceInIntrastat.vue'; - -describe('InvoiceInIntrastat', () => { - let vm; - - beforeAll(() => { - vm = createWrapper(InvoiceInIntrastat, { - global: { - stubs: ['vnPaginate'], - mocks: { - fetch: vi.fn(), - }, - }, - }).vm; - vi.spyOn(axios, 'get').mockResolvedValue({ data: [{}] }); - }); - - describe('getTotal()', () => { - it('should correctly handle the sum', () => { - const invoceInIntrastat = [ - { amount: 10, stems: 162 }, - { amount: 20, stems: 21 }, - ]; - - const totalAmount = vm.getTotal(invoceInIntrastat, 'amount'); - const totalStems = vm.getTotal(invoceInIntrastat, 'stems'); - - expect(totalAmount).toBe(10 + 20); - expect(totalStems).toBe(162 + 21); - }); - }); -}); diff --git a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js deleted file mode 100644 index 76453f65a..000000000 --- a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js +++ /dev/null @@ -1,38 +0,0 @@ -import { vi, describe, expect, it, beforeAll } from 'vitest'; -import { createWrapper } from 'app/test/vitest/helper'; -import InvoiceInVat from 'src/pages/InvoiceIn/Card/InvoiceInVat.vue'; - -describe('InvoiceInVat', () => { - let vm; - - beforeAll(() => { - vm = createWrapper(InvoiceInVat, { - global: { - stubs: [], - mocks: { - fetch: vi.fn(), - }, - }, - }).vm; - }); - - describe('taxRate()', () => { - it('should correctly compute the tax rate', () => { - const invoiceInTax = { taxableBase: 100, taxTypeSageFk: 1 }; - vm.sageTaxTypes = [ - { id: 1, rate: 10 }, - { id: 2, rate: 20 }, - ]; - const result = vm.taxRate(invoiceInTax); - expect(result).toBe((10 / 100) * 100); - }); - - it('should return 0 if there is not tax rate', () => { - const invoiceInTax = { taxableBase: 100, taxTypeSageFk: 1 }; - vm.sageTaxTypes = []; - - const result = vm.taxRate(invoiceInTax); - expect(result).toBe(0); - }); - }); -});