Fixed SW registration issues due to pending mikrotasks

This commit is contained in:
FinnStutzenstein 2019-03-06 07:38:43 +01:00
parent 118b853a91
commit e1fcda7890
3 changed files with 31 additions and 13 deletions

View File

@ -1,10 +1,11 @@
import { TestBed, async } from '@angular/core/testing';
import { TestBed, async, fakeAsync, tick } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { E2EImportsModule } from './../e2e-imports.module';
import { ServertimeService } from './core/core-services/servertime.service';
import { TranslateService } from '@ngx-translate/core';
describe('AppComponent', () => {
let servertimeService;
let servertimeService, translate;
beforeEach(async(() => {
TestBed.configureTestingModule({
@ -12,12 +13,21 @@ describe('AppComponent', () => {
}).compileComponents();
servertimeService = TestBed.get(ServertimeService);
translate = TestBed.get(TranslateService);
spyOn(servertimeService, 'startScheduler').and.stub();
spyOn(translate, 'addLangs').and.stub();
spyOn(translate, 'setDefaultLang').and.stub();
spyOn(translate, 'getBrowserLang').and.stub();
spyOn(translate, 'getLangs').and.returnValue([]);
spyOn(translate, 'use').and.stub();
}));
it('should create the app', async(() => {
it('should create the app', fakeAsync(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
expect(servertimeService.startScheduler).toHaveBeenCalled();
tick();
fixture.whenStable().then(() => {
expect(servertimeService.startScheduler).toHaveBeenCalled();
});
}));
});

View File

@ -1,6 +1,8 @@
import { Component } from '@angular/core';
import { Component, ApplicationRef } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { take, filter } from 'rxjs/operators';
import { ConfigService } from './core/ui-services/config.service';
import { ConstantsService } from './core/ui-services/constants.service';
import { CountUsersService } from './core/ui-services/count-users.service';
@ -45,7 +47,8 @@ export class AppComponent {
themeService: ThemeService,
countUsersService: CountUsersService, // Needed to register itself.
configService: ConfigService,
loadFontService: LoadFontService
loadFontService: LoadFontService,
appRef: ApplicationRef
) {
// manually add the supported languages
translate.addLangs(['en', 'de', 'cs']);
@ -57,7 +60,17 @@ export class AppComponent {
translate.use(translate.getLangs().includes(browserLang) ? browserLang : 'en');
// change default JS functions
this.overloadArrayToString();
servertimeService.startScheduler();
appRef.isStable.subscribe(s => console.log('is stable', s));
appRef.isStable
.pipe(
filter(s => s),
take(1)
)
.subscribe(() => {
console.log('start scheduler');
servertimeService.startScheduler();
});
}
/**

View File

@ -1,4 +1,4 @@
import { enableProdMode, NgModuleRef } from '@angular/core';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
@ -10,9 +10,4 @@ if (environment.production) {
platformBrowserDynamic()
.bootstrapModule(AppModule)
.then(() => {
if ('serviceWorker' in navigator && environment.production) {
navigator.serviceWorker.register('ngsw-worker.js');
}
})
.catch(err => console.log(err));