57 lines
2.4 KiB
TypeScript
57 lines
2.4 KiB
TypeScript
import { signal } from '@angular/core';
|
|
import { TestBed } from '@angular/core/testing';
|
|
import { provideRouter } from '@angular/router';
|
|
import { vi } from 'vitest';
|
|
import { App } from './app';
|
|
import { AuthState } from './auth/auth.state';
|
|
import { routes } from './app.routes';
|
|
import { authGuard, anonymousGuard } from './auth/auth.guard';
|
|
|
|
describe('App', () => {
|
|
const status = signal('anonymous');
|
|
const user = signal({ id: 1, username: 'demo', roles: ['CAMA'] });
|
|
const logout = vi.fn();
|
|
beforeEach(async () => {
|
|
status.set('anonymous'); logout.mockClear();
|
|
await TestBed.configureTestingModule({
|
|
imports: [App],
|
|
providers: [provideRouter([]), { provide: AuthState, useValue: {
|
|
status, user, isAuthenticated: () => status() === 'authenticated', logout,
|
|
} }],
|
|
})
|
|
.compileComponents();
|
|
});
|
|
|
|
it('should create the app', () => {
|
|
const fixture = TestBed.createComponent(App);
|
|
const app = fixture.componentInstance;
|
|
expect(app).toBeTruthy();
|
|
});
|
|
|
|
it('should render the toolbar title', async () => {
|
|
status.set('authenticated');
|
|
const fixture = TestBed.createComponent(App);
|
|
await fixture.whenStable();
|
|
fixture.detectChanges();
|
|
const compiled = fixture.nativeElement as HTMLElement;
|
|
expect(compiled.querySelector('mat-toolbar')?.textContent).toContain('ISSSTELEON');
|
|
});
|
|
it('hides shell for anonymous and checking', () => {
|
|
const fixture = TestBed.createComponent(App); fixture.detectChanges();
|
|
expect(fixture.nativeElement.querySelector('mat-toolbar')).toBeNull();
|
|
status.set('checking'); fixture.detectChanges();
|
|
expect(fixture.nativeElement.querySelector('mat-toolbar')).toBeNull();
|
|
expect(fixture.nativeElement.querySelector('router-outlet').parentElement.hidden).toBe(true);
|
|
});
|
|
it('invokes local logout from shell', () => {
|
|
status.set('authenticated'); const fixture = TestBed.createComponent(App); fixture.detectChanges();
|
|
fixture.nativeElement.querySelector('button').click(); expect(logout).toHaveBeenCalledOnce();
|
|
});
|
|
it('preserves login, protected home and wildcard alongside area routes', () => {
|
|
expect(routes.map(route => route.path)).toEqual(['login', '', 'cacu', 'cama', 'tea', '**']);
|
|
expect(routes[0].canActivate).toEqual([anonymousGuard]);
|
|
expect(routes[1].canActivate).toEqual([authGuard]);
|
|
expect(routes.at(-1)?.redirectTo).toBe('');
|
|
});
|
|
});
|