61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { TestBed } from '@angular/core/testing';
|
|
|
|
import { PiThemeService } from './theme.service';
|
|
|
|
describe('PiThemeService', () => {
|
|
afterEach(() => {
|
|
localStorage.clear();
|
|
document.body.className = '';
|
|
document.documentElement.classList.remove('dark-mode');
|
|
});
|
|
|
|
it('applies `pi-shell pi-shell__default` to <body> by default', () => {
|
|
TestBed.inject(PiThemeService);
|
|
TestBed.flushEffects();
|
|
|
|
expect(document.body.className).toBe('pi-shell pi-shell__default');
|
|
});
|
|
|
|
it('updates <body> when the theme changes', () => {
|
|
const service = TestBed.inject(PiThemeService);
|
|
|
|
service.setTheme('alternative');
|
|
TestBed.flushEffects();
|
|
|
|
expect(document.body.className).toBe('pi-shell pi-shell__alternative');
|
|
});
|
|
|
|
it('toggles `dark-mode` on <html>', () => {
|
|
const service = TestBed.inject(PiThemeService);
|
|
|
|
service.setDarkMode(true);
|
|
TestBed.flushEffects();
|
|
expect(document.documentElement.classList.contains('dark-mode')).toBe(true);
|
|
|
|
service.toggleDarkMode();
|
|
TestBed.flushEffects();
|
|
expect(document.documentElement.classList.contains('dark-mode')).toBe(false);
|
|
});
|
|
|
|
it('persists the theme and dark mode across a fresh instance (reload)', () => {
|
|
const service = TestBed.inject(PiThemeService);
|
|
service.setTheme('minimal');
|
|
service.setDarkMode(true);
|
|
TestBed.flushEffects();
|
|
|
|
TestBed.resetTestingModule();
|
|
const reloaded = TestBed.inject(PiThemeService);
|
|
|
|
expect(reloaded.theme()).toBe('minimal');
|
|
expect(reloaded.darkMode()).toBe(true);
|
|
});
|
|
|
|
it('falls back to the default theme when nothing valid is stored', () => {
|
|
localStorage.setItem('pi-theme', JSON.stringify('not-a-real-theme'));
|
|
|
|
const service = TestBed.inject(PiThemeService);
|
|
|
|
expect(service.theme()).toBe('default');
|
|
});
|
|
});
|