feat: test coverage for utils
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* Tests for getDecimalPlaces utility
|
||||
*/
|
||||
|
||||
import {
|
||||
describe,
|
||||
expect,
|
||||
test,
|
||||
} from 'vitest';
|
||||
import { getDecimalPlaces } from './getDecimalPlaces';
|
||||
|
||||
describe('getDecimalPlaces', () => {
|
||||
describe('basic functionality', () => {
|
||||
test('should return 0 for integers', () => {
|
||||
expect(getDecimalPlaces(0)).toBe(0);
|
||||
expect(getDecimalPlaces(1)).toBe(0);
|
||||
expect(getDecimalPlaces(42)).toBe(0);
|
||||
expect(getDecimalPlaces(-7)).toBe(0);
|
||||
expect(getDecimalPlaces(1000)).toBe(0);
|
||||
});
|
||||
|
||||
test('should return correct decimal places for decimals', () => {
|
||||
expect(getDecimalPlaces(0.1)).toBe(1);
|
||||
expect(getDecimalPlaces(0.5)).toBe(1);
|
||||
expect(getDecimalPlaces(0.01)).toBe(2);
|
||||
expect(getDecimalPlaces(0.05)).toBe(2);
|
||||
expect(getDecimalPlaces(0.001)).toBe(3);
|
||||
expect(getDecimalPlaces(0.123)).toBe(3);
|
||||
expect(getDecimalPlaces(0.123456)).toBe(6);
|
||||
});
|
||||
|
||||
test('should handle negative decimal numbers', () => {
|
||||
expect(getDecimalPlaces(-0.1)).toBe(1);
|
||||
expect(getDecimalPlaces(-0.05)).toBe(2);
|
||||
expect(getDecimalPlaces(-1.5)).toBe(1);
|
||||
expect(getDecimalPlaces(-99.99)).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('whole numbers with decimal part', () => {
|
||||
test('should handle numbers with integer and decimal parts', () => {
|
||||
expect(getDecimalPlaces(1.5)).toBe(1);
|
||||
expect(getDecimalPlaces(10.25)).toBe(2);
|
||||
expect(getDecimalPlaces(100.125)).toBe(3);
|
||||
expect(getDecimalPlaces(1234.5678)).toBe(4);
|
||||
});
|
||||
|
||||
test('should handle trailing zeros correctly', () => {
|
||||
// Note: JavaScript string representation drops trailing zeros
|
||||
expect(getDecimalPlaces(1.5)).toBe(1);
|
||||
expect(getDecimalPlaces(1.50)).toBe(1); // 1.50 becomes "1.5" in string
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
test('should handle zero', () => {
|
||||
expect(getDecimalPlaces(0)).toBe(0);
|
||||
expect(getDecimalPlaces(0.0)).toBe(0);
|
||||
});
|
||||
|
||||
test('should handle very small decimals', () => {
|
||||
expect(getDecimalPlaces(0.0001)).toBe(4);
|
||||
expect(getDecimalPlaces(0.00001)).toBe(5);
|
||||
expect(getDecimalPlaces(0.000001)).toBe(6);
|
||||
});
|
||||
|
||||
test('should handle very large numbers', () => {
|
||||
expect(getDecimalPlaces(123456789.123)).toBe(3);
|
||||
expect(getDecimalPlaces(999999.9999)).toBe(4);
|
||||
});
|
||||
|
||||
test('should handle negative whole numbers', () => {
|
||||
expect(getDecimalPlaces(-1)).toBe(0);
|
||||
expect(getDecimalPlaces(-100)).toBe(0);
|
||||
expect(getDecimalPlaces(-9999)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('special number values', () => {
|
||||
test('should handle Infinity', () => {
|
||||
expect(getDecimalPlaces(Infinity)).toBe(0);
|
||||
expect(getDecimalPlaces(-Infinity)).toBe(0);
|
||||
});
|
||||
|
||||
test('should handle NaN', () => {
|
||||
expect(getDecimalPlaces(NaN)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('scientific notation', () => {
|
||||
test('should handle numbers in scientific notation', () => {
|
||||
// Very small numbers may be represented in scientific notation
|
||||
const tiny = 1e-10;
|
||||
const result = getDecimalPlaces(tiny);
|
||||
// The result depends on how JS represents this as a string
|
||||
expect(typeof result).toBe('number');
|
||||
});
|
||||
|
||||
test('should handle large scientific notation numbers', () => {
|
||||
const large = 1.23e5; // 123000
|
||||
expect(getDecimalPlaces(large)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('real-world scenarios', () => {
|
||||
test('should handle currency values (2 decimal places)', () => {
|
||||
expect(getDecimalPlaces(0.01)).toBe(2); // 1 cent
|
||||
expect(getDecimalPlaces(0.99)).toBe(2); // 99 cents
|
||||
// Note: JavaScript string representation drops trailing zeros
|
||||
// 10.50 becomes "10.5" in string, so returns 1 decimal place
|
||||
expect(getDecimalPlaces(10.50)).toBe(1); // $10.50
|
||||
expect(getDecimalPlaces(999.99)).toBe(2); // $999.99
|
||||
});
|
||||
|
||||
test('should handle measurement values', () => {
|
||||
expect(getDecimalPlaces(12.5)).toBe(1); // 12.5 mm
|
||||
expect(getDecimalPlaces(12.34)).toBe(2); // 12.34 cm
|
||||
expect(getDecimalPlaces(12.345)).toBe(3); // 12.345 m
|
||||
});
|
||||
|
||||
test('should handle step values for sliders', () => {
|
||||
expect(getDecimalPlaces(0.1)).toBe(1); // Fine adjustment
|
||||
expect(getDecimalPlaces(0.25)).toBe(2); // Quarter steps
|
||||
expect(getDecimalPlaces(0.5)).toBe(1); // Half steps
|
||||
expect(getDecimalPlaces(1)).toBe(0); // Whole steps
|
||||
});
|
||||
|
||||
test('should handle font size increments', () => {
|
||||
expect(getDecimalPlaces(0.5)).toBe(1); // Half point increments
|
||||
expect(getDecimalPlaces(1)).toBe(0); // Whole point increments
|
||||
});
|
||||
|
||||
test('should handle opacity values', () => {
|
||||
expect(getDecimalPlaces(0.1)).toBe(1); // 10% increments
|
||||
expect(getDecimalPlaces(0.05)).toBe(2); // 5% increments
|
||||
expect(getDecimalPlaces(0.01)).toBe(2); // 1% increments
|
||||
});
|
||||
|
||||
test('should handle percentage values', () => {
|
||||
expect(getDecimalPlaces(0.5)).toBe(1); // 0.5%
|
||||
expect(getDecimalPlaces(12.5)).toBe(1); // 12.5%
|
||||
expect(getDecimalPlaces(33.33)).toBe(2); // 33.33%
|
||||
});
|
||||
|
||||
test('should handle coordinate precision', () => {
|
||||
expect(getDecimalPlaces(12.3456789)).toBe(7); // High precision GPS
|
||||
expect(getDecimalPlaces(100.5)).toBe(1); // Low precision coordinates
|
||||
});
|
||||
|
||||
test('should handle time values', () => {
|
||||
expect(getDecimalPlaces(0.1)).toBe(1); // 100ms
|
||||
expect(getDecimalPlaces(0.01)).toBe(2); // 10ms
|
||||
expect(getDecimalPlaces(0.001)).toBe(3); // 1ms
|
||||
});
|
||||
});
|
||||
|
||||
describe('common step values', () => {
|
||||
test('should correctly identify precision of common step values', () => {
|
||||
expect(getDecimalPlaces(0.05)).toBe(2); // Very fine steps
|
||||
expect(getDecimalPlaces(0.1)).toBe(1); // Fine steps
|
||||
expect(getDecimalPlaces(0.25)).toBe(2); // Quarter steps
|
||||
expect(getDecimalPlaces(0.5)).toBe(1); // Half steps
|
||||
expect(getDecimalPlaces(1)).toBe(0); // Whole steps
|
||||
expect(getDecimalPlaces(2)).toBe(0); // Even steps
|
||||
expect(getDecimalPlaces(5)).toBe(0); // Five steps
|
||||
expect(getDecimalPlaces(10)).toBe(0); // Ten steps
|
||||
expect(getDecimalPlaces(25)).toBe(0); // Twenty-five steps
|
||||
expect(getDecimalPlaces(50)).toBe(0); // Fifty steps
|
||||
expect(getDecimalPlaces(100)).toBe(0); // Hundred steps
|
||||
});
|
||||
});
|
||||
|
||||
describe('floating-point representation', () => {
|
||||
test('should handle standard floating-point representation', () => {
|
||||
expect(getDecimalPlaces(1.1)).toBe(1);
|
||||
expect(getDecimalPlaces(1.2)).toBe(1);
|
||||
expect(getDecimalPlaces(1.3)).toBe(1);
|
||||
});
|
||||
|
||||
test('should handle numbers that might have floating-point issues', () => {
|
||||
// 0.1 + 0.2 = 0.30000000000000004 in JS
|
||||
const sum = 0.1 + 0.2;
|
||||
const places = getDecimalPlaces(sum);
|
||||
// The function analyzes the string representation
|
||||
expect(typeof places).toBe('number');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user