Adds tests for the update service

parent 346852c4
import { UpdateActivatedEvent, UpdateAvailableEvent } from '@angular/service-worker';
import { Subject } from 'rxjs';
export class SwUpdateMock {
public isEnabled: true;
public readonly available: Subject<UpdateAvailableEvent> = new Subject<UpdateAvailableEvent>();
public readonly activated: Subject<UpdateActivatedEvent> = new Subject<UpdateActivatedEvent>();
public checkForUpdate(): Promise<void> {
return new Promise<void>(resolve => resolve());
}
}
import { TestBed } from '@angular/core/testing';
import { SwUpdate } from '@angular/service-worker';
import { TranslateService } from '@ngx-translate/core';
import { TOAST_CONFIG } from 'ngx-toastr';
import { TOAST_CONFIG, ToastrService } from 'ngx-toastr';
import { of, Subject } from 'rxjs';
import { SwUpdateMock } from '../../../_mocks/SwUpdateMock';
import { TrackingMockService } from '../tracking/tracking.mock.service';
import { TranslateServiceMock } from '../../../_mocks/TranslateServiceMock';
import { UpdateCheckService } from './update-check.service';
describe('UpdateCheckService', () => {
......@@ -15,13 +16,23 @@ describe('UpdateCheckService', () => {
useClass: SwUpdateMock,
}, {
provide: TranslateService,
useClass: TrackingMockService,
useClass: TranslateServiceMock,
}, {
provide: TOAST_CONFIG,
useValue: {
default: {},
config: {},
},
}, {
provide: ToastrService,
useValue: {
remove: () => {},
info: () => {
return {
onTap: new Subject(),
};
},
},
},
],
}));
......@@ -30,4 +41,53 @@ describe('UpdateCheckService', () => {
const service: UpdateCheckService = TestBed.get(UpdateCheckService);
expect(service).toBeTruthy();
});
it('should prompt a user when a new version is available', () => {
const toastService: ToastrService = TestBed.get(ToastrService);
spyOn(toastService, 'remove').and.callFake(() => service['swUpdateToast'] = null);
spyOn(toastService, 'info').and.callFake(() => (
{ onTap: of({}) } as any
));
const service: UpdateCheckService = TestBed.get(UpdateCheckService);
// @ts-ignore
service['INTERVAL_PERIOD'] = 1;
service['swUpdateToast'] = { toastId: 1 } as any;
service['promptUser']({
available: {
appData: '',
hash: '',
},
current: {
appData: '',
hash: '',
},
type: 'UPDATE_AVAILABLE',
});
expect(toastService.remove).toHaveBeenCalledWith(1);
expect(toastService.info).toHaveBeenCalled();
});
it('should check for updates', () => {
const service: UpdateCheckService = TestBed.get(UpdateCheckService);
service.checkForUpdates();
expect(service).toBeTruthy();
});
it('should query for new updates', () => {
const service: UpdateCheckService = TestBed.get(UpdateCheckService);
const swUpdate: SwUpdate = TestBed.get(SwUpdate);
spyOn(swUpdate, 'checkForUpdate').and.callThrough();
service.doCheck();
expect(swUpdate.checkForUpdate).toHaveBeenCalled();
});
it('should clear all window caches', done => {
const service: UpdateCheckService = TestBed.get(UpdateCheckService);
service.clearCache().finally(() => {
expect(service).toBeTruthy();
done();
});
});
});
......@@ -9,10 +9,11 @@ import { interval } from 'rxjs';
})
export class UpdateCheckService {
private swUpdateToast: ActiveToast<any>;
private readonly INTERVAL_PERIOD = 21600;
constructor(private updates: SwUpdate, private translateService: TranslateService, private toastService: ToastrService) {
if (updates.isEnabled) {
interval(6 * 60 * 60).subscribe(() => this.doCheck().then(() => console.log('checking for updates')));
interval(this.INTERVAL_PERIOD).subscribe(() => this.doCheck().then(() => console.log('checking for updates')));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment