Align optional apartment legal profile validation (#45)

This commit is contained in:
nessar committed 2026-08-17 14:18:16 +02:00
1 parent 72d3f0ccfa
commit c1e7964f2e
10 files changed
+477 -56

No files matched your search

@@ -1,5 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { vi } from 'vitest';
import { ApartmentLegalProfileForm } from '../apartment-legal-profile-form/apartment-legal-profile-form';
import { ApartmentForm } from './apartment-form';
import { emptyApartmentLegalProfile } from '../../services/apartment.service';
@@ -293,6 +295,101 @@ describe('ApartmentForm', () => {
expect(saved).not.toHaveBeenCalled();
});
it('accepts zero legal-profile numbers with the first valid reference year', () => {
const fixture = TestBed.createComponent(ApartmentForm);
const form = fixture.componentInstance;
const saved = vi.fn();
form.saved.subscribe(saved);
form.apartmentModel.set({
name: 'T2',
address: '1 rue de Paris',
purchasePrice: 250000,
description: '',
privateAreas: [],
legalProfile: {
...emptyApartmentLegalProfile(),
habitableSurface: 0,
estimatedAnnualEnergyCost: 0,
estimatedAnnualEnergyCostReferenceYear: 1900,
},
});
form.save();
expect(saved).toHaveBeenCalledWith(
expect.objectContaining({
legalProfile: expect.objectContaining({
habitableSurface: 0,
estimatedAnnualEnergyCost: 0,
estimatedAnnualEnergyCostReferenceYear: 1900,
}),
}),
);
});
it('rejects an out-of-range energy reference year when an estimated cost is provided', () => {
const fixture = TestBed.createComponent(ApartmentForm);
const form = fixture.componentInstance;
const saved = vi.fn();
form.saved.subscribe(saved);
form.apartmentModel.set({
name: 'T2',
address: '1 rue de Paris',
purchasePrice: 250000,
description: '',
privateAreas: [],
legalProfile: {
...emptyApartmentLegalProfile(),
estimatedAnnualEnergyCost: 850,
estimatedAnnualEnergyCostReferenceYear: 1800,
},
});
form.save();
expect(form.error()).toContain('1900 et 2100');
expect(saved).not.toHaveBeenCalled();
});
it('rejects an invalid department code inline', () => {
const fixture = TestBed.createComponent(ApartmentForm);
const form = fixture.componentInstance;
const saved = vi.fn();
form.saved.subscribe(saved);
form.apartmentModel.set({
name: 'T2',
address: '1 rue de Paris',
purchasePrice: 250000,
description: '',
privateAreas: [],
legalProfile: { ...emptyApartmentLegalProfile(), departmentCode: '20' },
});
form.save();
expect(form.error()).toContain('département français');
expect(saved).not.toHaveBeenCalled();
});
it('shows known legal-profile server errors next to their controls', async () => {
const fixture = TestBed.createComponent(ApartmentForm);
fixture.componentRef.setInput('saveFieldErrors', [
{ field: 'legalProfile.departmentCodeValid', code: 'assert_true' },
]);
const changed = vi.fn();
fixture.componentInstance.fieldChanged.subscribe(changed);
fixture.detectChanges();
await fixture.whenStable();
expect(
fixture.debugElement
.query(By.directive(ApartmentLegalProfileForm))
.componentInstance.departmentCodeError(),
).toContain('Saisissez un département français');
fixture.nativeElement.querySelector('#departmentCode').dispatchEvent(new Event('input'));
expect(changed).toHaveBeenCalledWith('legalProfile.departmentCodeValid');
});
it('turns an emptied nullable number field back into null', async () => {
const fixture = TestBed.createComponent(ApartmentForm);
fixture.detectChanges();
@@ -337,7 +434,7 @@ describe('ApartmentForm', () => {
);
});
it('clears an energy reference year that has no estimated cost', async () => {
it('clears a stale energy reference year that has no estimated cost', async () => {
const fixture = TestBed.createComponent(ApartmentForm);
fixture.componentRef.setInput('apartment', {
id: 'apartment-1',
@@ -348,7 +445,7 @@ describe('ApartmentForm', () => {
privateAreas: [],
legalProfile: {
...emptyApartmentLegalProfile(),
estimatedAnnualEnergyCostReferenceYear: 2021,
estimatedAnnualEnergyCostReferenceYear: 1800,
},
});
fixture.detectChanges();