Skip to content
Snippets Groups Projects
Commit 8dc15258 authored by Tifenn Guillas's avatar Tifenn Guillas
Browse files

Merge branch '11-add-tests-for-portal-module' into 'develop'

Resolve "Add tests for portal module"

Closes #11

See merge request !16
parents a1e81c62 02c207fa
No related branches found
No related tags found
2 merge requests!29Develop,!16Resolve "Add tests for portal module"
Pipeline #6489 passed
Pipeline: anis-next

#6490

    ......@@ -7,7 +7,6 @@ import { provideMockStore, MockStore } from '@ngrx/store/testing';
    import { AppComponent } from './app.component';
    import { AppConfigService } from 'src/app/app-config.service';
    import * as authActions from 'src/app/auth/auth.actions';
    import * as attributeActions from '../../metamodel/actions/attribute.actions';
    import * as instanceActions from '../../metamodel/actions/instance.actions';
    describe('AppComponent', () => {
    ......
    ......@@ -4,7 +4,7 @@
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    </div>
    <div class="card-footer bg-transparent text-right">
    <a routerLink="/instance/{{instance.name}}" class="btn btn-outline-primary" title="Go to instance">
    <a routerLink="/instance/{{ instance.name }}" class="btn btn-outline-primary" title="Go to instance">
    Go to instance
    </a>
    </div>
    ......
    import { TestBed, waitForAsync, ComponentFixture } from '@angular/core/testing';
    import { RouterTestingModule } from '@angular/router/testing';
    import { InstanceCardComponent } from './instance-card.component';
    describe('InstanceCardComponent', () => {
    let component: InstanceCardComponent;
    let fixture: ComponentFixture<InstanceCardComponent>;
    beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
    imports: [RouterTestingModule],
    declarations: [InstanceCardComponent]
    }).compileComponents();
    fixture = TestBed.createComponent(InstanceCardComponent);
    component = fixture.componentInstance;
    }));
    it('should create the component', () => {
    expect(component).toBeDefined();
    });
    });
    ......@@ -11,6 +11,10 @@ import { Component, Input, ChangeDetectionStrategy } from '@angular/core';
    import { Instance } from 'src/app/metamodel/models';
    /**
    * @class
    * @classdesc Instance card component.
    */
    @Component({
    selector: 'app-instance-card',
    templateUrl: 'instance-card.component.html',
    ......
    ......@@ -3,17 +3,18 @@ import { TestBed, waitForAsync, ComponentFixture } from '@angular/core/testing'
    import { RouterTestingModule } from '@angular/router/testing';
    import { provideMockStore, MockStore } from '@ngrx/store/testing';
    import { of } from 'rxjs';
    import { PortalHomeComponent } from './portal-home.component';
    import { AppConfigService } from 'src/app/app-config.service';
    import { UserProfile } from 'src/app/auth/user-profile.model';
    import { Instance } from 'src/app/metamodel/models';
    import * as authActions from 'src/app/auth/auth.actions';
    import { PortalHomeComponent } from './portal-home.component';
    describe('PortalHomeComponent', () => {
    @Component({ selector: 'app-navbar', template: '' })
    class NavbarStubComponent {
    @Input() links: {label: string, icon: string, routerLink: string}[];
    @Input() links: { label: string, icon: string, routerLink: string }[];
    @Input() isAuthenticated: boolean;
    @Input() userProfile: UserProfile = null;
    @Input() baseHref: string;
    ......@@ -28,13 +29,11 @@ describe('PortalHomeComponent', () => {
    let component: PortalHomeComponent;
    let fixture: ComponentFixture<PortalHomeComponent>;
    let store: MockStore;
    let config: AppConfigService
    let appConfigServiceStub = new AppConfigService();
    beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
    imports: [
    RouterTestingModule
    ],
    imports: [RouterTestingModule],
    declarations: [
    PortalHomeComponent,
    NavbarStubComponent,
    ......@@ -42,47 +41,81 @@ describe('PortalHomeComponent', () => {
    ],
    providers: [
    provideMockStore({ }),
    AppConfigService
    { provide: AppConfigService, useValue: appConfigServiceStub }
    ]
    }).compileComponents();
    fixture = TestBed.createComponent(PortalHomeComponent);
    component = fixture.componentInstance;
    store = TestBed.inject(MockStore);
    config = TestBed.inject(AppConfigService);
    document.body.innerHTML = '<link id="favicon" href="">';
    }));
    it('should create the portal home component', () => {
    it('should create the component', () => {
    expect(component).toBeDefined();
    });
    it('getBaseHref() should give base href config key value', () => {
    config.baseHref = '/my-project';
    it('should execute ngOnInit lifecycle and add admin link if no authentication', () => {
    appConfigServiceStub.authenticationEnabled = false;
    component.ngOnInit();
    const expected = [
    { label: 'Home', icon: 'fas fa-home', routerLink: '/portal' },
    { label: 'Admin', icon: 'fas fa-tools', routerLink: '/admin' }
    ];
    expect(component.links).toEqual(expected);
    expect(component.favIcon.href).toEqual('http://localhost/favicon.ico');
    });
    it('should execute ngOnInit lifecycle and add admin link depending on user rights', () => {
    appConfigServiceStub.authenticationEnabled = true;
    appConfigServiceStub.adminRole = 'admin';
    component.userRoles = of([]);
    component.ngOnInit();
    expect(component.links).toEqual([{ label: 'Home', icon: 'fas fa-home', routerLink: '/portal' }]);
    component.userRoles = of(['admin']);
    component.ngOnInit();
    const expected = [
    { label: 'Home', icon: 'fas fa-home', routerLink: '/portal' },
    { label: 'Admin', icon: 'fas fa-tools', routerLink: '/admin' }
    ];
    expect(component.links).toEqual(expected);
    });
    it('#getBaseHref() should return base href config key value', () => {
    appConfigServiceStub.baseHref = '/my-project';
    expect(component.getBaseHref()).toBe('/my-project');
    });
    it('authenticationEnabled() should give authentication enabled config key value', () => {
    config.authenticationEnabled = true;
    it('#authenticationEnabled() should return authentication enabled config key value', () => {
    appConfigServiceStub.authenticationEnabled = true;
    expect(component.getAuthenticationEnabled()).toBeTruthy();
    });
    it('login() should dispatch login action', () => {
    it('#login() should dispatch login action', () => {
    const spy = jest.spyOn(store, 'dispatch');
    component.login();
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(authActions.login());
    });
    it('logout() should dispatch logout action', () => {
    it('#logout() should dispatch logout action', () => {
    const spy = jest.spyOn(store, 'dispatch');
    component.logout();
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(authActions.logout());
    });
    it('openEditProfile() should dispatch open edit profile action', () => {
    it('#openEditProfile() should dispatch open edit profile action', () => {
    const spy = jest.spyOn(store, 'dispatch');
    component.openEditProfile();
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(authActions.openEditProfile());
    });
    it('should unsubscribe to user roles when component is destroyed', () => {
    component.userRolesSubscription = of().subscribe();
    const spy = jest.spyOn(component.userRolesSubscription, 'unsubscribe');
    component.ngOnDestroy();
    expect(spy).toHaveBeenCalledTimes(1);
    });
    });
    ......@@ -8,6 +8,7 @@
    */
    import { Component, OnInit, OnDestroy } from '@angular/core';
    import { Observable, Subscription } from 'rxjs';
    import { Store } from '@ngrx/store';
    ......@@ -18,26 +19,24 @@ import * as authSelector from 'src/app/auth/auth.selector';
    import * as instanceSelector from 'src/app/metamodel/selectors/instance.selector';
    import { AppConfigService } from 'src/app/app-config.service';
    @Component({
    selector: 'app-portal-home',
    templateUrl: 'portal-home.component.html'
    })
    /**
    * @class
    * @classdesc Portal home container.
    *
    * @implements OnInit
    * @implements OnDestroy
    */
    @Component({
    selector: 'app-portal-home',
    templateUrl: 'portal-home.component.html'
    })
    export class PortalHomeComponent implements OnInit, OnDestroy {
    public favIcon: HTMLLinkElement = document.querySelector('#favicon');
    public links = [
    { label: 'Home', icon: 'fas fa-home', routerLink: '/portal' }
    ];
    public links = [{ label: 'Home', icon: 'fas fa-home', routerLink: '/portal' }];
    public isAuthenticated: Observable<boolean>;
    public userProfile: Observable<UserProfile>;
    public userRoles: Observable<string[]>;
    public instanceList: Observable<Instance[]>;
    public userRolesSubscription: Subscription;
    constructor(private store: Store<{ }>, private config: AppConfigService) {
    ......@@ -61,27 +60,49 @@ export class PortalHomeComponent implements OnInit, OnDestroy {
    }
    }
    getBaseHref() {
    /**
    * Returns application base href.
    *
    * @return string
    */
    getBaseHref(): string {
    return this.config.baseHref;
    }
    getAuthenticationEnabled() {
    /**
    * Checks if authentication is enabled.
    *
    * @return boolean
    */
    getAuthenticationEnabled(): boolean {
    return this.config.authenticationEnabled;
    }
    /**
    * Dispatches action to log in.
    */
    login(): void {
    this.store.dispatch(authActions.login());
    }
    /**
    * Dispatches action to log out.
    */
    logout(): void {
    this.store.dispatch(authActions.logout());
    }
    /**
    * Dispatches action to open profile editor.
    */
    openEditProfile(): void {
    this.store.dispatch(authActions.openEditProfile());
    }
    ngOnDestroy() {
    /**
    * Unsubscribes to user roles when component is destroyed.
    */
    ngOnDestroy(): void {
    if (this.userRolesSubscription) this.userRolesSubscription.unsubscribe();
    }
    }
    ......@@ -17,6 +17,10 @@ const routes: Routes = [
    { path: 'home', component: PortalHomeComponent }
    ];
    /**
    * @class
    * @classdesc Portal routing module.
    */
    @NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
    ......
    ......@@ -13,6 +13,10 @@ import { SharedModule } from 'src/app/shared/shared.module';
    import { PortalRoutingModule, routedComponents } from './portal-routing.module';
    import { dummiesComponents } from './components';
    /**
    * @class
    * @classdesc Portal module.
    */
    @NgModule({
    imports: [
    SharedModule,
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment