49 lines
2.7 KiB
TypeScript
49 lines
2.7 KiB
TypeScript
import * as path from 'path';
|
|
import * as fs from 'fs';
|
|
import * as os from 'os';
|
|
import { ThemeLoaderService } from '../../backend/src/common/utils/theme-loader.service';
|
|
|
|
describe('ThemeLoaderService', () => {
|
|
const config = { get: jest.fn() } as any;
|
|
// ThemeLoaderService now takes SettingsService for the configured-theme
|
|
// validation step. We pass a stub that always returns 'classic' so the
|
|
// existing tests focus on disk loading + lookup behavior.
|
|
const settings = { get: jest.fn().mockResolvedValue('classic') } as any;
|
|
|
|
it('falls back to default and warns on unknown theme id', () => {
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
|
|
fs.writeFileSync(path.join(tmp, 'classic.json'), JSON.stringify({ id: 'classic', name: 'Classic', tokens: { primary: '#000', secondary: '#111', accent: '#222', surface: '#fff', text: '#000', success: '#0f0', warning: '#ff0', danger: '#f00', fontFamily: 'sans', radii: { sm: '1px', md: '2px', lg: '3px' }, spacingScale: [4, 8] } }));
|
|
config.get.mockImplementation((k: string, d: any) => (k === 'THEMES_DIR' ? tmp : d));
|
|
const svc = new ThemeLoaderService(config, settings);
|
|
return svc.onModuleInit().then(() => {
|
|
const t = svc.getTheme('does-not-exist');
|
|
expect(t.id).toBe('classic');
|
|
expect(t.tokens.primary).toBe('#000');
|
|
});
|
|
});
|
|
|
|
it('loads a valid theme by id', () => {
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
|
|
const theme = { id: 'classic', name: 'Classic', tokens: { primary: '#000', secondary: '#111', accent: '#222', surface: '#fff', text: '#000', success: '#0f0', warning: '#ff0', danger: '#f00', fontFamily: 'sans', radii: { sm: '1px', md: '2px', lg: '3px' }, spacingScale: [4, 8] } };
|
|
fs.writeFileSync(path.join(tmp, 'classic.json'), JSON.stringify(theme));
|
|
config.get.mockImplementation((k: string, d: any) => (k === 'THEMES_DIR' ? tmp : d));
|
|
const svc = new ThemeLoaderService(config, settings);
|
|
return svc.onModuleInit().then(() => {
|
|
const t = svc.getTheme('classic');
|
|
expect(t.id).toBe('classic');
|
|
expect(t.tokens.spacingScale).toEqual([4, 8]);
|
|
});
|
|
});
|
|
|
|
it('skips invalid theme files', () => {
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
|
|
fs.writeFileSync(path.join(tmp, 'classic.json'), JSON.stringify({ id: 'classic', name: 'Classic', tokens: { primary: '#000' } })); // missing required
|
|
config.get.mockImplementation((k: string, d: any) => (k === 'THEMES_DIR' ? tmp : d));
|
|
const svc = new ThemeLoaderService(config, settings);
|
|
return svc.onModuleInit().then(() => {
|
|
// Falls back to builtins; classic should be findable via builtins
|
|
const t = svc.getTheme('classic');
|
|
expect(t).toBeDefined();
|
|
});
|
|
});
|
|
}); |