From 9ccd7135338ca3d5c64c6f0491ab73c06b1b39c9 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Thu, 24 Oct 2024 12:05:29 +0100 Subject: [PATCH] fix: careful now --- src/__tests__/utils/number-utils.test.ts | 12 ++++++++++++ src/utils/number-utils.ts | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/__tests__/utils/number-utils.test.ts b/src/__tests__/utils/number-utils.test.ts index 9647caf6d..306d8e64b 100644 --- a/src/__tests__/utils/number-utils.test.ts +++ b/src/__tests__/utils/number-utils.test.ts @@ -1,4 +1,11 @@ import { clampToRange } from '../../utils/number-utils' +import { logger } from '../../utils/logger' + +jest.mock('../../utils/logger', () => ({ + logger: { + warn: jest.fn(), + }, +})) describe('number-utils', () => { describe('clampToRange', () => { @@ -13,5 +20,10 @@ describe('number-utils', () => { const result = clampToRange(value, min, max, 'Test Label') expect(result).toBe(expected) }) + + it('logs a warning when min is greater than max', () => { + expect(clampToRange(50, 100, 10, 'Test Label')).toBe(10) + expect(logger.warn).toHaveBeenCalledWith('min cannot be greater than max.') + }) }) }) diff --git a/src/utils/number-utils.ts b/src/utils/number-utils.ts index e3ec455b8..7c59b67d0 100644 --- a/src/utils/number-utils.ts +++ b/src/utils/number-utils.ts @@ -2,6 +2,11 @@ import { isNumber } from './type-utils' import { logger } from './logger' export function clampToRange(value: unknown, min: number, max: number, label?: string): number { + if (min > max) { + logger.warn('min cannot be greater than max.') + min = max + } + if (!isNumber(value)) { label && logger.warn(label + ' must be a number. Defaulting to max value:' + max) return max