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 Jun 10, 2021
1 parent 9b5752c commit e0c0f88
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 @@ -283,6 +283,47 @@ describe('parseString', () => {
it.todo('more tests');
});
describe('parseColor', () => {
it('returns null 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)).toBeNull());
});
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 @@ -295,6 +336,25 @@ describe('parseColor', () => {

expect(output).toBe('rgba(5, 5, 5, 0.5)');
});
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)'
);
});
it('works with custom variable', () => {
expect(parsers.parseColor('rgb(var(--')).toBe('rgb(var(--');
expect(parsers.parseColor('hsl(var(--')).toBe('hsl(var(--');
Expand All @@ -305,8 +365,6 @@ describe('parseColor', () => {
'hsl(var(--hue), var(--sat), 0, var(--alpha))'
);
});

it.todo('Add more tests');
});
describe('parseCustomVariable', () => {
it('returns null for invalid values', () => {
Expand Down

0 comments on commit e0c0f88

Please sign in to comment.