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 { AppComponent } from './app.component';
import { E2EImportsModule } from './../e2e-imports.module'; import { E2EImportsModule } from './../e2e-imports.module';
import { ServertimeService } from './core/core-services/servertime.service'; import { ServertimeService } from './core/core-services/servertime.service';
import { TranslateService } from '@ngx-translate/core';
describe('AppComponent', () => { describe('AppComponent', () => {
let servertimeService; let servertimeService, translate;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@ -12,12 +13,21 @@ describe('AppComponent', () => {
}).compileComponents(); }).compileComponents();
servertimeService = TestBed.get(ServertimeService); servertimeService = TestBed.get(ServertimeService);
translate = TestBed.get(TranslateService);
spyOn(servertimeService, 'startScheduler').and.stub(); 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 fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance; const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy(); expect(app).toBeTruthy();
tick();
fixture.whenStable().then(() => {
expect(servertimeService.startScheduler).toHaveBeenCalled(); 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 { TranslateService } from '@ngx-translate/core';
import { take, filter } from 'rxjs/operators';
import { ConfigService } from './core/ui-services/config.service'; import { ConfigService } from './core/ui-services/config.service';
import { ConstantsService } from './core/ui-services/constants.service'; import { ConstantsService } from './core/ui-services/constants.service';
import { CountUsersService } from './core/ui-services/count-users.service'; import { CountUsersService } from './core/ui-services/count-users.service';
@ -45,7 +47,8 @@ export class AppComponent {
themeService: ThemeService, themeService: ThemeService,
countUsersService: CountUsersService, // Needed to register itself. countUsersService: CountUsersService, // Needed to register itself.
configService: ConfigService, configService: ConfigService,
loadFontService: LoadFontService loadFontService: LoadFontService,
appRef: ApplicationRef
) { ) {
// manually add the supported languages // manually add the supported languages
translate.addLangs(['en', 'de', 'cs']); translate.addLangs(['en', 'de', 'cs']);
@ -57,7 +60,17 @@ export class AppComponent {
translate.use(translate.getLangs().includes(browserLang) ? browserLang : 'en'); translate.use(translate.getLangs().includes(browserLang) ? browserLang : 'en');
// change default JS functions // change default JS functions
this.overloadArrayToString(); this.overloadArrayToString();
appRef.isStable.subscribe(s => console.log('is stable', s));
appRef.isStable
.pipe(
filter(s => s),
take(1)
)
.subscribe(() => {
console.log('start scheduler');
servertimeService.startScheduler(); 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 { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module'; import { AppModule } from './app/app.module';
@ -10,9 +10,4 @@ if (environment.production) {
platformBrowserDynamic() platformBrowserDynamic()
.bootstrapModule(AppModule) .bootstrapModule(AppModule)
.then(() => {
if ('serviceWorker' in navigator && environment.production) {
navigator.serviceWorker.register('ngsw-worker.js');
}
})
.catch(err => console.log(err)); .catch(err => console.log(err));