-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
prefix-color-selector.spec.ts
189 lines (163 loc) · 6.31 KB
/
prefix-color-selector.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
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
import { PrefixColorSelector } from './prefix-color-selector';
function assertSelectedColors({
prefixColorSelector,
expectedColors,
}: {
prefixColorSelector: PrefixColorSelector;
expectedColors: string[];
}) {
expectedColors.forEach(expectedColor => {
expect(prefixColorSelector.getNextColor()).toBe(expectedColor);
});
}
afterEach(() => {
jest.restoreAllMocks();
});
describe('#getNextColor', function () {
it('does not produce a color if prefixColors empty', () => {
assertSelectedColors({
prefixColorSelector: new PrefixColorSelector([]),
expectedColors: ['', '', ''],
});
});
it('does not produce a color if prefixColors undefined', () => {
assertSelectedColors({
prefixColorSelector: new PrefixColorSelector(),
expectedColors: ['', '', ''],
});
});
it('uses user defined prefix colors only, if no auto is used', () => {
assertSelectedColors({
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'blue']),
expectedColors: [
'red',
'green',
'blue',
// uses last color if last color is not "auto"
'blue',
'blue',
'blue',
],
});
});
it('uses picks colors when user defines an auto color', () => {
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([
'green',
'blue',
]);
assertSelectedColors({
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'auto', 'orange']),
expectedColors: [
// custom colors
'red',
'green',
'blue', // picks auto color, not repeating consecutive "green" color
'orange',
// uses last color if last color is not "auto"
'orange',
'orange',
'orange',
],
});
});
it('uses user defined colors then recurring auto colors without repeating consecutive colors', () => {
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([
'green',
'blue',
]);
assertSelectedColors({
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'auto']),
expectedColors: [
// custom colors
'red',
'green',
// picks auto colors, not repeating consecutive "green" color
'blue',
'green',
'blue',
'green',
],
});
});
it('can sometimes produce consecutive colors', () => {
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([
'green',
'blue',
]);
assertSelectedColors({
prefixColorSelector: new PrefixColorSelector(['blue', 'auto']),
expectedColors: [
// custom colors
'blue',
// picks auto colors
'green',
// does not repeat custom colors for initial auto colors, ie does not use "blue" again so soon
'green', // consecutive color picked, however practically there would be a lot of colors that need to be set in a particular order for this to occur
'blue',
'green',
'blue',
'green',
'blue',
],
});
});
it('considers the Bright variants of colors equal to the normal colors to avoid similar colors', function () {
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([
'greenBright',
'blueBright',
'green',
'blue',
'magenta',
]);
assertSelectedColors({
prefixColorSelector: new PrefixColorSelector(['green', 'blue', 'auto']),
expectedColors: [
// custom colors
'green',
'blue',
// picks auto colors, not repeating green and blue colors and variants initially
'magenta',
// picks auto colors
'greenBright',
'blueBright',
'green',
'blue',
'magenta',
],
});
});
it('does not repeat consecutive colors when last prefixColor is auto', () => {
const prefixColorSelector = new PrefixColorSelector(['auto']);
// pick auto colors over 2 sets
const expectedColors: string[] = [
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS,
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS,
];
expectedColors.reduce((previousColor, currentExpectedColor) => {
const actualSelectedColor = prefixColorSelector.getNextColor();
expect(actualSelectedColor).not.toBe(previousColor); // no consecutive colors
expect(actualSelectedColor).toBe(currentExpectedColor); // expected color
return actualSelectedColor;
}, '');
});
it('handles when more individual auto prefixColors exist than acceptable console colors', () => {
// pick auto colors over 2 sets
const expectedColors: string[] = [
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS,
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS,
];
const prefixColorSelector = new PrefixColorSelector(expectedColors.map(() => 'auto'));
expectedColors.reduce((previousColor, currentExpectedColor) => {
const actualSelectedColor = prefixColorSelector.getNextColor();
expect(actualSelectedColor).not.toBe(previousColor); // no consecutive colors
expect(actualSelectedColor).toBe(currentExpectedColor); // expected color
return actualSelectedColor;
}, '');
});
});
describe('PrefixColorSelector#ACCEPTABLE_CONSOLE_COLORS', () => {
it('has more than 1 auto color defined', () => {
// ! code assumes this always has more than one entry, so make sure
expect(PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length).toBeGreaterThan(1);
});
});