Skip to content

Commit

Permalink
test: add tests for color
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoublev committed May 13, 2021
1 parent 61c99fd commit 4d25f8f
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions lib/parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,47 @@ describe('parseString', () => {
it.todo('test');
});
describe('parseColor', () => {
it('returns undefined for invalid values', () => {
const invalid = [
'invalid',
'#ffz',
'#1',
'#12',
'#12345',
'#1234567',
'#123456789',
'rg(0, 0, 0)',
'rgbo(0, 0, 0)',
'rgb(0, 0)',
'rgb(0, 0 0)',
'rgb(0%, 0, 0)',
'rgb(0, 1deg, 1px)',
'rgba(0, 1deg, 1px, invalid)',
'rgba(0 0 0 0)',
'rgba(0, 0, 0 / 0)',
'hs(0, 0, 0)',
'hslo(0, 0, 0)',
'hsl(0, 0)',
'hsl(0, 0 0)',
'hsl(0%, 0, 0)',
'hsl(0, 1deg, 1px)',
'hsla(0, 1deg, 1px, invalid)',
'hsla(0 0 0 0)',
'hsla(0, 0, 0 / 0)',
];
invalid.forEach(input => expect(parsers.parseColor(input)).toBeUndefined());
});
it('resolves with lowercased color name or function name', () => {
expect(parsers.parseColor('RED')).toBe('red');
expect(parsers.parseColor('RGb(0, 0, 0)')).toBe('rgb(0, 0, 0)');
});
it('should convert hex to rgba values', () => {
expect(parsers.parseColor('#F00')).toBe('rgb(255, 0, 0)');
expect(parsers.parseColor('#0f06')).toBe('rgba(0, 255, 0, 0.4)');
expect(parsers.parseColor('#0000ff')).toBe('rgb(0, 0, 255)');
expect(parsers.parseColor('#ff00ffff')).toBe('rgb(255, 0, 255)');
expect(parsers.parseColor('#ff00ff66')).toBe('rgba(255, 0, 255, 0.4)');
});
it('should convert hsl to rgb values', () => {
let input = 'hsla(0, 1%, 2%)';
let output = parsers.parseColor(input);
Expand All @@ -236,8 +277,25 @@ describe('parseColor', () => {

expect(output).toEqual('rgba(5, 5, 5, 0.5)');
});

it.todo('Add more tests');
it('clamps overflowing rgb values', () => {
expect(parsers.parseColor('rgb(300, 300, 300, 2)')).toBe('rgb(255, 255, 255)');
expect(parsers.parseColor('rgb(-1, -1, -1, -1)')).toBe('rgba(0, 0, 0, 0)');
expect(parsers.parseColor('hsl(540, 100%, 50%)')).toBe('rgb(0, 255, 255)');
expect(parsers.parseColor('hsla(400, 200%, 200%, 200%)')).toBe('rgb(255, 255, 255)');
expect(parsers.parseColor('hsla(-20deg, -1%, -1%, -1%)')).toBe('rgba(0, 0, 0, 0)');
});
it('preserves precision', () => {
expect(parsers.parseColor('rgba(245.5, 245.5, 0, 50.1%)')).toBe('rgba(246, 246, 0, 0.5)');
expect(parsers.parseColor('rgba(245.5, 245.5, 0, 49.9%)')).toBe('rgba(246, 246, 0, 0.498)');
});
it('works with calc()', () => {
expect(parsers.parseColor('rgb(calc(0 + 255), 0, calc(0 + calc(0 + 255)))')).toBe(
'rgb(255, 0, 255)'
);
expect(parsers.parseColor('hsl(calc(0 + 300), 100%, calc(0% + calc(0% + 50%)))')).toBe(
'rgb(255, 0, 255)'
);
});
});
describe('dashedToCamelCase', () => {
it.todo('test');
Expand Down

0 comments on commit 4d25f8f

Please sign in to comment.