mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 05:51:44 +03:00
fix(percentage-calculator): clear the result when an input is cleared
n-input-number emits null when its text is cleared, while the computeds only treated undefined as a missing value. The arithmetic then coerced null to 0, so emptying To showed -100 and emptying X showed 0. The three formulas moved into a service that treats null and undefined alike, with unit tests for the cleared cases and an e2e case that clears a filled field. Fixes #1906
This commit is contained in:
1 parent
1d77ed3bbe
commit
91ee2ab7a8
4 files changed
+120
-20
No files matched your search
@@ -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('');
|
||||
});
|
||||
});
|
||||
@@ -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('');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1,35 +1,24 @@
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import {
|
||||
computeIsWhatPercentOf,
|
||||
computePercentageIncreaseDecrease,
|
||||
computePercentageOf,
|
||||
} from './percentage-calculator.service';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const percentageX = ref();
|
||||
const percentageY = ref();
|
||||
const percentageResult = computed(() => {
|
||||
if (percentageX.value === undefined || percentageY.value === undefined) {
|
||||
return '';
|
||||
}
|
||||
return ((percentageX.value / 100) * percentageY.value).toString();
|
||||
});
|
||||
const percentageResult = computed(() => computePercentageOf(percentageX.value, percentageY.value));
|
||||
|
||||
const numberX = ref();
|
||||
const numberY = ref();
|
||||
const numberResult = computed(() => {
|
||||
if (numberX.value === undefined || numberY.value === undefined) {
|
||||
return '';
|
||||
}
|
||||
const result = (100 * numberX.value) / numberY.value;
|
||||
return !Number.isFinite(result) || Number.isNaN(result) ? '' : result.toString();
|
||||
});
|
||||
const numberResult = computed(() => computeIsWhatPercentOf(numberX.value, numberY.value));
|
||||
|
||||
const numberFrom = ref();
|
||||
const numberTo = ref();
|
||||
const percentageIncreaseDecrease = computed(() => {
|
||||
if (numberFrom.value === undefined || numberTo.value === undefined) {
|
||||
return '';
|
||||
}
|
||||
const result = ((numberTo.value - numberFrom.value) / numberFrom.value) * 100;
|
||||
return !Number.isFinite(result) || Number.isNaN(result) ? '' : result.toString();
|
||||
});
|
||||
const percentageIncreaseDecrease = computed(() => computePercentageIncreaseDecrease(numberFrom.value, numberTo.value));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in new issue
Block a user