Files
ToBePlayed-Frontend/src/utils/appearance.test.js
T
lobo 83494519f9
Build, Test & Push Frontend / quality-check (push) Successful in 13s
Build, Test & Push Frontend / docker (push) Successful in 24s
feat(dashboard): pick the banner game or turn the banner off
2026-09-24 15:35:48 +02:00

53 lines
2.0 KiB
JavaScript

// @vitest-environment jsdom
import {afterEach, describe, expect, it} from 'vitest';
import {
applyAppearance,
cacheAppearance,
DEFAULT_APPEARANCE,
normalizeAppearance,
readCachedAppearance,
} from './appearance.js';
describe('normalizeAppearance', () => {
it('keeps known values', () => {
const appearance = {accent: 'blue', corners: 'round', wideLayout: true, backgroundAppid: 620, bannerAppid: 1086940, showBanner: false};
expect(normalizeAppearance(appearance)).toEqual(appearance);
});
it('replaces unknown or missing values with the defaults', () => {
expect(normalizeAppearance({accent: 'red;x', corners: 'pointy', wideLayout: 'yes', backgroundAppid: -1, bannerAppid: '620', showBanner: 'no'}))
.toEqual(DEFAULT_APPEARANCE);
expect(normalizeAppearance(null)).toEqual(DEFAULT_APPEARANCE);
expect(normalizeAppearance({backgroundAppid: 1.5})).toEqual(DEFAULT_APPEARANCE);
});
});
describe('appearance cache', () => {
afterEach(() => localStorage.clear());
it('reads back what was cached', () => {
cacheAppearance({accent: 'green', corners: 'sharp', wideLayout: false, backgroundAppid: null, bannerAppid: 620, showBanner: true});
expect(readCachedAppearance()).toEqual({accent: 'green', corners: 'sharp', wideLayout: false, backgroundAppid: null, bannerAppid: 620, showBanner: true});
});
it('falls back to the defaults for a broken or empty cache', () => {
expect(readCachedAppearance()).toEqual(DEFAULT_APPEARANCE);
localStorage.setItem('tbp_appearance', '{broken');
expect(readCachedAppearance()).toEqual(DEFAULT_APPEARANCE);
});
});
describe('applyAppearance', () => {
it('sets the presets as data attributes for the CSS', () => {
const root = document.createElement('html');
applyAppearance({accent: 'orange', corners: 'round', wideLayout: true, backgroundAppid: 620}, root);
expect(root.dataset.accent).toBe('orange');
expect(root.dataset.corners).toBe('round');
});
});