vendredi 9 septembre 2016

angular 2 testing - stubing a window open and flush interval

I need to unit test this part of my code in MyService

social(someUrl){
    let intervalTimer = Observable.interval(1000);

    let authWindow = window.open(someUrl);

    let  pingWindow = intervalTimer.subscribe(()=>{
        if(authWindow.closed){
            pingWindow.unsubscribe();
            return this.http.get(...)
        }
    })
}

it pops up with some Url which calls to execute some function in my backend and once it's done, it will be redirected to an empty page which closes the pop up (so just window.close() in the script)

So basically the code above checks every second if the pop up is still open, if not it unsubscribes and returns a http.get request as Observable.

I have two major questions:

  1. How to stub the window.open and test, that someUrl is being used, I've read that so far you can only use jasmine? In Sinon you have sinon.stub(window, 'open',()=>{})
  2. how to test the http.get inside the interval? I'm getting this error

1 periodic timer(s) still in the queue.

this is my test.spect setup

const mockHttpProvider = {
    deps: [MockBackend, BaseRequestOptions],
    useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
        return new Http(backend, defaultOptions)
    }
};

describe('my Test', () => {

beforeEachProviders(() => {
    return [
        MyService,
        MockBackend,
        BaseRequestOptions,
        provide(Http, mockHttpProvider)
    ]
});

it('should...', inject([MyService, MockBackend], fakeAsync((myService: MyService, backend: MockBackend) => {

        backend.connections.subscribe((connection: MockConnection) => {
???
        });

        myService.social().subscribe(res => {
             ???
        });
    })))

thanks!

Aucun commentaire:

Enregistrer un commentaire