Skip to content

Commit c64a802

Browse files
committed
update propValidation, add tests
1 parent 46dfae2 commit c64a802

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

__tests__/propValidation.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { offsetMin, offsetMax } from 'utils/propValidation';
2+
3+
describe('offsetMin prop validation', () => {
4+
5+
it('returns null when provided a valid value', () => {
6+
expect(offsetMin({ offsetXMin: -100 }, 'offsetXMin', 'foo')).toBe(null);
7+
expect(offsetMin({ offsetXMin: '-10321px' }, 'offsetXMin', 'foo')).toBe(null);
8+
expect(offsetMin({ offsetXMin: '-10%' }, 'offsetXMin', 'foo')).toBe(null);
9+
});
10+
11+
it('throws when provided a invalid value type', () => {
12+
const error = new Error('[offsetXMin] in foo must be a string with with "%"" or "px" units or number.');
13+
expect(offsetMin({ offsetXMin: false }, 'offsetXMin', 'foo')).toEqual(error);
14+
expect(offsetMin({ offsetXMin: {} }, 'offsetXMin', 'foo')).toEqual(error);
15+
expect(offsetMin({ offsetXMin: null }, 'offsetXMin', 'foo')).toEqual(error);
16+
expect(offsetMin({ offsetXMin: undefined }, 'offsetXMin', 'foo')).toEqual(error);
17+
expect(offsetMin({ offsetXMin: [] }, 'offsetXMin', 'foo')).toEqual(error);
18+
});
19+
20+
it('throws when provided a invalid value', () => {
21+
const error = new Error('[offsetXMin] in foo is greater than zero. [offsetXMin] must be less than or equal to zero.')
22+
expect(offsetMin({ offsetXMin: 100 }, 'offsetXMin', 'foo')).toEqual(error);
23+
expect(offsetMin({ offsetXMin: 1 }, 'offsetXMin', 'foo')).toEqual(error);
24+
expect(offsetMin({ offsetXMin: 0.0009 }, 'offsetXMin', 'foo')).toEqual(error);
25+
expect(offsetMin({ offsetXMin: '100px' }, 'offsetXMin', 'foo')).toEqual(error);
26+
expect(offsetMin({ offsetXMin: '99%' }, 'offsetXMin', 'foo')).toEqual(error);
27+
});
28+
29+
});
30+
31+
describe('offsetMax prop validation', () => {
32+
33+
it('returns null when provided a valid value', () => {
34+
expect(offsetMax({ offsetXMax: 100 }, 'offsetXMax', 'foo')).toBe(null);
35+
expect(offsetMax({ offsetXMax: '10321px' }, 'offsetXMax', 'foo')).toBe(null);
36+
expect(offsetMax({ offsetXMax: '10%' }, 'offsetXMax', 'foo')).toBe(null);
37+
});
38+
39+
it('throws when provided a invalid value type', () => {
40+
const error = new Error('[offsetXMax] in foo must be a string with with "%"" or "px" units or number.');
41+
expect(offsetMax({ offsetXMax: false }, 'offsetXMax', 'foo')).toEqual(error);
42+
expect(offsetMax({ offsetXMax: {} }, 'offsetXMax', 'foo')).toEqual(error);
43+
expect(offsetMax({ offsetXMax: null }, 'offsetXMax', 'foo')).toEqual(error);
44+
expect(offsetMax({ offsetXMax: undefined }, 'offsetXMax', 'foo')).toEqual(error);
45+
expect(offsetMax({ offsetXMax: [] }, 'offsetXMax', 'foo')).toEqual(error);
46+
});
47+
48+
it('throws when provided a invalid value', () => {
49+
const error = new Error('[offsetXMax] in foo is less than zero. [offsetXMax] must be greater than or equal to zero.')
50+
expect(offsetMax({ offsetXMax: -100 }, 'offsetXMax', 'foo')).toEqual(error);
51+
expect(offsetMax({ offsetXMax: -1 }, 'offsetXMax', 'foo')).toEqual(error);
52+
expect(offsetMax({ offsetXMax: -0.0009 }, 'offsetXMax', 'foo')).toEqual(error);
53+
expect(offsetMax({ offsetXMax: '-100px' }, 'offsetXMax', 'foo')).toEqual(error);
54+
expect(offsetMax({ offsetXMax: '-99%' }, 'offsetXMax', 'foo')).toEqual(error);
55+
});
56+
57+
});

src/utils/propValidation.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
export function offsetMin(props, propName, componentName) {
2-
componentName = componentName || 'ANONYMOUS';
1+
export function offsetMin(props, propName, componentName = 'ANONYMOUS') {
2+
let value = props[propName];
3+
const isValid = typeof value === 'string' || typeof value === 'number';
34

4-
if (!typeof value === 'string' || !typeof value === 'number') {
5+
if (!isValid) {
56
return new Error(
6-
`[${propName}] in ${componentName} must be a string with with "%"" or "px" units or number`
7+
`[${propName}] in ${componentName} must be a string with with "%"" or "px" units or number.`
78
);
89
}
910

1011
if (props[propName]) {
11-
let value = props[propName];
1212
if (typeof value === 'string') {
1313
value = parseInt(value, 10);
1414
}
@@ -21,17 +21,17 @@ export function offsetMin(props, propName, componentName) {
2121
return null;
2222
}
2323

24-
export function offsetMax(props, propName, componentName) {
25-
componentName = componentName || 'ANONYMOUS';
24+
export function offsetMax(props, propName, componentName = 'ANONYMOUS') {
25+
let value = props[propName];
26+
const isValid = typeof value === 'string' || typeof value === 'number';
2627

27-
if (!typeof value === 'string' || !typeof value === 'number') {
28+
if (!isValid) {
2829
return new Error(
29-
`[${propName}] in ${componentName} must be a string with with "%"" or "px" units or number`
30+
`[${propName}] in ${componentName} must be a string with with "%"" or "px" units or number.`
3031
);
3132
}
3233

3334
if (props[propName]) {
34-
let value = props[propName];
3535
if (typeof value === 'string') {
3636
value = parseInt(value, 10);
3737
}

0 commit comments

Comments
 (0)