fix
This commit is contained in:
60
libs/ui/src/lib/util/theme/theme.service.spec.ts
Normal file
60
libs/ui/src/lib/util/theme/theme.service.spec.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user