-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
215 lines (193 loc) · 4.46 KB
/
test.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import { convert, sort, sortFn } from './index.js'
test('it exposes a basic sort function', () => {
assert.is(typeof sort, 'function')
})
test('it exposes a convert function', () => {
assert.is(typeof convert, 'function')
})
test('it exposes a sortFn', () => {
assert.is(typeof sortFn, 'function')
})
test('the convert fn converts colors to an HSLA object', () => {
const colors = [
'red',
'hsla(0, 100%, 50%, 1)',
'hsl(0, 100%, 50%)',
'rgb(255, 0, 0)',
'rgba(255, 0, 0, 1)',
'oklch(62.8% 0.25768330773615683 29.2338851923426)'
]
for (let color of colors) {
let converted = convert(color)
// Making sure most colors are mostly within the range
assert.ok(converted.hue >= 0 && converted.hue <= 0.01, `Failed hue for '${color}', got ${converted.hue}`)
assert.ok(converted.saturation >= 99.9 && converted.saturation <= 100.02, `Failed saturation for '${color}', got ${converted.saturation}`)
assert.ok(converted.lightness >= 49.9 && converted.lightness <= 50.02, `Failed lightness for '${color}', got ${converted.lightness}`)
assert.equal(converted.alpha, 1, `Failed alpha for '${color}'`)
assert.equal(converted.authored, color, `Failed authored for '${color}'`)
}
})
test('invalid colors return a default object', () => {
const colors = [
'invalid',
'hsl(0, 0, 0)',
]
for (let color of colors) {
assert.equal(convert(color), {
hue: 0,
saturation: 0,
lightness: 0,
alpha: 0,
authored: color
}, `Failed convert for '${color}'`)
}
})
test('Colors are sorted by Hue', () => {
const colors = [
'hsl(0, 100%, 50%)',
'hsl(200, 100%, 50%)',
'hsl(50, 100%, 50%)',
'hsl(10, 100%, 50%)',
'hsl(100, 100%, 50%)'
]
const expected = [
'hsl(0, 100%, 50%)',
'hsl(10, 100%, 50%)',
'hsl(50, 100%, 50%)',
'hsl(100, 100%, 50%)',
'hsl(200, 100%, 50%)'
]
const actual = sort(colors)
assert.equal(actual, expected)
})
test('Colors are sorted by Hue, then by saturation', () => {
const colors = [
'hsl(0, 100%, 50%)',
'hsl(0, 50%, 50%)',
'hsl(50, 20%, 50%)',
'hsl(50, 100%, 50%)'
]
const expected = [
'hsl(0, 50%, 50%)',
'hsl(0, 100%, 50%)',
'hsl(50, 20%, 50%)',
'hsl(50, 100%, 50%)'
]
const actual = sort(colors)
assert.equal(actual, expected)
})
test('Grey-ish values are shifted to the end (lightest first)', () => {
const colors = [
'hsl(0, 0%, 0%)', // Black
'hsl(0, 100%, 50%)', // Red,
'hsl(0, 0%, 100%)', // White
'hsl(240, 100%, 50%)' // Blue
]
const expected = [
'hsl(0, 100%, 50%)', // Red
'hsl(240, 100%, 50%)', // Blue
'hsl(0, 0%, 100%)', // White
'hsl(0, 0%, 0%)', // Black
]
const actual = sort(colors)
assert.equal(actual, expected)
})
test('Grey-ish colors are sorted by Lightness', () => {
// The key here is that saturation (the middle value in HSL)
// equals 0
const colors = [
'#000',
'#fff',
'#eee',
'#555',
'#222'
]
const expected = [
'#fff',
'#eee',
'#555',
'#222',
'#000'
]
const actual = sort(colors)
assert.equal(actual, expected)
})
test('Grey-ish colors are sorted by Lightness, then by Alpha', () => {
const colors = [
'hsla(0, 0%, 20%, 1)',
'hsla(0, 0%, 10%, 1)',
'hsla(0, 0%, 10%, 0)',
'hsla(0, 0%, 0%, 0)'
]
const expected = [
'hsla(0, 0%, 20%, 1)',
'hsla(0, 0%, 10%, 1)',
'hsla(0, 0%, 10%, 0)',
'hsla(0, 0%, 0%, 0)',
]
const actual = sort(colors)
assert.equal(actual, expected)
})
test('Fully transparent colors are sorted along their opaque companions', () => {
const colors = ['rgba(255, 0, 0, 0)', 'hsla(0, 100%, 50%, 0.1)', 'red']
const actual = sort(colors)
const expected = ['red', 'hsla(0, 100%, 50%, 0.1)', 'rgba(255, 0, 0, 0)']
assert.equal(actual, expected)
})
test('smoke test', () => {
const colors = [
'#4b4747',
'#f00',
'#d70c0b',
'#f22b24',
'#ff6930',
'#eb6c1e',
'#eb6d1e',
'#f57917',
'#ff8a0a',
'#f7a336',
'#feb95a',
'#eca920',
'#f1c15d',
'#f1c260',
'#ff0',
'#c8d05b',
'#ccd557',
'#d2ff52',
'#10ac47',
'#04a03b',
'#03fff3',
'#38d7df',
'#25bbc3',
'#15b8ec',
'#00adea',
'#8e34c9',
'#9a3dd1',
'#cd66f6',
'#fff',
'rgba(255,255,255,0.2)',
'rgba(255,255,255,0.07)',
'#f9f9f9',
'#f4f4f4',
'#f2f2f2',
'#e4e4e4',
'#ddd',
'#c0c0c0',
'#666',
'#4a4a4a',
'#1d1d1d',
'#0d0d0d',
'#000',
'rgba(0,0,0,0.8)',
'rgba(0,0,0,0.6)',
'rgba(0,0,0,0.4)',
'rgba(0,0,0,0.1)',
'rgba(0,0,0,0.05)'
]
const expected = [...colors]
const actual = sort(colors)
assert.equal(actual, expected)
})
test.run()