import { signal } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { HttpErrorResponse } from '@angular/common/http'; import { of, throwError } from 'rxjs'; import { vi } from 'vitest'; import { LandlordProfileService } from '../../services/landlord-profile.service'; import { SessionService } from '../../services/session.service'; import { ThemeService } from '../../services/theme.service'; import { SettingsPage } from './settings-page'; describe('SettingsPage', () => { const profile = { getProfile: vi.fn(), updateProfile: vi.fn(), }; const session = { username: signal('Camille Martin'), email: signal('camille@example.com'), logout: vi.fn(), }; const theme = { preference: signal('system'), setPreference: vi.fn(), }; beforeEach(() => { vi.clearAllMocks(); profile.getProfile.mockReturnValue( of({ firstName: 'Camille', lastName: 'Martin', email: 'camille@example.com', phone: '+33600000000', address: '1 rue de Paris', }), ); TestBed.configureTestingModule({ providers: [ { provide: LandlordProfileService, useValue: profile }, { provide: SessionService, useValue: session }, { provide: ThemeService, useValue: theme }, ], }); }); it('prefills the landlord profile from the connected account', async () => { const fixture = TestBed.createComponent(SettingsPage); fixture.detectChanges(); await fixture.whenStable(); expect(fixture.nativeElement.querySelector('input#profile-firstName').value).toBe('Camille'); expect(fixture.nativeElement.querySelector('input#profile-email').value).toBe( 'camille@example.com', ); expect(fixture.nativeElement.querySelector('input#profile-address').value).toBe( '1 rue de Paris', ); }); it('keeps the entered values and explains a rejected profile save', async () => { profile.updateProfile.mockReturnValue( throwError( () => new HttpErrorResponse({ error: { message: 'Cette adresse e-mail est déjà utilisée.' }, status: 409, }), ), ); const fixture = TestBed.createComponent(SettingsPage); fixture.detectChanges(); await fixture.whenStable(); const email = fixture.nativeElement.querySelector('input#profile-email') as HTMLInputElement; email.value = 'alex@example.com'; email.dispatchEvent(new Event('input')); fixture.detectChanges(); fixture.nativeElement.querySelector('button.primary-action').click(); fixture.detectChanges(); await fixture.whenStable(); expect(profile.updateProfile).toHaveBeenCalledWith({ firstName: 'Camille', lastName: 'Martin', email: 'alex@example.com', phone: '+33600000000', address: '1 rue de Paris', }); expect(fixture.nativeElement.textContent).toContain('Cette adresse e-mail est déjà utilisée.'); expect(fixture.nativeElement.querySelector('input#profile-email').value).toBe( 'alex@example.com', ); }); });