Вот мой app.ts
Код: Выделить всё
import { Component, inject, signal } from '@angular/core';
import { AuthService } from './services/auth-service';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.html',
standalone: false,
styleUrl: './app.scss',
})
export class App {
protected readonly titleSignal = signal('app-project-pmt');
title = "App projet PMT"
router:Router = inject(Router);
authService = inject(AuthService);
username = localStorage.getItem("name");
isLoggedIn() {
return this.authService.isLoggedIn();
}
logout() {
this.authService.logout();
this.router.navigate(['/login-form']);
}
}
Код: Выделить всё
home
{{title}}
@if(isLoggedIn()) {
create_new_folder Nouveau projet
{{username}}
Déconnexion
}
@else {
Inscription
Connexion
}
Код: Выделить всё
import { TestBed } from '@angular/core/testing';
import { App } from './app';
import { AuthService } from './services/auth-service';
import { Router } from '@angular/router';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { AppRoutingModule } from './app-routing-module';
describe('App', () => {
let authServiceSpy: jasmine.SpyObj;
let routerSpy: jasmine.SpyObj;
beforeEach(async () => {
authServiceSpy = jasmine.createSpyObj('AuthService', ['isLoggedIn', 'logout']);
authServiceSpy.isLoggedIn.and.returnValue(false);
routerSpy = jasmine.createSpyObj('Router', ['navigate']);
await TestBed.configureTestingModule({
imports: [
AppRoutingModule,
MatToolbarModule,
MatIconModule,
MatButtonModule
],
declarations: [App],
providers: [
{ provide: AuthService, useValue: authServiceSpy },
{ provide: Router, useValue: routerSpy }
]
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(App);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
});
Я не знаю, как имитировать выход маршрутизатора, чтобы не вызывать форму входа или если это возможно.
Код: Выделить всё
Watch mode enabled. Watching for file changes...
05 02 2026 14:36:24.572:WARN [karma]: No captured browser, open http://localhost:9876/
05 02 2026 14:36:24.670:INFO [karma-server]: Karma v6.4.4 server started at http://localhost:9876/
05 02 2026 14:36:24.671:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
05 02 2026 14:36:24.685:INFO [launcher]: Starting browser Chrome
05 02 2026 14:36:26.061:INFO [Chrome 144.0.0.0 (Windows 10)]: Connected on socket tgBw3o46XBG2-Io2AAAB with id 46244528
Chrome 144.0.0.0 (Windows 10) App should create the app FAILED
TypeError: Cannot read properties of undefined (reading 'root')
at Object.rootRoute [as useFactory] (node_modules/@angular/router/fesm2022/router_module.mjs:1023:31)
at Object.factory (node_modules/@angular/core/fesm2022/root_effect_scheduler.mjs:2290:38)
at node_modules/@angular/core/fesm2022/root_effect_scheduler.mjs:2187:47
at runInInjectorProfilerContext (node_modules/@angular/core/fesm2022/root_effect_scheduler.mjs:746:9)
at R3Injector.hydrate (node_modules/@angular/core/fesm2022/root_effect_scheduler.mjs:2185:21)
at R3Injector.get (node_modules/@angular/core/fesm2022/root_effect_scheduler.mjs:2033:33)
at ChainedInjector.get (node_modules/@angular/core/fesm2022/debug_node.mjs:13406:36)
at lookupTokenUsingModuleInjector (node_modules/@angular/core/fesm2022/debug_node.mjs:1718:39)
at getOrCreateInjectable (node_modules/@angular/core/fesm2022/debug_node.mjs:1766:12)
at ɵɵdirectiveInject (node_modules/@angular/core/fesm2022/debug_node.mjs:13457:19)
LOG: 'Error during login'
Chrome 144.0.0.0 (Windows 10): Executed 22 of 28 (1 FAILED) (0 secs / 0.405 secs)
Chrome 144.0.0.0 (Windows 10): Executed 28 of 28 (1 FAILED) (0.46 secs / 0.433 secs)
TOTAL: 1 FAILED, 27 SUCCESS
Chrome 144.0.0.0 (Windows 10) ERROR
Подробнее здесь: https://stackoverflow.com/questions/798 ... -of-undefi
Мобильная версия