Karma is deprecated and depends on braces, which has no patched release for GHSA-vfj7-8cjw-p6xm. Tests now run through the @angular/build unit-test builder with Vitest and jsdom. fakeAsync is replaced by Vitest fake timers, and zone.js is dropped because the zoneless app only loaded it for the Karma test polyfills. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.1 KiB
TypeScript
97 lines
3.1 KiB
TypeScript
import type { ComponentFixture } from '@angular/core/testing';
|
|
import { TestBed } from '@angular/core/testing';
|
|
import { provideRouter } from '@angular/router';
|
|
|
|
import { AppComponent } from './app.component';
|
|
|
|
describe('AppComponent', (): void => {
|
|
let fixture: ComponentFixture<AppComponent>;
|
|
let component: AppComponent;
|
|
|
|
beforeEach(async (): Promise<void> => {
|
|
localStorage.clear();
|
|
|
|
await TestBed.configureTestingModule({
|
|
imports: [AppComponent],
|
|
providers: [provideRouter([])],
|
|
}).compileComponents();
|
|
|
|
fixture = TestBed.createComponent(AppComponent);
|
|
component = fixture.componentInstance;
|
|
fixture.detectChanges();
|
|
});
|
|
|
|
it('should create', (): void => {
|
|
expect(component).toBeTruthy();
|
|
});
|
|
|
|
it('should keep the language selector closed on first visit', (): void => {
|
|
const el = fixture.nativeElement as HTMLElement;
|
|
expect(el.querySelector('.language-overlay')).toBeNull();
|
|
});
|
|
|
|
it('should preselect English by default', (): void => {
|
|
const comp = component as unknown as {
|
|
selectedLanguage(): string;
|
|
};
|
|
expect(comp.selectedLanguage()).toBe('en');
|
|
});
|
|
|
|
it('should render the language switcher on first visit', (): void => {
|
|
const el = fixture.nativeElement as HTMLElement;
|
|
expect(el.querySelector('.language-switcher')).not.toBeNull();
|
|
});
|
|
|
|
it('should always render a router-outlet element', (): void => {
|
|
const el = fixture.nativeElement as HTMLElement;
|
|
expect(el.querySelector('router-outlet')).not.toBeNull();
|
|
});
|
|
|
|
it('should confirm and persist the selected language', (): void => {
|
|
const comp = component as unknown as {
|
|
chooseLanguage(language: 'en' | 'de'): void;
|
|
currentLanguageCode(): string;
|
|
isLanguageConfirmed(): boolean;
|
|
isLanguageSelectorOpen(): boolean;
|
|
};
|
|
|
|
comp.chooseLanguage('de');
|
|
fixture.detectChanges();
|
|
|
|
expect(comp.currentLanguageCode()).toBe('de');
|
|
expect(comp.isLanguageConfirmed()).toBe(true);
|
|
expect(comp.isLanguageSelectorOpen()).toBe(false);
|
|
expect(localStorage.getItem('portfolio-language')).toBe('de');
|
|
});
|
|
|
|
it('should restore a saved language from localStorage', async (): Promise<void> => {
|
|
localStorage.setItem('portfolio-language', 'de');
|
|
|
|
const restoredFixture = TestBed.createComponent(AppComponent);
|
|
restoredFixture.detectChanges();
|
|
const restoredComponent = restoredFixture.componentInstance as unknown as {
|
|
currentLanguageCode(): string;
|
|
isLanguageConfirmed(): boolean;
|
|
isLanguageSelectorOpen(): boolean;
|
|
};
|
|
|
|
expect(restoredComponent.currentLanguageCode()).toBe('de');
|
|
expect(restoredComponent.isLanguageConfirmed()).toBe(true);
|
|
expect(restoredComponent.isLanguageSelectorOpen()).toBe(false);
|
|
});
|
|
|
|
it('should reopen the selector after confirmation', (): void => {
|
|
const comp = component as unknown as {
|
|
chooseLanguage(language: 'en' | 'de'): void;
|
|
reopenLanguageSelector(): void;
|
|
isLanguageSelectorOpen(): boolean;
|
|
};
|
|
|
|
comp.chooseLanguage('en');
|
|
comp.reopenLanguageSelector();
|
|
fixture.detectChanges();
|
|
|
|
expect(comp.isLanguageSelectorOpen()).toBe(true);
|
|
});
|
|
});
|