Files
lmnp-organizer/frontend/src/app/components/dashboard-page/dashboard-page.spec.ts
T
nessar 878da155bd chore: add husky pre-commit hook and lint-staged configuration
- Created a pre-commit hook in .husky to run lint-staged.
- Added .lintstagedrc.mjs to format frontend files with Prettier and apply Spotless to backend Java files.
- Introduced .prettierignore to exclude specific directories from formatting.
2026-08-17 14:18:16 +02:00

69 lines
2.4 KiB
TypeScript

import { HttpClient } from '@angular/common/http';
import { signal } from '@angular/core';
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: signal([]),
apartmentLoadState: signal({ loaded: false, loading: true, error: '' }),
loadApartments: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
workspace.apartmentLoadState.set({ loaded: false, loading: true, error: '' });
TestBed.configureTestingModule({
providers: [
{ provide: HttpClient, useValue: http },
{ provide: Router, useValue: router },
{ provide: SessionService, useValue: session },
{ provide: WorkspaceService, useValue: workspace },
],
});
});
it('skeletonizes only the apartment count while it loads', () => {
const fixture = TestBed.createComponent(DashboardPage);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('.count-skeleton')).not.toBeNull();
expect(fixture.nativeElement.textContent).toContain('Tableau de bord');
});
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');
});
});