2021-03-11 19:13:27 +05:30
|
|
|
import { textColorForBackground, hexToRgb, validateHexColor } from '~/lib/utils/color_utils';
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
describe('Color utils', () => {
|
|
|
|
describe('Converting hex code to rgb', () => {
|
|
|
|
it('convert hex code to rgb', () => {
|
|
|
|
expect(hexToRgb('#000000')).toEqual([0, 0, 0]);
|
|
|
|
expect(hexToRgb('#ffffff')).toEqual([255, 255, 255]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('convert short hex code to rgb', () => {
|
|
|
|
expect(hexToRgb('#000')).toEqual([0, 0, 0]);
|
|
|
|
expect(hexToRgb('#fff')).toEqual([255, 255, 255]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handle conversion regardless of the characters case', () => {
|
|
|
|
expect(hexToRgb('#f0F')).toEqual([255, 0, 255]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Getting text color for given background', () => {
|
|
|
|
// following tests are being ported from `text_color_for_bg` section in labels_helper_spec.rb
|
|
|
|
it('uses light text on dark backgrounds', () => {
|
|
|
|
expect(textColorForBackground('#222E2E')).toEqual('#FFFFFF');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses dark text on light backgrounds', () => {
|
|
|
|
expect(textColorForBackground('#EEEEEE')).toEqual('#333333');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports RGB triplets', () => {
|
|
|
|
expect(textColorForBackground('#FFF')).toEqual('#333333');
|
|
|
|
expect(textColorForBackground('#000')).toEqual('#FFFFFF');
|
|
|
|
});
|
|
|
|
});
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
describe('Validate hex color', () => {
|
|
|
|
it.each`
|
|
|
|
color | output
|
|
|
|
${undefined} | ${null}
|
|
|
|
${null} | ${null}
|
|
|
|
${''} | ${null}
|
|
|
|
${'ABC123'} | ${false}
|
|
|
|
${'#ZZZ'} | ${false}
|
|
|
|
${'#FF0'} | ${true}
|
|
|
|
${'#FF0000'} | ${true}
|
|
|
|
`('returns $output when $color is given', ({ color, output }) => {
|
|
|
|
expect(validateHexColor(color)).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
2019-10-12 21:52:04 +05:30
|
|
|
});
|