Fix grafica scouthub-home-fe
This commit is contained in:
@@ -1,24 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import Keycloak from 'keycloak-js';
|
||||
import { Observable, of } from 'rxjs';
|
||||
|
||||
import { environment } from '../../environments/environment';
|
||||
import { OrganizationContextService } from '../core/organization-context.service';
|
||||
import { Home } from './home';
|
||||
|
||||
describe('Home', () => {
|
||||
let component: Home;
|
||||
let fixture: ComponentFixture<Home>;
|
||||
let organizationContext: { currentOrganizationName: Observable<string | null> };
|
||||
let keycloak: { logout: ReturnType<typeof vi.fn> };
|
||||
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: [
|
||||
{ provide: OrganizationContextService, useValue: organizationContext },
|
||||
{ provide: Keycloak, useValue: keycloak }
|
||||
]
|
||||
providers: [provideRouter([]), { provide: Keycloak, useValue: keycloak }]
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(Home);
|
||||
@@ -27,39 +26,73 @@ describe('Home', () => {
|
||||
await fixture.whenStable();
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
organizationContext = { currentOrganizationName: of('Agesci Milano 1') };
|
||||
keycloak = { logout: vi.fn() };
|
||||
});
|
||||
|
||||
it('si crea correttamente', async () => {
|
||||
keycloak = { authenticated: false, login: vi.fn() };
|
||||
await setup();
|
||||
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
|
||||
it('mostra il nome del gruppo scout corrente', async () => {
|
||||
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;
|
||||
expect(compiled.querySelector('h1')?.textContent).toContain('Agesci Milano 1');
|
||||
});
|
||||
|
||||
it('mostra il link verso l\'app Attività puntando a attivitaAppBaseUrl', async () => {
|
||||
await setup();
|
||||
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
const link = compiled.querySelector('a.servizio-card') as HTMLAnchorElement;
|
||||
const link = compiled.querySelector('a.servizio-card__link') as HTMLAnchorElement;
|
||||
expect(link).toBeTruthy();
|
||||
expect(link.getAttribute('href')).toBe(environment.attivitaAppBaseUrl);
|
||||
expect(link.getAttribute('href')).toBe(environment.attivitaFeBaseUrl);
|
||||
});
|
||||
|
||||
it('invoca il logout di Keycloak al click sul pulsante "Esci"', async () => {
|
||||
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;
|
||||
const button = compiled.querySelector('button') as HTMLButtonElement;
|
||||
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.logout).toHaveBeenCalledWith({ redirectUri: window.location.origin + '/' });
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user