Permalink
Browse files

Merge pull request #656 from jknowles-cs/online-tracker-latest

Refactored online-tracker.component to include a service (online-tracker-service)
  • Loading branch information...
maikofelix89 committed Mar 23, 2018
2 parents e21f811 + 38d226d commit 2bf6ad5552bc2224df7b2a0c41988625d9de70b6
@@ -32,12 +32,13 @@ import { CacheService } from 'ionic-cache';
import { DataCacheService } from './shared/services/data-cache.service';
import { FeedBackComponent } from './feedback';
import { FormVisitTypeSearchModule } from
'./patient-dashboard/common/form-visit-type-search/form-visit-type-search.module';
'./patient-dashboard/common/form-visit-type-search/form-visit-type-search.module';
import { BusyModule, BusyConfig } from 'angular2-busy';
import { LabOrderSearchModule } from './lab-order-search/lab-order-search.module';
import { ModalModule } from 'ngx-bootstrap/modal';
import { CookieModule } from 'ngx-cookie';
import { OnlineTrackerService } from './online-tracker/online-tracker.service';
// Application wide providers
const APP_PROVIDERS = [
@@ -113,6 +114,7 @@ export function httpClient(xhrBackend: XHRBackend, requestOptions: RequestOption
AuthGuard,
LoginGuard,
LocalStorageService,
OnlineTrackerService,
{
provide: Http,
useFactory: httpClient,
@@ -0,0 +1,6 @@
<p *ngIf="isOnline" class="text-bold"><i class="fa fa-circle text-success"></i>
<span></span>
</p>
<p *ngIf="!isOnline" class="text-bold"><i class="fa fa-circle text-danger"></i>
<span class="text-danger"><span *ngIf="isUpdating"> (updating...) </span></span>
</p>
@@ -0,0 +1,98 @@
/* tslint:disable:no-unused-variable */
import { TestBed, async, fakeAsync, ComponentFixture,
tick, discardPeriodicTasks } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { OnlineTrackerComponent } from './online-tracker.component';
import { OnlineTrackerService } from './online-tracker.service';
import { SessionService } from '../openmrs-api/session.service';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { Http, Response, Headers, BaseRequestOptions, ResponseOptions } from '@angular/http';
import { AppSettingsService } from './../app-settings/app-settings.service';
import { LocalStorageService } from './../utils/local-storage.service';
import { AppFeatureAnalytics } from './../shared/app-analytics/app-feature-analytics.service';
class DataStub {
public updateOnlineStatus(): Promise<any> {
alert('Data stub call');
return Promise.resolve(true);
}
}
describe('Component: OnlineTracker', () => {
let fixture: ComponentFixture<OnlineTrackerComponent>;
let component: OnlineTrackerComponent;
let debugElement: DebugElement;
let dataStub: OnlineTrackerService;
let element;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [OnlineTrackerComponent],
providers: [
{ provide: OnlineTrackerService, useClass: DataStub },
{
provide: Http,
useFactory: (
backendInstance: MockBackend,
defaultOptions: BaseRequestOptions
) => {
return new Http(backendInstance, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
SessionService,
MockBackend,
BaseRequestOptions,
AppSettingsService,
LocalStorageService,
AppFeatureAnalytics
]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(OnlineTrackerComponent);
debugElement = fixture.debugElement;
element = fixture.nativeElement;
component = fixture.componentInstance;
dataStub = fixture.debugElement.injector.get(OnlineTrackerService);
});
}));
it('should create an instance', () => {
expect(component).toBeTruthy();
});
it('should set online to true when updateOnline return true', fakeAsync(() => {
const spy = spyOn(dataStub, 'updateOnlineStatus').and.returnValue(
Promise.resolve(true)
);
fixture.detectChanges();
component.getOnlineStatus();
tick();
component.subscribeToTimer = false;
expect(component.isOnline).toBe(true);
expect(component.isUpdating).toBe(false);
expect(spy.calls.any()).toEqual(true);
discardPeriodicTasks();
}));
it('should set online to false when updateOnline return false', fakeAsync(() => {
const spy = spyOn(dataStub, 'updateOnlineStatus').and.returnValue(
Promise.resolve(false)
);
fixture.detectChanges();
component.getOnlineStatus();
tick();
component.subscribeToTimer = false;
expect(component.isOnline).toBe(false);
expect(component.isUpdating).toBe(true);
expect(spy.calls.any()).toEqual(true);
discardPeriodicTasks();
}));
});
@@ -1,46 +1,47 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit , OnDestroy } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http, Response, Headers } from '@angular/http';
import { SessionService } from '../openmrs-api/session.service';
import { OnlineTrackerService } from './online-tracker.service';
@Component({
selector: 'online-tracker',
template: `
<p *ngIf="isOnline" class="text-bold"><i class="fa fa-circle text-success"></i>
<span></span>
</p>
<p *ngIf="!isOnline" class="text-bold"><i class="fa fa-circle text-danger"></i>
<span class="text-danger"><span *ngIf="isUpdating"> (updating...) </span></span>
</p>
`
templateUrl: './online-tracker.component.html'
})
export class OnlineTrackerComponent implements OnInit {
export class OnlineTrackerComponent implements OnInit, OnDestroy {
public isOnline: boolean = false;
public isUpdating: boolean = false;
public timer: Observable<any>;
public subscribeToTimer: boolean = true;
constructor(private sessionService: SessionService) {
constructor(private _onlineTrackerService: OnlineTrackerService) {
}
public ngOnInit() {
let timer = Observable.timer(1000, 30000);
timer.subscribe((t) => this.updateOnlineStatus());
console.log('Tracker Loaded');
this.timer = Observable.timer(1000, 30000);
this.timer
.takeWhile(() => this.subscribeToTimer)
.subscribe((t) => this.getOnlineStatus());
}
public updateOnlineStatus() {
if (this.isUpdating) {
return;
}
public ngOnDestroy() {
this.subscribeToTimer = false;
console.log('Timer Unsubscription');
}
public getOnlineStatus() {
this.isUpdating = true;
let request = this.sessionService.getSession();
request
.subscribe(
(response: Response) => {
this.isUpdating = false;
this.isOnline = true;
}, (error) => {
this.isUpdating = false;
this.isOnline = false;
});
this._onlineTrackerService.updateOnlineStatus()
.then((results: any) => {
if (results) {
this.isOnline = results;
this.isUpdating = !results;
}
}).catch((error) => {
this.isOnline = false;
console.error('ERROR: GetOnline Status Error', error);
});
}
}
@@ -0,0 +1,48 @@
/* tslint:disable:no-unused-variable */
import { TestBed, async } from '@angular/core/testing';
import { Observable } from 'rxjs/Rx';
import { BaseRequestOptions, Http, HttpModule, Response,
ResponseOptions, RequestMethod } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { SessionService } from '../openmrs-api/session.service';
import { OnlineTrackerService } from './online-tracker.service';
import { AppSettingsService } from '../app-settings';
import { LocalStorageService } from '../utils/local-storage.service';
describe('Service: OnlineTracker', () => {
let onlineTrackerService: OnlineTrackerService;
let sessionServiceSpy: jasmine.SpyObj<SessionService>;
beforeEach(() => {
const spy = jasmine.createSpyObj('SessionService', ['getSession']);
TestBed.configureTestingModule({
providers: [
OnlineTrackerService,
{
provide: SessionService,
useValue: spy
},
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (backend, options) => new Http(backend, options),
deps: [MockBackend, BaseRequestOptions]
},
AppSettingsService,
LocalStorageService
]
});
onlineTrackerService = TestBed.get(OnlineTrackerService);
sessionServiceSpy = TestBed.get(SessionService);
});
it('should create an instance', () => {
expect(onlineTrackerService).toBeTruthy();
});
it('should get session when update online status is called', () => {
onlineTrackerService.updateOnlineStatus();
expect(sessionServiceSpy.getSession).toHaveBeenCalled();
});
});
@@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import { Observable, Subject, BehaviorSubject } from 'rxjs/Rx';
import { SessionService } from '../openmrs-api/session.service';
@Injectable()
export class OnlineTrackerService {
public isOnline: boolean = false;
constructor(private _sessionService: SessionService) {
}
public updateOnlineStatus() {
// console.log('Online Tracker Service: updateOnlineStatus');
return new Promise((resolve, reject) => {
this._sessionService.getSession()
.subscribe(
(results) => {
this.isOnline = true;
resolve(this.isOnline);
}, (error) => {
this.isOnline = false;
resolve(this.isOnline);
});
});
}
}

0 comments on commit 2bf6ad5

Please sign in to comment.