Select Git revision
app.component.spec.ts
app.component.spec.ts 6.88 KiB
import { ApiRootLinkRel, ApiRootResource, ApiRootService } from '@alfa-client/api-root-shared';
import {
HasLinkPipe,
createEmptyStateResource,
createStateResource,
} from '@alfa-client/tech-shared';
import {
Mock,
dispatchEventFromFixture,
existsAsHtmlElement,
getElementFromFixture,
mock,
notExistsAsHtmlElement,
} from '@alfa-client/test-utils';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Router, RouterOutlet } from '@angular/router';
import {
AdminLogoIconComponent,
MailboxIconComponent,
NavItemComponent,
NavbarComponent,
OrgaUnitIconComponent,
} from '@ods/system';
import { AuthenticationService } from 'authentication';
import { NavigationComponent } from 'libs/admin/settings/src/lib/navigation/navigation.component';
import { createApiRootResource } from 'libs/api-root-shared/test/api-root';
import { getDataTestIdOf } from 'libs/tech-shared/test/data-test';
import { MockComponent, MockDirective } from 'ng-mocks';
import { of } from 'rxjs';
import { UserProfileButtonContainerComponent } from '../common/user-profile-button-container/user-profile.button-container.component';
import { UnavailablePageComponent } from '../pages/unavailable/unavailable-page/unavailable-page.component';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
const adminHeaderSelector: string = getDataTestIdOf('admin-header');
const buildVersionSelector: string = getDataTestIdOf('build-version');
const userProfileButtonSelector: string = getDataTestIdOf('user-profile-button');
const navigationSelector: string = getDataTestIdOf('navigation');
const logoLink: string = getDataTestIdOf('logo-link');
const routerOutletSelector: string = getDataTestIdOf('router-outlet');
const authenticationService: Mock<AuthenticationService> = {
...mock(AuthenticationService),
login: jest.fn().mockResolvedValue(Promise.resolve()),
};
const router: Mock<Router> = mock(Router);
const apiRootService: Mock<ApiRootService> = mock(ApiRootService);
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AppComponent,
MockComponent(NavigationComponent),
MockComponent(AdminLogoIconComponent),
MockComponent(OrgaUnitIconComponent),
MockComponent(MailboxIconComponent),
MockComponent(UserProfileButtonContainerComponent),
MockComponent(UnavailablePageComponent),
MockComponent(NavbarComponent),
MockComponent(NavItemComponent),
HasLinkPipe,
MockDirective(RouterOutlet),
],
providers: [
{
provide: AuthenticationService,
useValue: authenticationService,
},
{
provide: ApiRootService,
useValue: apiRootService,
},
{
provide: Router,
useValue: router,
},
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
});
it(`should have as title 'admin'`, () => {
const appTitle: string = fixture.componentInstance.title;
expect(appTitle).toEqual('admin');
});
describe('ngOnInit', () => {
it('should call authService login', () => {
component.ngOnInit();
expect(authenticationService.login).toHaveBeenCalled();
});
it('should call doAfterLoggedIn', async () => {
component.doAfterLoggedIn = jest.fn();
component.ngOnInit();
await fixture.whenStable();
expect(component.doAfterLoggedIn).toHaveBeenCalled();
});
describe('do after logged in', () => {
it('should call apiRootService to getApiRoot', () => {
component.doAfterLoggedIn();
expect(apiRootService.getApiRoot).toHaveBeenCalled();
});
it('should navigate to default route', () => {
component.doAfterLoggedIn();
expect(router.navigate).toHaveBeenCalledWith(['/']);
});
});
});
it('show not show header if apiRoot is not loaded', () => {
component.apiRootStateResource$ = of(createEmptyStateResource<ApiRootResource>());
notExistsAsHtmlElement(fixture, adminHeaderSelector);
});
describe('user profile button', () => {
beforeEach(() => {
component.apiRootStateResource$ = of(createStateResource(createApiRootResource()));
});
it('should show if apiRoot exists', () => {
fixture.detectChanges();
existsAsHtmlElement(fixture, userProfileButtonSelector);
});
});
describe('administration logo', () => {
const apiResource: ApiRootResource = createApiRootResource();
beforeEach(() => {
component.apiRootStateResource$ = of(createStateResource(apiResource));
fixture.detectChanges();
});
it('should navigate to start page on click', () => {
dispatchEventFromFixture(fixture, logoLink, 'click');
expect(router.navigate).toHaveBeenCalledWith(['/']);
});
});
describe('navigation', () => {
beforeEach(() => {});
it('should show links if configuration link exists', () => {
component.apiRootStateResource$ = of(
createStateResource(createApiRootResource([ApiRootLinkRel.CONFIGURATION])),
);
fixture.detectChanges();
const navbarElement: HTMLElement = getElementFromFixture(fixture, navigationSelector);
expect(navbarElement.children.length).toBeGreaterThan(0);
});
it('should not not show links if configuration resource not available', () => {
component.apiRootStateResource$ = of(createStateResource(createApiRootResource([])));
fixture.detectChanges();
const navbarElement: HTMLElement = getElementFromFixture(fixture, navigationSelector);
expect(navbarElement.children.length).toBe(0);
});
});
describe('build version', () => {
const apiResource: ApiRootResource = createApiRootResource();
beforeEach(() => {
component.apiRootStateResource$ = of(createStateResource(apiResource));
});
it('should show after apiRoot loaded', () => {
fixture.detectChanges();
const buildVersionElement = getElementFromFixture(fixture, buildVersionSelector);
expect(buildVersionElement.textContent.trim()).toEqual(`Version: ${apiResource.version}`);
});
});
describe('router outlet', () => {
beforeEach(() => {});
it('should exist if configuration resource available', () => {
component.apiRootStateResource$ = of(
createStateResource(createApiRootResource([ApiRootLinkRel.CONFIGURATION])),
);
fixture.detectChanges();
existsAsHtmlElement(fixture, routerOutletSelector);
});
it('should not exist if configuration resource not available', () => {
component.apiRootStateResource$ = of(createStateResource(createApiRootResource()));
fixture.detectChanges();
notExistsAsHtmlElement(fixture, routerOutletSelector);
});
});
});