34 lines
1.7 KiB
TypeScript
34 lines
1.7 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
import { stepNumericValue } from '../src/admin/components/ui/number-step'
|
|
|
|
describe('stepNumericValue', () => {
|
|
test('increments and decrements integers without drifting', () => {
|
|
expect(stepNumericValue({ rawValue: '2', direction: 1 })).toBe('3')
|
|
expect(stepNumericValue({ rawValue: '2', direction: -1 })).toBe('1')
|
|
})
|
|
|
|
test('avoids IEEE-754 precision drift with decimal steps', () => {
|
|
expect(stepNumericValue({ rawValue: '1.10', direction: 1, step: '0.01' })).toBe('1.11')
|
|
expect(stepNumericValue({ rawValue: '1.1', direction: -1, step: 0.1 })).toBe('1')
|
|
expect(stepNumericValue({ rawValue: '0.3', direction: -1, step: 0.1 })).toBe('0.2')
|
|
})
|
|
|
|
test('defaults step="any" to 1', () => {
|
|
expect(stepNumericValue({ rawValue: '12.5', direction: 1, step: 'any' })).toBe('13.5')
|
|
expect(stepNumericValue({ rawValue: '12.5', direction: -1, step: 'any' })).toBe('11.5')
|
|
})
|
|
|
|
test('clamps results to min and max bounds', () => {
|
|
expect(stepNumericValue({ rawValue: '1', direction: -1, min: 1, max: 3650 })).toBe('1')
|
|
expect(stepNumericValue({ rawValue: '3650', direction: 1, min: 1, max: 3650 })).toBe('3650')
|
|
expect(stepNumericValue({ rawValue: '-90', direction: -1, min: -90, max: 90 })).toBe('-90')
|
|
expect(stepNumericValue({ rawValue: '90', direction: 1, min: -90, max: 90 })).toBe('90')
|
|
})
|
|
|
|
test('handles empty input consistently with bounds', () => {
|
|
expect(stepNumericValue({ rawValue: '', direction: 1 })).toBe('0')
|
|
expect(stepNumericValue({ rawValue: '', direction: 1, min: 1, max: 100 })).toBe('1')
|
|
expect(stepNumericValue({ rawValue: '', direction: -1, min: -100, max: -5 })).toBe('-5')
|
|
})
|
|
})
|