Skip to content

Commit 61c99fd

Browse files
committed
test: add tests for invalid numeric types
1 parent 0e60b3c commit 61c99fd

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

lib/parsers.test.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
const parsers = require('./parsers');
44

55
describe('parseInteger', () => {
6+
it('returns undefined for invalid values', () => {
7+
const invalid = ['string', '1px', '1%', '#1', '1.1', '.1', '1e-1', 'calc(1 / 2)'];
8+
invalid.forEach(input => expect(parsers.parseInteger(input)).toBeUndefined());
9+
});
610
it('parses integer with exponent', () => {
711
expect(parsers.parseInteger('1e1')).toBe('10');
812
expect(parsers.parseInteger('1e+1')).toBe('10');
913
});
1014
it('works with calc', () => {
1115
expect(parsers.parseInteger('calc(1 + 1)')).toBe('2');
1216
});
13-
it.todo('more tests');
1417
});
1518
describe('parseNumber', () => {
19+
it('returns undefined for invalid values', () => {
20+
const invalid = ['string', '1px', '1%', '#1', 'calc(1 * 1px)'];
21+
invalid.forEach(input => expect(parsers.parseNumber(input)).toBeUndefined());
22+
});
1623
it('parses number with exponent', () => {
1724
expect(parsers.parseNumber('1e1')).toBe('10');
1825
expect(parsers.parseNumber('1e+1')).toBe('10');
@@ -27,9 +34,12 @@ describe('parseNumber', () => {
2734
it('works with calc', () => {
2835
expect(parsers.parseNumber('calc(1.5 + 1.5)')).toBe('3');
2936
});
30-
it.todo('more tests');
3137
});
3238
describe('parseLength', () => {
39+
it('returns undefined for invalid values', () => {
40+
const invalid = ['string', '1', 'px', '1%', '#1px', '1pxx', 'calc(1 * 1%)'];
41+
invalid.forEach(input => expect(parsers.parseLength(input)).toBeUndefined());
42+
});
3343
it('parses 0 to 0px', () => {
3444
expect(parsers.parseLength('0')).toBe('0px');
3545
});
@@ -59,9 +69,12 @@ describe('parseLength', () => {
5969
it('works with calc', () => {
6070
expect(parsers.parseLength('calc(1px + 1px)')).toBe('calc(2px)');
6171
});
62-
it.todo('more tests');
6372
});
6473
describe('parsePercent', () => {
74+
it('returns undefined for invalid values', () => {
75+
const invalid = ['string', '1%%', '1px%', '#1%', 'calc(1 * 1px)'];
76+
invalid.forEach(input => expect(parsers.parsePercent(input)).toBeUndefined());
77+
});
6578
it('parses percent with exponent', () => {
6679
expect(parsers.parsePercent('1e1%')).toBe('10%');
6780
expect(parsers.parsePercent('1e+1%')).toBe('10%');
@@ -76,12 +89,15 @@ describe('parsePercent', () => {
7689
it('works with calc', () => {
7790
expect(parsers.parsePercent('calc(1% + 1%)')).toBe('calc(2%)');
7891
});
79-
it.todo('more tests');
8092
});
8193
describe('parseMeasurement', () => {
8294
it.todo('test');
8395
});
8496
describe('parseAngle', () => {
97+
it('returns undefined for invalid values', () => {
98+
const invalid = ['string', '1', '1degg', 'a1deg', 'deg', 'calc(1 * 1px)'];
99+
invalid.forEach(input => expect(parsers.parseAngle(input)).toBeUndefined());
100+
});
85101
it('parses 0 to 0deg', () => {
86102
expect(parsers.parseAngle('0')).toBe('0deg');
87103
});
@@ -110,9 +126,12 @@ describe('parseAngle', () => {
110126
it('works with calc', () => {
111127
expect(parsers.parseAngle('calc(1deg + 1deg)')).toBe('calc(2deg)');
112128
});
113-
it.todo('more tests');
114129
});
115130
describe('parseTime', () => {
131+
it('returns undefined for invalid values', () => {
132+
const invalid = ['string', '1', '1ss', 'a1s', 's', 'calc(1 * 1px)'];
133+
invalid.forEach(input => expect(parsers.parseTime(input)).toBeUndefined());
134+
});
116135
it('parses time with exponent', () => {
117136
expect(parsers.parseTime('1e1s')).toBe('10s');
118137
expect(parsers.parseTime('1e+1s')).toBe('10s');
@@ -133,7 +152,6 @@ describe('parseTime', () => {
133152
it('works with calc', () => {
134153
expect(parsers.parseTime('calc(1s + 1s)')).toBe('calc(2s)');
135154
});
136-
it.todo('more tests');
137155
});
138156
describe('parseCalc', () => {
139157
it.skip('return undefined with 0 and <dimension-percentage> as operands', () => {

0 commit comments

Comments
 (0)