76634: Update tests for AppComponent

This commit is contained in:
Bruno Roemers
2021-02-02 12:28:26 +01:00
parent ce5da5723b
commit 3f6cf777b6
2 changed files with 61 additions and 31 deletions

View File

@@ -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,9 +49,7 @@ describe('App component', () => {
}); });
} }
// waitForAsync beforeEach const defaultTestBedConf = {
beforeEach(waitForAsync(() => {
return TestBed.configureTestingModule({
imports: [ imports: [
CommonModule, CommonModule,
StoreModule.forRoot(authReducer, storeModuleConfig), StoreModule.forRoot(authReducer, storeModuleConfig),
@@ -79,7 +78,11 @@ describe('App component', () => {
RouteService RouteService
], ],
schemas: [CUSTOM_ELEMENTS_SCHEMA] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}); };
// waitForAsync beforeEach
beforeEach(waitForAsync(() => {
return TestBed.configureTestingModule(defaultTestBedConf);
})); }));
// 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);
});
});
});
}); });

View File

@@ -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) => {