forked from rugk/offline-qr-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.test.js
122 lines (98 loc) · 5.68 KB
/
colors.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
import "https://unpkg.com/[email protected]/mocha.js"; /* globals mocha */
import "https://unpkg.com/[email protected]/chai.js"; /* globals chai */
import * as Colors from "/common/modules/Colors.js";
const COLOR_ARRAY = Object.freeze({
// contrast 1
WHITE: [255, 255, 255],
BLACK: [0, 0, 0],
// contrast 2
"#00FEFF": [0, 254, 255], // nearly Cyan (#00FFFF), teal50 from Firefox Photon
"#FF0000": [255, 0, 0], // red
// contrast 3
"#4F477D": [79, 71, 125],
"#757D47": [17, 125, 71]
});
describe("common module: Color", function () {
describe("CONTRAST_RATIO", function () {
it("is there", function () {
chai.assert.exists(Colors.CONTRAST_RATIO);
chai.assert.isNotEmpty(Colors.CONTRAST_RATIO);
});
it("is frozen", function () {
chai.assert.isFrozen(Colors.CONTRAST_RATIO);
});
});
describe("contrastRatio()", function () {
const ALLOWED_DELTA = 0.01;
it("returns minimum contrast ratio for same color", function () {
chai.assert.strictEqual(Colors.contrastRatio(COLOR_ARRAY.WHITE, COLOR_ARRAY.WHITE), 1);
chai.assert.strictEqual(Colors.contrastRatio(COLOR_ARRAY.BLACK, COLOR_ARRAY.BLACK), 1);
chai.assert.strictEqual(Colors.contrastRatio(COLOR_ARRAY["#00FEFF"], COLOR_ARRAY["#00FEFF"]), 1);
});
it("returns maxiumum contrast ratio for black/white", function () {
chai.assert.strictEqual(Colors.contrastRatio(COLOR_ARRAY.WHITE, COLOR_ARRAY.BLACK), 21);
chai.assert.strictEqual(Colors.contrastRatio(COLOR_ARRAY.BLACK, COLOR_ARRAY.WHITE), 21);
});
it("returns correct contrast ratio for #00FEFF/white (low)", function () {
chai.assert.approximately(Colors.contrastRatio(COLOR_ARRAY["#00FEFF"], COLOR_ARRAY.WHITE), 1.26, ALLOWED_DELTA);
chai.assert.approximately(Colors.contrastRatio(COLOR_ARRAY.WHITE, COLOR_ARRAY["#00FEFF"]), 1.26, ALLOWED_DELTA);
});
it("returns correct contrast ratio for #00FEFF/black (high)", function () {
chai.assert.approximately(Colors.contrastRatio(COLOR_ARRAY["#00FEFF"], COLOR_ARRAY.BLACK), 16.62, ALLOWED_DELTA);
chai.assert.approximately(Colors.contrastRatio(COLOR_ARRAY.BLACK, COLOR_ARRAY["#00FEFF"]), 16.62, ALLOWED_DELTA);
});
it("returns correct contrast ratio for #00FEFF/#4F477D (medium)", function () {
chai.assert.approximately(Colors.contrastRatio(COLOR_ARRAY["#00FEFF"], COLOR_ARRAY["#4F477D"]), 6.57, ALLOWED_DELTA);
chai.assert.approximately(Colors.contrastRatio(COLOR_ARRAY["#4F477D"], COLOR_ARRAY["#00FEFF"]), 6.57, ALLOWED_DELTA);
});
});
describe("invertColor()", function () {
it("inverts white to black", function () {
chai.assert.strictEqual(Colors.invertColor(COLOR_ARRAY.WHITE), "#000000");
});
it("inverts black to white", function () {
chai.assert.strictEqual(Colors.invertColor(COLOR_ARRAY.BLACK), "#ffffff");
});
it("inverts #00FEFF and #FF0000 correctly", function () {
chai.assert.strictEqual(Colors.invertColor(COLOR_ARRAY["#00FEFF"]), "#ff0100");
chai.assert.strictEqual(Colors.invertColor(COLOR_ARRAY["#FF0000"]), "#00ffff");
});
it("inverts #4F477D and #757D47 correctly", function () {
chai.assert.strictEqual(Colors.invertColor(COLOR_ARRAY["#4F477D"]), "#b0b882");
chai.assert.strictEqual(Colors.invertColor(COLOR_ARRAY["#757D47"]), "#ee82b8");
});
});
describe("hexToRgb()", function () {
it("splits color correctly: black", function () {
chai.assert.deepEqual(Colors.hexToRgb("#000000"), COLOR_ARRAY.BLACK, "hex with # does not work");
chai.assert.deepEqual(Colors.hexToRgb("000000"), COLOR_ARRAY.BLACK, "hex without # does not work");
});
it("splits color correctly: white uppercase", function () {
chai.assert.deepEqual(Colors.hexToRgb("#FFFFFF"), COLOR_ARRAY.WHITE, "hex with # does not work");
chai.assert.deepEqual(Colors.hexToRgb("FFFFFF"), COLOR_ARRAY.WHITE, "hex without # does not work");
});
it("splits color correctly: white lowercase", function () {
chai.assert.deepEqual(Colors.hexToRgb("#ffffff"), COLOR_ARRAY.WHITE, "hex with # does not work");
chai.assert.deepEqual(Colors.hexToRgb("ffffff"), COLOR_ARRAY.WHITE, "hex without # does not work");
});
it("splits color correctly: #4f477D", function () {
chai.assert.deepEqual(Colors.hexToRgb("#4f477D"), COLOR_ARRAY["#4F477D"], "hex with # does not work");
chai.assert.deepEqual(Colors.hexToRgb("4f477D"), COLOR_ARRAY["#4F477D"], "hex without # does not work");
});
it("splits color correctly: #00feff", function () {
chai.assert.deepEqual(Colors.hexToRgb("#00feff"), COLOR_ARRAY["#00FEFF"], "hex with # does not work");
chai.assert.deepEqual(Colors.hexToRgb("00feff"), COLOR_ARRAY["#00FEFF"], "hex without # does not work");
});
it("split color fails for invalid values: obvious ones", function () {
chai.assert.strictEqual(Colors.hexToRgb("4"), null);
chai.assert.strictEqual(Colors.hexToRgb(null), null);
chai.assert.strictEqual(Colors.hexToRgb("#"), null);
});
it("split color fails for not supported three-letter colors", function () {
chai.assert.strictEqual(Colors.hexToRgb("#000"), null);
chai.assert.strictEqual(Colors.hexToRgb("000"), null);
chai.assert.strictEqual(Colors.hexToRgb("#123"), null);
chai.assert.strictEqual(Colors.hexToRgb("123"), null);
});
});
});