Newer
Older
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';
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);
document.body.innerHTML =
'<title id="title">Default title</title>' +
'<link id="favicon" href="">';
}));
it('should create the component', () => {
expect(component).toBeDefined();
});
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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');
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' }
];
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());
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');
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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);
});
});