mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
76634: Update tests for AppComponent
This commit is contained in:
@@ -32,6 +32,7 @@ import { storeModuleConfig } from './app.reducer';
|
|||||||
import { LocaleService } from './core/locale/locale.service';
|
import { LocaleService } from './core/locale/locale.service';
|
||||||
import { authReducer } from './core/auth/auth.reducer';
|
import { authReducer } from './core/auth/auth.reducer';
|
||||||
import { provideMockStore } from '@ngrx/store/testing';
|
import { provideMockStore } from '@ngrx/store/testing';
|
||||||
|
import {GoogleAnalyticsService} from './statistics/google-analytics.service';
|
||||||
|
|
||||||
let comp: AppComponent;
|
let comp: AppComponent;
|
||||||
let fixture: ComponentFixture<AppComponent>;
|
let fixture: ComponentFixture<AppComponent>;
|
||||||
@@ -48,38 +49,40 @@ describe('App component', () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultTestBedConf = {
|
||||||
|
imports: [
|
||||||
|
CommonModule,
|
||||||
|
StoreModule.forRoot(authReducer, storeModuleConfig),
|
||||||
|
TranslateModule.forRoot({
|
||||||
|
loader: {
|
||||||
|
provide: TranslateLoader,
|
||||||
|
useClass: TranslateLoaderMock
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
declarations: [AppComponent], // declare the test component
|
||||||
|
providers: [
|
||||||
|
{ provide: NativeWindowService, useValue: new NativeWindowRef() },
|
||||||
|
{ provide: MetadataService, useValue: new MetadataServiceMock() },
|
||||||
|
{ provide: Angulartics2GoogleAnalytics, useValue: new AngularticsProviderMock() },
|
||||||
|
{ provide: Angulartics2DSpace, useValue: new AngularticsProviderMock() },
|
||||||
|
{ provide: AuthService, useValue: new AuthServiceMock() },
|
||||||
|
{ provide: Router, useValue: new RouterMock() },
|
||||||
|
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
|
||||||
|
{ provide: MenuService, useValue: menuService },
|
||||||
|
{ provide: CSSVariableService, useClass: CSSVariableServiceStub },
|
||||||
|
{ provide: HostWindowService, useValue: new HostWindowServiceStub(800) },
|
||||||
|
{ provide: LocaleService, useValue: getMockLocaleService() },
|
||||||
|
provideMockStore({ initialState }),
|
||||||
|
AppComponent,
|
||||||
|
RouteService
|
||||||
|
],
|
||||||
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
||||||
|
};
|
||||||
|
|
||||||
// waitForAsync beforeEach
|
// waitForAsync beforeEach
|
||||||
beforeEach(waitForAsync(() => {
|
beforeEach(waitForAsync(() => {
|
||||||
return TestBed.configureTestingModule({
|
return TestBed.configureTestingModule(defaultTestBedConf);
|
||||||
imports: [
|
|
||||||
CommonModule,
|
|
||||||
StoreModule.forRoot(authReducer, storeModuleConfig),
|
|
||||||
TranslateModule.forRoot({
|
|
||||||
loader: {
|
|
||||||
provide: TranslateLoader,
|
|
||||||
useClass: TranslateLoaderMock
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
declarations: [AppComponent], // declare the test component
|
|
||||||
providers: [
|
|
||||||
{ provide: NativeWindowService, useValue: new NativeWindowRef() },
|
|
||||||
{ provide: MetadataService, useValue: new MetadataServiceMock() },
|
|
||||||
{ provide: Angulartics2GoogleAnalytics, useValue: new AngularticsProviderMock() },
|
|
||||||
{ provide: Angulartics2DSpace, useValue: new AngularticsProviderMock() },
|
|
||||||
{ provide: AuthService, useValue: new AuthServiceMock() },
|
|
||||||
{ provide: Router, useValue: new RouterMock() },
|
|
||||||
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
|
|
||||||
{ provide: MenuService, useValue: menuService },
|
|
||||||
{ provide: CSSVariableService, useClass: CSSVariableServiceStub },
|
|
||||||
{ provide: HostWindowService, useValue: new HostWindowServiceStub(800) },
|
|
||||||
{ provide: LocaleService, useValue: getMockLocaleService() },
|
|
||||||
provideMockStore({ initialState }),
|
|
||||||
AppComponent,
|
|
||||||
RouteService
|
|
||||||
],
|
|
||||||
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
||||||
});
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// synchronous beforeEach
|
// synchronous beforeEach
|
||||||
@@ -113,4 +116,31 @@ describe('App component', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when GoogleAnalyticsService is provided', () => {
|
||||||
|
let googleAnalyticsSpy;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
// NOTE: Cannot override providers once components have been compiled, so TestBed needs to be reset
|
||||||
|
TestBed.resetTestingModule();
|
||||||
|
TestBed.configureTestingModule(defaultTestBedConf);
|
||||||
|
googleAnalyticsSpy = jasmine.createSpyObj('googleAnalyticsService', [
|
||||||
|
'addTrackingIdToPage',
|
||||||
|
]);
|
||||||
|
TestBed.overrideProvider(GoogleAnalyticsService, {useValue: googleAnalyticsSpy});
|
||||||
|
fixture = TestBed.createComponent(AppComponent);
|
||||||
|
comp = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create component', () => {
|
||||||
|
expect(comp).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('the constructor', () => {
|
||||||
|
it('should call googleAnalyticsService.addTrackingIdToPage()', () => {
|
||||||
|
expect(googleAnalyticsSpy.addTrackingIdToPage).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -14,7 +14,7 @@ export class GoogleAnalyticsService {
|
|||||||
@Inject(DOCUMENT) private document: Document
|
@Inject(DOCUMENT) private document: Document
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
addTrackingIdToPage() {
|
addTrackingIdToPage(): void {
|
||||||
this.configService.findByPropertyName('google.analytics.key').pipe(
|
this.configService.findByPropertyName('google.analytics.key').pipe(
|
||||||
getFirstCompletedRemoteData(),
|
getFirstCompletedRemoteData(),
|
||||||
).subscribe((remoteData) => {
|
).subscribe((remoteData) => {
|
||||||
|
Reference in New Issue
Block a user