|
|
|
@ -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() |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |