Skip to content
Snippets Groups Projects
portal-home.component.spec.ts 3.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Component, Input } from '@angular/core';
    import { TestBed, waitForAsync, ComponentFixture  } from '@angular/core/testing';
    import { RouterTestingModule } from '@angular/router/testing';
    
    import { provideMockStore, MockStore } from '@ngrx/store/testing';
    
    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() isAuthenticated: boolean;
            @Input() userProfile: UserProfile = null;
            @Input() baseHref: string;
            @Input() authenticationEnabled: boolean;
        }
    
        @Component({ selector: 'app-instance-card', template: '' })
        class InstanceCardStubComponent {
            @Input() instance: Instance;
        }
    
        let component: PortalHomeComponent;
        let fixture: ComponentFixture<PortalHomeComponent>;
        let store: MockStore;
        let config: AppConfigService
    
        beforeEach(waitForAsync(() => {
            TestBed.configureTestingModule({
                imports: [
                    RouterTestingModule
                ],
                declarations: [
                    PortalHomeComponent,
                    NavbarStubComponent,
                    InstanceCardStubComponent
                ],
                providers: [
                    provideMockStore({ }),
                    AppConfigService
                ]
            }).compileComponents();
            fixture = TestBed.createComponent(PortalHomeComponent);
            component = fixture.componentInstance;
            store = TestBed.inject(MockStore);
            config = TestBed.inject(AppConfigService);
        }));
    
        it('should create the portal home component', () => {
            expect(component).toBeDefined();
        });
    
        it('getBaseHref() should give base href config key value', () => {
            config.baseHref = '/my-project';
            expect(component.getBaseHref()).toBe('/my-project');
        });
    
        it('authenticationEnabled() should give authentication enabled config key value', () => {
            config.authenticationEnabled = true;
            expect(component.getAuthenticationEnabled()).toBeTruthy();
        });
    
        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', () => {
            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', () => {
            const spy = jest.spyOn(store, 'dispatch');
            component.openEditProfile();
            expect(spy).toHaveBeenCalledTimes(1);
            expect(spy).toHaveBeenCalledWith(authActions.openEditProfile());
        });
    });