Skip to content

Commit c48c216

Browse files
committed
feat(pxtorem): add validation for number type and update tests for error handling
1 parent 10202ad commit c48c216

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

packages/pxtorem/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type LengthValue, type LengthUnit } from 'lightningcss'
2-
import { validatePositiveInteger } from './utils/errorHandler'
2+
import { validateIsNumber, validatePositiveInteger } from './utils/errorHandler'
33
import type { Config } from './contracts/config.type'
44

55
const defultConfig: Config = {
@@ -21,6 +21,9 @@ function pxtorem(config: Partial<Config> = {}) {
2121

2222
validatePositiveInteger(rootValue, 'rootValue')
2323
validatePositiveInteger(unitPrecision, 'unitPrecision')
24+
validateIsNumber(rootValue, 'rootValue')
25+
validateIsNumber(unitPrecision, 'unitPrecision')
26+
validateIsNumber(minValue, 'minValue')
2427

2528
const toFixed = (value: number, precision: number): number => {
2629
const factor = Math.pow(10, precision)

packages/pxtorem/src/utils/errorHandler.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
export function validatePositiveInteger(value: number | undefined, name: string) {
1+
export function validateIsNumber(value: number | undefined, name: string) {
22
if (typeof value !== 'number' || isNaN(value)) {
33
throw new Error(`Invalid ${name}: must be a valid number.`)
44
}
5+
}
56

7+
export function validatePositiveInteger(value: number, name: string) {
68
if (value < 0) {
79
throw new Error(`Invalid ${name}: must not be negative.`)
810
}

packages/pxtorem/test/index.test.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from 'vitest'
22
import { composeVisitors, transform } from 'lightningcss'
33
import pxtorem from '../src/index'
4-
import { validatePositiveInteger } from '../src/utils/errorHandler'
4+
import { validatePositiveInteger, validateIsNumber } from '../src/utils/errorHandler'
55

66
describe('pxtorem plugin', () => {
77
it('should convert pixel to rem using default configuration', () => {
@@ -251,24 +251,33 @@ describe('pxtorem plugin', () => {
251251
})
252252
})
253253

254-
describe('validatePositiveInteger', () => {
255-
it('should throw error for undefined value', () => {
256-
expect(() => validatePositiveInteger(undefined, 'rootValue')).toThrowError('Invalid rootValue: must be a valid number.')
257-
})
258-
259-
it('should throw error for NaN value', () => {
260-
expect(() => validatePositiveInteger(NaN, 'rootValue')).toThrowError('Invalid rootValue: must be a valid number.')
261-
})
262-
254+
describe('validate errors', () => {
263255
it('should throw error for negative value', () => {
264256
expect(() => validatePositiveInteger(-1, 'rootValue')).toThrowError('Invalid rootValue: must not be negative.')
265257
})
266258

267259
it('should throw error for decimal value', () => {
268-
expect(() => validatePositiveInteger(1.5, 'rootValue')).toThrowError('Invalid rootValue: must not be a decimal.')
260+
expect(() => validatePositiveInteger(1.5, 'unitPrecision')).toThrowError('Invalid unitPrecision: must not be a decimal.')
269261
})
270262

271263
it('should pass for valid integer value', () => {
272-
expect(() => validatePositiveInteger(1, 'rootValue')).not.toThrow()
264+
expect(() => validatePositiveInteger(1, 'minValue')).not.toThrow()
265+
})
266+
267+
it('should throw error for non-number value', () => {
268+
// @ts-expect-error
269+
expect(() => validateIsNumber('10', 'rootValue')).toThrowError('Invalid rootValue: must be a valid number.')
270+
})
271+
272+
it('should pass for valid number value', () => {
273+
expect(() => validateIsNumber(1, 'unitPrecision')).not.toThrow()
274+
})
275+
276+
it('should pass for valid negative number value', () => {
277+
expect(() => validateIsNumber(-10, 'rootValue')).not.toThrow()
278+
})
279+
280+
it('should pass for valid float number value', () => {
281+
expect(() => validateIsNumber(10.50, 'minValue')).not.toThrow()
273282
})
274283
})

0 commit comments

Comments
 (0)