diff --git a/src/tools/percentage-calculator/percentage-calculator.e2e.spec.ts b/src/tools/percentage-calculator/percentage-calculator.e2e.spec.ts index 5cb07da2..3136312c 100644 --- a/src/tools/percentage-calculator/percentage-calculator.e2e.spec.ts +++ b/src/tools/percentage-calculator/percentage-calculator.e2e.spec.ts @@ -33,4 +33,22 @@ test.describe('Tool - Percentage calculator', () => { await page.getByTestId('numberFrom').locator('input').pressSequentially('123\n'); await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue(''); }); + + test('Displays empty results after an input is cleared', async ({ page }) => { + await page.getByTestId('percentageX').locator('input').pressSequentially('40\n'); + await page.getByTestId('percentageY').locator('input').pressSequentially('50\n'); + await expect(page.getByTestId('percentageResult').locator('input')).toHaveValue('20'); + + await page.getByTestId('numberFrom').locator('input').pressSequentially('40\n'); + await page.getByTestId('numberTo').locator('input').pressSequentially('46\n'); + await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue('15'); + + await page.getByTestId('percentageY').locator('input').clear(); + await page.getByTestId('numberTo').locator('input').clear(); + await page.getByTestId('percentageX').locator('input').clear(); + await page.getByTestId('numberFrom').locator('input').clear(); + + await expect(page.getByTestId('percentageResult').locator('input')).toHaveValue(''); + await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue(''); + }); }); diff --git a/src/tools/percentage-calculator/percentage-calculator.service.test.ts b/src/tools/percentage-calculator/percentage-calculator.service.test.ts new file mode 100644 index 00000000..d3d3004a --- /dev/null +++ b/src/tools/percentage-calculator/percentage-calculator.service.test.ts @@ -0,0 +1,58 @@ +import { describe, expect, it } from 'vitest'; +import { + computeIsWhatPercentOf, + computePercentageIncreaseDecrease, + computePercentageOf, +} from './percentage-calculator.service'; + +describe('percentage-calculator', () => { + describe('computePercentageOf', () => { + it('computes a percentage of a value', () => { + expect(computePercentageOf(123, 456)).toBe('560.88'); + expect(computePercentageOf(0, 200)).toBe('0'); + }); + + it('returns an empty result when an input is missing', () => { + expect(computePercentageOf(undefined, 200)).toBe(''); + expect(computePercentageOf(200, undefined)).toBe(''); + expect(computePercentageOf(null, 200)).toBe(''); + expect(computePercentageOf(200, null)).toBe(''); + }); + }); + + describe('computeIsWhatPercentOf', () => { + it('computes what percent x is of y', () => { + expect(computeIsWhatPercentOf(123, 456)).toBe('26.973684210526315'); + }); + + it('returns an empty result when an input is missing', () => { + expect(computeIsWhatPercentOf(undefined, 12)).toBe(''); + expect(computeIsWhatPercentOf(12, undefined)).toBe(''); + expect(computeIsWhatPercentOf(null, 12)).toBe(''); + expect(computeIsWhatPercentOf(12, null)).toBe(''); + }); + + it('returns an empty result when the divisor is zero', () => { + expect(computeIsWhatPercentOf(12, 0)).toBe(''); + }); + }); + + describe('computePercentageIncreaseDecrease', () => { + it('computes the increase and the decrease', () => { + expect(computePercentageIncreaseDecrease(123, 456)).toBe('270.7317073170732'); + expect(computePercentageIncreaseDecrease(40, 46)).toBe('15'); + expect(computePercentageIncreaseDecrease(46, 40)).toBe('-13.043478260869565'); + }); + + it('returns an empty result when an input is missing', () => { + expect(computePercentageIncreaseDecrease(undefined, 46)).toBe(''); + expect(computePercentageIncreaseDecrease(40, undefined)).toBe(''); + expect(computePercentageIncreaseDecrease(null, 46)).toBe(''); + expect(computePercentageIncreaseDecrease(40, null)).toBe(''); + }); + + it('returns an empty result when the reference value is zero', () => { + expect(computePercentageIncreaseDecrease(0, 46)).toBe(''); + }); + }); +}); diff --git a/src/tools/percentage-calculator/percentage-calculator.service.ts b/src/tools/percentage-calculator/percentage-calculator.service.ts new file mode 100644 index 00000000..4a22619a --- /dev/null +++ b/src/tools/percentage-calculator/percentage-calculator.service.ts @@ -0,0 +1,35 @@ +export type PercentageInput = number | null | undefined; + +export { computePercentageOf, computeIsWhatPercentOf, computePercentageIncreaseDecrease }; + +function isMissing(value: PercentageInput): value is null | undefined { + return value === null || value === undefined; +} + +function formatFinite(value: number): string { + return Number.isFinite(value) ? value.toString() : ''; +} + +function computePercentageOf(x: PercentageInput, y: PercentageInput): string { + if (isMissing(x) || isMissing(y)) { + return ''; + } + + return formatFinite((x / 100) * y); +} + +function computeIsWhatPercentOf(x: PercentageInput, y: PercentageInput): string { + if (isMissing(x) || isMissing(y)) { + return ''; + } + + return formatFinite((100 * x) / y); +} + +function computePercentageIncreaseDecrease(from: PercentageInput, to: PercentageInput): string { + if (isMissing(from) || isMissing(to)) { + return ''; + } + + return formatFinite(((to - from) / from) * 100); +} diff --git a/src/tools/percentage-calculator/percentage-calculator.vue b/src/tools/percentage-calculator/percentage-calculator.vue index 0a046248..f2f24b82 100644 --- a/src/tools/percentage-calculator/percentage-calculator.vue +++ b/src/tools/percentage-calculator/percentage-calculator.vue @@ -1,35 +1,24 @@