Skip to content
Snippets Groups Projects
Select Git revision
  • afb777fe7e5831b2569bd967fea081859cffa331
  • main default protected
  • storybook-improvements
  • OZG-7287-forward-saml-token
  • release-administration
  • OZG-8422-BenutzerSpeichern
  • OZG-8314-Alfa-Vorgang-Bearbeiter-Zuweisung-entfernen
  • release-info
  • release
  • OZG-7856_schadcode-scanner-e2e
  • OZG-7985-fix-sorting
  • OZG-8305-Create-webpack-sbom
  • tooltip-improvements
  • OZG-7714-UpgradeKeycloakDependencyTo25
  • OZG-8086-Admin-Datenanfrage-erstellen
  • OZG-8086-Datenanfrage-Umbenennung
  • mongodb-7-0-16-e2e
  • OZG-6220-Bescheid-speichern-ohne-Postfach
  • OZG-7922-KeycloakOperatorExceptions
  • OZG-8142-poc-cards
  • OZG-8086-E2E
  • 1.12.1-administration
  • 1.12.0-administration
  • 1.12.0-info
  • 2.27.0-alfa
  • 1.11.0-info
  • 1.11.0-administration
  • 2.26.0-alfa
  • 1.10.0-info
  • 1.10.0-administration
  • 2.25.0-alfa
  • 1.9.0-info
  • 1.9.0-administration
  • 2.24.0-alfa
  • 1.8.0-info
  • 1.8.0-administration
  • 2.23.0-alfa
  • 1.7.0-info
  • 1.7.0-administration
  • 2.22.0-alfa
  • 1.6.0-info
41 results

app.component.spec.ts

Blame
  • 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);
        });
      });
    });