-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update prefix color selector to use a generator
- Loading branch information
Showing
5 changed files
with
185 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,131 @@ | ||
import { PrefixColorSelector } from './prefix-color-selector'; | ||
|
||
it('does not produce a color if it should not', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], false); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe(''); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe(''); | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe(''); | ||
}); | ||
function assertSelectedColors({ | ||
prefixColorSelector, | ||
expectedColors, | ||
}: { | ||
prefixColorSelector: PrefixColorSelector; | ||
expectedColors: string[]; | ||
}) { | ||
expectedColors.forEach(expectedColor => { | ||
expect(prefixColorSelector.getNextColor()).toBe(expectedColor); | ||
}); | ||
} | ||
|
||
it('uses user defined prefix colors only if not allowed to use auto colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['red', 'green', 'blue'], false); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe('red'); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe('green'); | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe('blue'); | ||
|
||
// uses last color if no more user defined colors | ||
selectedColor = prefixColorSelector.getNextColor(3); | ||
expect(selectedColor).toBe('blue'); | ||
selectedColor = prefixColorSelector.getNextColor(4); | ||
expect(selectedColor).toBe('blue'); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('uses user defined colors then recurring auto colors without repeating consecutive colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['red', 'green'], true); | ||
describe('#getNextColor', function () { | ||
it('does not produce a color if no prefixColors defined', () => { | ||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector([]), | ||
expectedColors: ['', '', ''], | ||
}); | ||
}); | ||
|
||
jest.spyOn(prefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
it('uses user defined prefix colors only, if no auto is used', () => { | ||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'blue']), | ||
expectedColors: [ | ||
'red', | ||
'green', | ||
'blue', | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe('red'); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe('green'); | ||
// uses last color if last color is not "auto" | ||
'blue', | ||
'blue', | ||
'blue', | ||
], | ||
}); | ||
}); | ||
|
||
// auto colors now, does not repeat last user color of green | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe('blue'); | ||
it('uses picks colors when user defines an auto color', () => { | ||
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
|
||
selectedColor = prefixColorSelector.getNextColor(3); | ||
expect(selectedColor).toBe('green'); | ||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'auto', 'orange']), | ||
expectedColors: [ | ||
// custom colors | ||
'red', | ||
'green', | ||
'blue', // picks auto color, not repeating consecutive "green" color | ||
'orange', | ||
|
||
selectedColor = prefixColorSelector.getNextColor(4); | ||
expect(selectedColor).toBe('blue'); | ||
}); | ||
// uses last color if last color is not "auto" | ||
'orange', | ||
'orange', | ||
'orange', | ||
], | ||
}); | ||
}); | ||
|
||
it('has more than 1 auto color defined', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], true); | ||
// ! code assumes this always has more than one entry, so make sure | ||
expect(prefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length).toBeGreaterThan(1); | ||
}); | ||
it('uses user defined colors then recurring auto colors without repeating consecutive colors', () => { | ||
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
|
||
it('can use only auto colors and does not repeat consecutive colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], true); | ||
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('does not repeat consecutive colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['auto']); | ||
|
||
Array(PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length * 2) | ||
.fill(0) | ||
.reduce((previousColor: string) => { | ||
const selectedColor = prefixColorSelector.getNextColor(); | ||
expect(selectedColor).not.toBe(previousColor); | ||
return selectedColor; | ||
}); | ||
}); | ||
}); | ||
|
||
let previousColor; | ||
let selectedColor: string; | ||
Array(prefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length * 2).forEach((_, index) => { | ||
previousColor = selectedColor; | ||
selectedColor = prefixColorSelector.getNextColor(index); | ||
expect(selectedColor).not.toBe(previousColor); | ||
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); | ||
}); | ||
}); |
Oops, something went wrong.