Files
scouthub/scouthub-home-fe/src/app/home/home.spec.ts
T

99 lines
3.4 KiB
TypeScript

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideRouter } from '@angular/router';
import Keycloak from 'keycloak-js';
import { environment } from '../../environments/environment';
import { Home } from './home';
describe('Home', () => {
let component: Home;
let fixture: ComponentFixture<Home>;
let keycloak: {
authenticated: boolean | undefined;
login: ReturnType<typeof vi.fn>;
tokenParsed?: { realm_access?: { roles?: string[] } };
};
async function setup(): Promise<void> {
await TestBed.configureTestingModule({
imports: [Home],
providers: [provideRouter([]), { provide: Keycloak, useValue: keycloak }]
}).compileComponents();
fixture = TestBed.createComponent(Home);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
}
it('si crea correttamente', async () => {
keycloak = { authenticated: false, login: vi.fn() };
await setup();
expect(component).toBeTruthy();
});
it('mostra la card del servizio Attività puntando a attivitaFeBaseUrl', async () => {
keycloak = { authenticated: false, login: vi.fn() };
await setup();
const compiled = fixture.nativeElement as HTMLElement;
const link = compiled.querySelector('a.servizio-card__link') as HTMLAnchorElement;
expect(link).toBeTruthy();
expect(link.getAttribute('href')).toBe(environment.attivitaFeBaseUrl);
});
it('mostra l\'avviso di accesso e il bottone "Accedi" se l\'utente non è autenticato', async () => {
keycloak = { authenticated: false, login: vi.fn() };
await setup();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('.servizio-card__avviso')?.textContent).toContain(
'Accedi per poter aggiungere attività'
);
const button = compiled.querySelector('.servizio-card button') as HTMLButtonElement;
expect(button).toBeTruthy();
button.click();
expect(keycloak.login).toHaveBeenCalledWith({ redirectUri: window.location.href });
});
it('non mostra l\'avviso di accesso se l\'utente è già autenticato', async () => {
keycloak = { authenticated: true, login: vi.fn() };
await setup();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('.servizio-card__avviso')).toBeNull();
expect(compiled.querySelector('.servizio-card button')).toBeNull();
});
describe('sezione amministrazione', () => {
it('è visibile con i link a /gruppi e /richieste-creazione-gruppo per admin', async () => {
keycloak = {
authenticated: true,
login: vi.fn(),
tokenParsed: { realm_access: { roles: ['admin'] } }
};
await setup();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.textContent).toContain('Amministrazione');
const links = Array.from(compiled.querySelectorAll('.amministrazione__links a')) as HTMLAnchorElement[];
expect(links.map((a) => a.getAttribute('href'))).toEqual(['/gruppi', '/richieste-creazione-gruppo']);
});
it('non è visibile per un utente senza ruolo admin', async () => {
keycloak = {
authenticated: true,
login: vi.fn(),
tokenParsed: { realm_access: { roles: ['censito'] } }
};
await setup();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.textContent).not.toContain('Amministrazione');
});
});
});