-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.spec.ts
125 lines (114 loc) · 2.97 KB
/
color.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as colors from '@src/color';
describe('hexToRgb test', () => {
test('hexToRgb, param is not hex color', () => {
const color = '0xffff000';
expect(() => {
colors.hexToRgb(color);
}).toThrow();
});
test('hexToRgb, white color', () => {
const color = '#fff';
expect(colors.hexToRgb(color)).toEqual([255, 255, 255]);
});
});
describe('rgbToHex test', () => {
test('rgbToHex', () => {
const color = 'rgb(2 255 255)';
const result = colors.rgbToHex(color);
expect(result).toBe('#02ffff');
});
});
describe('rgbToHSL test', () => {
test('rgbToHsl', () => {
const colorsArr = [
{
rgb: 'rgb(255 255 255 / 10%)',
hsl: [0, 0, 100, 0.1],
},
{
rgb: 'rgb(255 0 0 / 10%)',
hsl: [0, 100, 50, 0.1],
},
{
rgb: 'rgb(0 255 0 / 10%)',
hsl: [120, 100, 50, 0.1],
},
{
rgb: 'rgb(0 0 255 / 10%)',
hsl: [240, 100, 50, 0.1],
},
];
for (let i = 0, len = colorsArr.length; i < len; i++) {
expect(colors.rgbToHsl(colorsArr[i].rgb)).toEqual(colorsArr[i].hsl);
}
});
});
describe('hslToRgb test', () => {
test('hslToRgb with alpha', () => {
const color = 'hsla(120deg, 100%, 50%, 10%)';
const result = colors.hslToRgb(color);
expect(result).toEqual([0, 255, 0, 0.1]);
});
test('hslToRgb no alpha', () => {
const color = 'hsla(120deg 100% 50%)';
const result = colors.hslToRgb(color);
expect(result).toEqual([0, 255, 0]);
});
});
describe('auxiliary funcs test', () => {
test('colorType', () => {
const colorTypes = [
{
value: '#fff',
type: 'hex',
},
{
value: 'rgb(255, 255, 255)',
type: 'rgb',
},
{
value: 'rgba(255, 255, 255 , 0.1)',
type: 'rgb',
},
{
value: 'hsl(23deg, 10%, 14%)',
type: 'hsl',
},
{
value: 'hsla(23deg, 10%, 10%, 20%)',
type: 'hsl',
},
];
colorTypes.forEach((item) => {
expect(colors.colorType(item.value)).toBe(item.type);
});
});
test('toHSL', () => {
const inputColors = ['hsl(327deg 100% 30%)', '#990054', 'rgb(153 0 84)'];
inputColors.forEach((c) => {
expect(colors.toHSL(c)).toEqual([327, 100, 30]);
});
});
});
describe('isDarkColor test', () => {
test('isDarkColor', () => {
const color = '#990054';
expect(colors.isDarkColor(color)).toBeTruthy();
});
});
describe('randomHex test', () => {
it('should return a string', () => {
expect(typeof colors.randomHex()).toBe('string');
});
it('should return a 7 character hex color code', () => {
expect(colors.randomHex().length).toBe(7);
});
it('should return a valid hex color', () => {
expect(colors.randomHex()).toMatch(/^#([A-Fa-f0-9]{6})$/);
});
it('should return a different color each time', () => {
const color1 = colors.randomHex();
const color2 = colors.randomHex();
expect(color1).not.toBe(color2);
});
});