mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-21 14:06:19 +00:00
Move shlink-web-component tests to their own folder
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import { ColorGenerator } from '../../../shlink-web-component/src/utils/services/ColorGenerator';
|
||||
import type { LocalStorage } from '../../../src/utils/services/LocalStorage';
|
||||
import { MAIN_COLOR } from '../../../src/utils/theme';
|
||||
|
||||
describe('ColorGenerator', () => {
|
||||
let colorGenerator: ColorGenerator;
|
||||
const storageMock = fromPartial<LocalStorage>({
|
||||
set: vi.fn(),
|
||||
get: vi.fn().mockImplementation(() => undefined),
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
colorGenerator = new ColorGenerator(storageMock);
|
||||
});
|
||||
|
||||
it('sets a color in the storage and makes it available after that', () => {
|
||||
const color = '#ff0000';
|
||||
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(colorGenerator.getColorForKey('foo')).toEqual(color);
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('generates a random color when none is available for requested key', () => {
|
||||
expect(colorGenerator.getColorForKey('bar')).toEqual(expect.stringMatching(/^#(?:[0-9a-fA-F]{6})$/));
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('trims and lower cases keys before trying to match', () => {
|
||||
const color = '#ff0000';
|
||||
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(colorGenerator.getColorForKey(' foo')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('foO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FoO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FOO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FOO ')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey(' FoO ')).toEqual(color);
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
describe('isColorLightForKey', () => {
|
||||
it.each([
|
||||
[MAIN_COLOR, true],
|
||||
['#8A661C', false],
|
||||
['#F7BE05', true],
|
||||
['#5A02D8', false],
|
||||
['#202786', false],
|
||||
])('returns that the color for a key is light based on the color assigned to that key', (color, isLight) => {
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(isLight).toEqual(colorGenerator.isColorLightForKey('foo'));
|
||||
expect(isLight).toEqual(colorGenerator.isColorLightForKey('foo')); // To cover when color is already calculated
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,8 +0,0 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ColorGenerator } from '../../../../shlink-web-component/src/utils/services/ColorGenerator';
|
||||
|
||||
export const colorGeneratorMock = fromPartial<ColorGenerator>({
|
||||
getColorForKey: vi.fn(() => 'red'),
|
||||
setColorForKey: vi.fn(),
|
||||
isColorLightForKey: vi.fn(() => false),
|
||||
});
|
||||
Reference in New Issue
Block a user