storebound/apps/desktop/tests/prefs.test.ts

50 lines
2.5 KiB
TypeScript

import { describe, expect, test } from 'bun:test';
import { parseStart } from '../src/nav';
import { shortPath, tildeText } from '../src/paths';
import { applyPrefs, DEFAULT_PREFS, parsePrefs, resolveLang } from '../src/prefs';
describe('prefs', () => {
test('parse is tolerant and only keeps known values', () => {
expect(parsePrefs(null)).toEqual(DEFAULT_PREFS);
expect(parsePrefs('{broken')).toEqual(DEFAULT_PREFS);
expect(parsePrefs('{"theme":"dark","motion":"reduce","lang":"tr","onboarded":true}')).toEqual({ theme: 'dark', motion: 'reduce', lang: 'tr', onboarded: true });
expect(parsePrefs('{"theme":"neon","motion":"fast","lang":"xx","onboarded":"yes"}')).toEqual(DEFAULT_PREFS);
});
test('apply sets and clears data attributes and the document language', () => {
const root = { dataset: {} as DOMStringMap, lang: '' };
applyPrefs({ theme: 'light', motion: 'reduce', lang: 'tr', onboarded: true }, root);
expect({ ...root.dataset }).toEqual({ theme: 'light', motion: 'reduce' });
expect(root.lang).toBe('tr');
applyPrefs(DEFAULT_PREFS, root, 'en-GB');
expect({ ...root.dataset }).toEqual({});
expect(root.lang).toBe('en');
});
test('system language: Turkish when the system is, else English', () => {
expect(resolveLang('system', 'tr-TR')).toBe('tr');
expect(resolveLang('system', 'de-DE')).toBe('en');
expect(resolveLang('en', 'tr-TR')).toBe('en');
});
});
describe('paths', () => {
test('home shown as ~', () => {
expect(shortPath('/home/you/games/a', '/home/you')).toBe('~/games/a');
expect(shortPath('/home/you', '/home/you/')).toBe('~');
expect(shortPath('/home/you-2/a', '/home/you')).toBe('/home/you-2/a');
expect(shortPath('C:\\Users\\you\\a', 'C:\\Users\\you')).toBe('~\\a');
expect(shortPath('/x', null)).toBe('/x');
expect(tildeText('in /home/you/a and /home/you/b', '/home/you')).toBe('in ~/a and ~/b');
});
});
describe('routes', () => {
test('deep links and start switches', () => {
expect(parseStart('accounts', [])).toEqual({ screen: 'accounts' });
expect(parseStart('portfolio', [])).toEqual({ screen: 'portfolio' });
expect(parseStart('app:health', ['a', 'b'])).toEqual({ screen: 'app', appId: 'a', tab: 'health' });
expect(parseStart('app:reviews:2', ['a', 'b'])).toEqual({ screen: 'app', appId: 'b', tab: 'reviews' });
expect(parseStart('app:nope', ['a'])).toEqual({ screen: 'app', appId: 'a', tab: 'releases' });
expect(parseStart('app:health', [])).toBeNull();
expect(parseStart('write', ['a'])).toBeNull();
});
});