Files

124 lines
4.9 KiB
TypeScript
Raw Permalink Normal View History

import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
PRIMARY_COLOR_PALETTE,
applyPrimaryColor,
initPrimaryColor,
primaryColorByName,
setPrimaryColor,
setPrimaryColorUser,
storedPrimaryColor,
} from '../src/lib/primary-color.svelte';
const styleText = (): string => document.getElementById('sc-primary-color')?.textContent ?? '';
function reset(): void {
document.getElementById('sc-primary-color')?.remove();
document.documentElement.removeAttribute('data-primary-color-user');
localStorage.clear();
}
describe('primary color palette', () => {
it('has unique names and valid colors', () => {
const names = PRIMARY_COLOR_PALETTE.map((color) => color.name);
expect(new Set(names).size).toBe(names.length);
for (const color of PRIMARY_COLOR_PALETTE) {
for (const value of [color.dark, color.light, color.darkFg]) {
expect(value, `${color.name} ${value}`).toMatch(/^#[0-9a-f]{6}$/);
}
}
});
it('finds a color by name and nothing for an unknown name', () => {
expect(primaryColorByName('Vermilion')?.light).toBe('#db3e24');
expect(primaryColorByName('Not a color')).toBeUndefined();
});
});
describe('primary color', () => {
beforeEach(reset);
afterEach(reset);
it('writes the light and dark declarations into one stylesheet, and replaces them on a change', () => {
applyPrimaryColor('app', 'Vermilion');
const css = styleText();
expect(css).toContain('--color-primary: #db3e24 !important;');
expect(css).toContain("[data-theme='dark']");
expect(css).toContain('--color-primary: #f59080 !important;');
expect(css).toContain('--sc-primary-fg: #ffffff !important;');
expect(css).toContain('--sc-primary-fg: #2b110d !important;');
expect(css).toMatch(/--sc-primary-hover: #[0-9a-f]{6} !important;/);
applyPrimaryColor('app', 'Cyan');
expect(document.querySelectorAll('#sc-primary-color')).toHaveLength(1);
expect(styleText()).toContain('#249edb');
expect(styleText()).not.toContain('#db3e24');
});
it('uses dark text on light colors where white would be hard to read', () => {
applyPrimaryColor('app', 'Honey');
const [light] = styleText().split("[data-theme='dark']");
expect(light).toContain('--sc-primary-fg: #2a2b0d !important;');
expect(light).not.toContain('--sc-primary-fg: #ffffff !important;');
});
it('makes the hover lighter on dark and darker on light', () => {
applyPrimaryColor('app', 'Clover');
const [light, dark] = styleText().split("[data-theme='dark']");
const hover = (css: string): number => {
const hex = /--sc-primary-hover: #([0-9a-f]{6})/.exec(css)?.[1] ?? '';
return [0, 2, 4].reduce((sum, start) => sum + parseInt(hex.slice(start, start + 2), 16), 0);
};
const base = (hexValue: string): number =>
[1, 3, 5].reduce((sum, start) => sum + parseInt(hexValue.slice(start, start + 2), 16), 0);
expect(hover(light)).toBeLessThan(base('#16a34a'));
expect(hover(dark)).toBeGreaterThan(base('#22c55e'));
});
it('removes the stylesheet for an unknown or empty color', () => {
applyPrimaryColor('app', 'Vermilion');
applyPrimaryColor('app', 'Not a color');
expect(document.getElementById('sc-primary-color')).toBeNull();
applyPrimaryColor('app', '');
expect(document.getElementById('sc-primary-color')).toBeNull();
});
it('stores the choice per project and user, and clears it', () => {
setPrimaryColorUser('Alice');
setPrimaryColor('My App', 'Violet');
expect(localStorage.getItem('primary-color:my%20app:alice')).toBe('Violet');
expect(storedPrimaryColor('my app')).toBe('Violet');
expect(styleText()).toContain('#6d24db');
setPrimaryColorUser('bob');
expect(storedPrimaryColor('my app')).toBe('');
setPrimaryColorUser('alice');
expect(storedPrimaryColor('my app')).toBe('Violet');
setPrimaryColor('my app', '');
expect(storedPrimaryColor('my app')).toBe('');
expect(localStorage.getItem('primary-color:my%20app:alice')).toBeNull();
expect(document.getElementById('sc-primary-color')).toBeNull();
});
it('ignores a stored name that is not in the palette', () => {
localStorage.setItem('primary-color:app:anonymous', 'Chartreuse-ish');
expect(storedPrimaryColor('app')).toBe('');
});
it('applies the stored color on init and re-applies it when the user changes', () => {
localStorage.setItem('primary-color:shop:carol', 'Rose');
initPrimaryColor('shop', 'Carol');
expect(styleText()).toContain('#db2474');
localStorage.setItem('primary-color:shop:dave', 'Lime');
setPrimaryColorUser('dave');
expect(styleText()).toContain('#72b814');
setPrimaryColorUser('someone-without-a-choice');
expect(document.getElementById('sc-primary-color')).toBeNull();
});
it('treats a blank user as anonymous', () => {
setPrimaryColor('app', 'Gold');
expect(localStorage.getItem('primary-color:app:anonymous')).toBe('Gold');
});
});