Skip to content
Snippets Groups Projects
instance.component.spec.ts 6.34 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { TestBed, waitForAsync, ComponentFixture  } from '@angular/core/testing';
    import { RouterTestingModule } from '@angular/router/testing';
    
    import { of } from 'rxjs';
    import { provideMockStore, MockStore } from '@ngrx/store/testing';
    
    import { InstanceComponent } from './instance.component';
    import { AppConfigService } from 'src/app/app-config.service';
    import * as authActions from 'src/app/auth/auth.actions';
    import { Component, EventEmitter, Input, Output } from '@angular/core';
    import { Attribute, Instance } from '../metamodel/models';
    import { UserProfile } from '../auth/user-profile.model';
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
    import * as datasetFamilyActions from '../metamodel/actions/dataset-family.actions';
    import * as datasetActions from '../metamodel/actions/dataset.actions';
    import * as surveyActions from '../metamodel/actions/survey.actions';
    
    
    describe('InstanceComponent', () => {
        @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;
            @Input() apiUrl: string;
            @Input() instance: Instance;
        }
    
        let component: InstanceComponent;
        let fixture: ComponentFixture<InstanceComponent>;
        let store: MockStore;
        let appConfigServiceStub = new AppConfigService();
    
        beforeEach(waitForAsync(() => {
            TestBed.configureTestingModule({
                imports: [RouterTestingModule],
                declarations: [
                    InstanceComponent,
                    NavbarStubComponent
                ],
                providers: [
                    provideMockStore({ }),
                    { provide: AppConfigService, useValue: appConfigServiceStub }
                ]
            }).compileComponents();
            fixture = TestBed.createComponent(InstanceComponent);
            component = fixture.componentInstance;
            store = TestBed.inject(MockStore);
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
            document.body.innerHTML =
                '<title id="title">Default title</title>' +
                '<link id="favicon" href="">';
    
        }));
    
        it('should create the component', () => {
            expect(component).toBeDefined();
        });
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        it('should execute ngOnInit lifecycle', (done) => {
            const instance: Instance = {
                name: 'myInstance',
                label: 'My Instance',
                data_path: 'data/path',
                config: {
                    design: {
                        design_color: 'green',
                        design_background_color: 'darker green',
                        design_logo: 'path/to/logo',
                        design_favicon: 'path/to/favicon'
                    },
                    home: {
                        home_component: 'HomeComponent',
                        home_config: {
                            home_component_text: 'Description',
                            home_component_logo: 'path/to/logo'
                        }
                    },
                    search: {
                        search_by_criteria_allowed: true,
                        search_by_criteria_label: 'Search',
                        search_multiple_allowed: true,
                        search_multiple_label: 'Search multiple',
                        search_multiple_all_datasets_selected: false
                    },
                    documentation: {
                        documentation_allowed: true,
                        documentation_label: 'Documentation'
                    }
                },
                nb_dataset_families: 1,
                nb_datasets: 2
            };
            component.instance = of(instance);
            const spy = jest.spyOn(store, 'dispatch');
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
            const expectedLinks = [
                { label: 'Home', icon: 'fas fa-home', routerLink: 'home' },
                { label: 'Search', icon: 'fas fa-search', routerLink: 'search' },
                { label: 'Search multiple', icon: 'fas fa-search-plus', routerLink: 'search-multiple' },
                { label: 'Documentation', icon: 'fas fa-question', routerLink: 'documentation' }
            ];
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
            component.ngOnInit();
            Promise.resolve(null).then(function() {
                expect(spy).toHaveBeenCalledTimes(3);
                expect(spy).toHaveBeenCalledWith(datasetFamilyActions.loadDatasetFamilyList());
                expect(spy).toHaveBeenCalledWith(datasetActions.loadDatasetList());
                expect(spy).toHaveBeenCalledWith(surveyActions.loadSurveyList());
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
                expect(component.links).toEqual(expectedLinks);
                expect(component.favIcon.href).toEqual('http://localhost/undefined/download-instance-file/myInstance/path/to/favicon');
                expect(component.title.textContent).toEqual('My Instance');
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
                done();
            });
        });
    
    
        it('#getBaseHref() should return base href config key value', () => {
            appConfigServiceStub.baseHref = '/my-project';
            expect(component.getBaseHref()).toBe('/my-project');
        });
    
        it('#authenticationEnabled() should return authentication enabled config key value', () => {
            appConfigServiceStub.authenticationEnabled = true;
            expect(component.getAuthenticationEnabled()).toBeTruthy();
        });
    
        it('#getApiUrl() should return API URL', () => {
            appConfigServiceStub.apiUrl = 'http:test.com';
            expect(component.getApiUrl()).toEqual('http:test.com');
        });
    
        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());
        });
    
        it('should unsubscribe to instance when component is destroyed', () => {
            component.instanceSubscription = of().subscribe();
            const spy = jest.spyOn(component.instanceSubscription, 'unsubscribe');
            component.ngOnDestroy();
            expect(spy).toHaveBeenCalledTimes(1);
        });
    });