add test 'when internet connection value changes'

pull/17054/head
Sinan997 2 years ago committed by Mahmut Gundogdu
parent 42918354ba
commit 1255f88034

@ -17,13 +17,13 @@ export class InternetConnectionService{
networkStatus = computed(() => this.status())
constructor(){
this.window.addEventListener('offline', () => this.setStatus());
this.window.addEventListener('online', () => this.setStatus());
this.window.addEventListener('offline', () => this.setStatus(false));
this.window.addEventListener('online', () => this.setStatus(true));
}
private setStatus(){
this.status.set(navigator.onLine)
this.status$.next(navigator.onLine)
setStatus(val:boolean){
this.status.set(val)
this.status$.next(val)
}
get networkStatus$(){

@ -2,7 +2,8 @@ import { TestBed} from '@angular/core/testing';
import { DOCUMENT } from '@angular/common';
import { InternetConnectionService } from '../services/internet-connection-service';
import { first, takeLast } from 'rxjs';
import { BehaviorSubject, first, of, takeLast } from 'rxjs';
import { computed, signal } from '@angular/core';
let service: InternetConnectionService;
@ -54,4 +55,42 @@ describe('Internet connection when connected', () => {
done();
});
});
});
describe('when connection value changes', () => {
let mockDocument = { defaultView: {navigator: {onLine: true}, addEventListener: (eventName,cb)=>{
window.addEventListener(eventName,cb)
}, dispatchEvent: function(event){
this.navigator.onLine = !this.navigator.onLine
}}}
beforeEach(() => {
TestBed.configureTestingModule({
providers:[{provide:DOCUMENT, useValue: mockDocument}]
})
service = TestBed.inject(InternetConnectionService);
});
it('check is signal value changes', () => {
expect(service.networkStatus()).toEqual(true);
const event = new Event("offline");
service.window.dispatchEvent(event)
service.setStatus(service.window.navigator.onLine)
expect(service.networkStatus()).toEqual(false);
});
it('check is observable value changes',(done: any) => {
const event = new Event("online");
service.window.dispatchEvent(event)
service.networkStatus$.pipe(first()).subscribe(value => {
expect(value).toEqual(true)
done()
});
service.window.dispatchEvent(event)
service.setStatus(service.window.navigator.onLine)
service.networkStatus$.pipe().subscribe(value => {
expect(value).toEqual(false)
done()
});
});
});
Loading…
Cancel
Save