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');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user