Here is my component for implementing the swUpdate
:
public thereIsUpdate: boolean;
constructor(public swUpdate: SwUpdate) {
this.checkForUpdates();
}
checkForUpdates() {
this.swUpdate.checkForUpdate().then(() => {
this.swUpdate.available.subscribe(swEvt => {
// an update is available
this.thereIsUpdate = true;
});
});
}
and here is my unit testing :
class MockSwUpdate {
available = new Subject<{ available: { hash: string } }>().asObservable();
activated = new Subject<{ current: { hash: string } }>().asObservable();
public checkForUpdate(): Promise<void> {
console.log('This is not working');
return new Promise((resolve) => resolve());
}
public activateUpdate(): Promise<void> {
return new Promise((resolve) => resolve());
}
constructor(public isEnabled: boolean) {
}
}
class MockApplicationRef {
isStable = new Subject<boolean>();
}
describe('xComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
xComponent
],
imports: [
RouterTestingModule,
ServiceWorkerModule.register('', {enabled: false})
],
providers: [
{provide: SwUpdate, useValue: MockSwUpdate},
{provide: ApplicationRef, useClass: MockApplicationRef}
]
}).compileComponents();
}));
}
My problem is cannot doing the "mock" for swUpdate
, somehow it is not working.
Somehow the SwUpdate
is not mocked even i have specify it.
When i run ng test
, it is showing this error :
Uncaught TypeError: this.swUpdate.checkForUpdate is not a function thrown
TypeError: this.swUpdate.checkForUpdate is not a function
Please note that : The mock is only not working for the SwUpdate
.