feat: add unit tests for tenant, guarantor, lease card, dashboard, session, theme, and workspace services
This commit is contained in:
1 parent
74a77f2d2b
commit
181410e28f
8 files changed
+514
-2
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { Router } from '@angular/router';
|
||||
import { of, throwError } from 'rxjs';
|
||||
import { vi } from 'vitest';
|
||||
import { SessionService } from '../../services/session.service';
|
||||
import { WorkspaceService } from '../../services/workspace.service';
|
||||
import { DashboardPage } from './dashboard-page';
|
||||
|
||||
describe('DashboardPage', () => {
|
||||
const http = { get: vi.fn() };
|
||||
const router = { navigateByUrl: vi.fn() };
|
||||
const session = { username: vi.fn(() => 'camille'), email: vi.fn(() => 'camille@example.com') };
|
||||
const workspace = { apartments: vi.fn(() => []), loadApartments: vi.fn() };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
{ provide: HttpClient, useValue: http },
|
||||
{ provide: Router, useValue: router },
|
||||
{ provide: SessionService, useValue: session },
|
||||
{ provide: WorkspaceService, useValue: workspace },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('loads the workspace and displays the user initial', () => {
|
||||
const fixture = TestBed.createComponent(DashboardPage);
|
||||
|
||||
expect(workspace.loadApartments).toHaveBeenCalled();
|
||||
expect(fixture.componentInstance.userInitial()).toBe('C');
|
||||
});
|
||||
|
||||
it('shows the backend response or connection error', () => {
|
||||
const fixture = TestBed.createComponent(DashboardPage);
|
||||
const component = fixture.componentInstance;
|
||||
http.get.mockReturnValueOnce(of('Hello'));
|
||||
component.checkConnection();
|
||||
http.get.mockReturnValueOnce(throwError(() => ({ statusText: 'Offline' })));
|
||||
component.checkConnection();
|
||||
|
||||
expect(http.get).toHaveBeenCalledWith('/api/hello', { responseType: 'text' });
|
||||
expect(component.helloMessage()).toBe('Failed to connect to backend: Offline');
|
||||
});
|
||||
|
||||
it('navigates to apartments', () => {
|
||||
const fixture = TestBed.createComponent(DashboardPage);
|
||||
|
||||
fixture.componentInstance.showApartments();
|
||||
|
||||
expect(router.navigateByUrl).toHaveBeenCalledWith('/apartments');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { vi } from 'vitest';
|
||||
import { GuarantorForm } from './guarantor-form';
|
||||
|
||||
describe('GuarantorForm', () => {
|
||||
it('marks an invalid form as touched without submitting it', () => {
|
||||
const fixture = TestBed.createComponent(GuarantorForm);
|
||||
const markAllAsTouched = vi.fn();
|
||||
const submitted = vi.fn();
|
||||
fixture.componentInstance.submitted.subscribe(submitted);
|
||||
|
||||
fixture.componentInstance.save({ invalid: true, control: { markAllAsTouched } } as never);
|
||||
|
||||
expect(markAllAsTouched).toHaveBeenCalled();
|
||||
expect(submitted).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('emits trimmed data and resets the form after saving', () => {
|
||||
const fixture = TestBed.createComponent(GuarantorForm);
|
||||
const component = fixture.componentInstance;
|
||||
const submitted = vi.fn();
|
||||
const resetForm = vi.fn();
|
||||
component.submitted.subscribe(submitted);
|
||||
component.firstName.set(' Alex ');
|
||||
component.lastName.set(' Durand ');
|
||||
component.email.set(' alex@example.com ');
|
||||
component.phone.set(' 0600000000 ');
|
||||
component.address.set(' 1 rue de Paris ');
|
||||
|
||||
component.save({ invalid: false, resetForm } as never);
|
||||
|
||||
expect(submitted).toHaveBeenCalledWith({
|
||||
firstName: 'Alex', lastName: 'Durand', email: 'alex@example.com', phone: '0600000000', address: '1 rue de Paris',
|
||||
});
|
||||
expect(component.firstName()).toBe('');
|
||||
expect(resetForm).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { LeaseCard } from './lease-card';
|
||||
|
||||
describe('LeaseCard', () => {
|
||||
it('resolves apartment, tenants, and every status label', () => {
|
||||
const fixture = TestBed.createComponent(LeaseCard);
|
||||
fixture.componentRef.setInput('apartments', [{ id: 'apartment-1', name: 'T2', address: '', purchasePrice: 0, description: '' }]);
|
||||
fixture.componentRef.setInput('tenants', [
|
||||
{ id: 'tenant-1', firstName: 'Camille', lastName: 'Martin', email: '', phone: '', address: '' },
|
||||
{ id: 'tenant-2', firstName: 'Alex', lastName: 'Durand', email: '', phone: '', address: '' },
|
||||
]);
|
||||
fixture.componentRef.setInput('lease', {
|
||||
apartmentId: 'apartment-1', tenantIds: ['tenant-1', 'missing', 'tenant-2'], guarantors: [], startDate: '', endDate: '', monthlyRent: 0, monthlyCharges: 0, securityDeposit: 0, status: 'DRAFT',
|
||||
});
|
||||
const component = fixture.componentInstance;
|
||||
|
||||
expect(component.apartmentName()).toBe('T2');
|
||||
expect(component.tenantNames()).toBe('Camille Martin, Alex Durand');
|
||||
expect(component.statusLabel()).toBe('Bail en préparation');
|
||||
fixture.componentRef.setInput('lease', { ...component.lease(), status: 'ACTIVE' });
|
||||
expect(component.statusLabel()).toBe('Bail actif');
|
||||
fixture.componentRef.setInput('lease', { ...component.lease(), apartmentId: 'missing', status: 'ARCHIVED' });
|
||||
expect(component.apartmentName()).toBe('Bien inconnu');
|
||||
expect(component.statusLabel()).toBe('Bail archivé');
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { vi } from 'vitest';
|
||||
import { TenantForm } from './tenant-form';
|
||||
|
||||
describe('TenantForm', () => {
|
||||
@@ -15,4 +16,21 @@ describe('TenantForm', () => {
|
||||
|
||||
expect(firstName).toBe('Camille');
|
||||
});
|
||||
|
||||
it('loads an existing tenant and leaves invalid data untouched', () => {
|
||||
const fixture = TestBed.createComponent(TenantForm);
|
||||
fixture.componentRef.setInput('tenant', {
|
||||
id: 'tenant-1', firstName: 'Camille', lastName: 'Martin', email: 'camille@example.com', phone: '0600000000', address: '1 rue de Paris',
|
||||
});
|
||||
fixture.detectChanges();
|
||||
const markAllAsTouched = vi.fn();
|
||||
const submitted = vi.fn();
|
||||
fixture.componentInstance.submitted.subscribe(submitted);
|
||||
|
||||
fixture.componentInstance.save({ invalid: true, control: { markAllAsTouched } } as never);
|
||||
|
||||
expect(fixture.componentInstance.firstName()).toBe('Camille');
|
||||
expect(markAllAsTouched).toHaveBeenCalled();
|
||||
expect(submitted).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user