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 { authReducer } from './core/auth/auth.reducer';
import { provideMockStore } from '@ngrx/store/testing';
import {GoogleAnalyticsService} from './statistics/google-analytics.service';
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
@@ -48,9 +49,7 @@ describe('App component', () => {
});
}
// waitForAsync beforeEach
beforeEach(waitForAsync(() => {
return TestBed.configureTestingModule({
const defaultTestBedConf = {
imports: [
CommonModule,
StoreModule.forRoot(authReducer, storeModuleConfig),
@@ -79,7 +78,11 @@ describe('App component', () => {
RouteService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
});
};
// waitForAsync beforeEach
beforeEach(waitForAsync(() => {
return TestBed.configureTestingModule(defaultTestBedConf);
}));
// 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
) { }
addTrackingIdToPage() {
addTrackingIdToPage(): void {
this.configService.findByPropertyName('google.analytics.key').pipe(
getFirstCompletedRemoteData(),
).subscribe((remoteData) => {