javascript - Jasmine test, addEventListener - Stack Overflow

I'm trying to write a test using jasmine. It's for an angular app where I'm opening a wi

I'm trying to write a test using jasmine. It's for an angular app where I'm opening a window using $window.open and then settings a couple of event listeners.

Here is controller:

$scope.login = function() {

    LoadingService.show();
    AccountsService.setPro($scope.pro);

    if ($scope.pro) {
        options.scope.push("repo");
    }

    var githubUrl = '?';
    var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scope;
    var authWindow = $window.open(authUrl, '_blank', 'location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=Close,clearcache=yes');

    authWindow.addEventListener('loadstart', function(e) {

        var url = (typeof e.url !== 'undefined' ? e.url : e.originalEvent.url),
            raw_code = /code=([^&]*)/.exec(e.url) || null,
            code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
            error = /\?error=(.+)$/.exec(e.url);

        if (code || error) {
            authWindow.close();
        }

        // If there is a code, proceed to get token from GitHub
        if (code) {
            requestToken(code);
        } else if (error) {
            AlertService.raiseAlert("Oops! Something went wrong and we couldn't log you in using Github. Please try again.");
            LoadingService.hide();
        }

    });

    // If "Done" button is pressed, hide "Loading"
    authWindow.addEventListener('exit', function(e) {
        LoadingService.hide();
    }, false);

};

And here is the test that I wrote passes because it can't get in authWindow.addEventListener('loadstart', function(e) { ... }

it("Should go to the wele screen.", function () {

    spyOn(loadingService, 'show');
    spyOn(accountsService, 'setPro');

    spy = jasmine.createSpy('message');

    window.addEventListener('message', function (e) {
        console.log(Object.keys(e), e.data);
        spy();
    });

    expect(scope.pro).toBeFalsy();

    var controller = createController();

    scope.togglePro();
    expect(scope.pro).toBeTruthy();

    scope.login();
    expect(loadingService.show).toHaveBeenCalled();

    window.postMessage('loadstart', '/?code="12313123');
    // window.postMessage('exit', '*');

    expect(accountsService.setPro).toHaveBeenCalledWith(scope.pro);

});

Any idea how I'll make it call that event listener?

I'm trying to write a test using jasmine. It's for an angular app where I'm opening a window using $window.open and then settings a couple of event listeners.

Here is controller:

$scope.login = function() {

    LoadingService.show();
    AccountsService.setPro($scope.pro);

    if ($scope.pro) {
        options.scope.push("repo");
    }

    var githubUrl = 'https://github./login/oauth/authorize?';
    var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scope;
    var authWindow = $window.open(authUrl, '_blank', 'location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=Close,clearcache=yes');

    authWindow.addEventListener('loadstart', function(e) {

        var url = (typeof e.url !== 'undefined' ? e.url : e.originalEvent.url),
            raw_code = /code=([^&]*)/.exec(e.url) || null,
            code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
            error = /\?error=(.+)$/.exec(e.url);

        if (code || error) {
            authWindow.close();
        }

        // If there is a code, proceed to get token from GitHub
        if (code) {
            requestToken(code);
        } else if (error) {
            AlertService.raiseAlert("Oops! Something went wrong and we couldn't log you in using Github. Please try again.");
            LoadingService.hide();
        }

    });

    // If "Done" button is pressed, hide "Loading"
    authWindow.addEventListener('exit', function(e) {
        LoadingService.hide();
    }, false);

};

And here is the test that I wrote passes because it can't get in authWindow.addEventListener('loadstart', function(e) { ... }

it("Should go to the wele screen.", function () {

    spyOn(loadingService, 'show');
    spyOn(accountsService, 'setPro');

    spy = jasmine.createSpy('message');

    window.addEventListener('message', function (e) {
        console.log(Object.keys(e), e.data);
        spy();
    });

    expect(scope.pro).toBeFalsy();

    var controller = createController();

    scope.togglePro();
    expect(scope.pro).toBeTruthy();

    scope.login();
    expect(loadingService.show).toHaveBeenCalled();

    window.postMessage('loadstart', 'http://www.github./?code="12313123');
    // window.postMessage('exit', '*');

    expect(accountsService.setPro).toHaveBeenCalledWith(scope.pro);

});

Any idea how I'll make it call that event listener?

Share Improve this question asked Mar 26, 2015 at 19:53 manosimmanosim 3,69013 gold badges47 silver badges68 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4
// in your test add a mock for window (remember to reset back to normal window after)
windowmock.open = function(url, target, settings){

    //test the url, target and settings

    return {
        addEventListener: function(event, callback){
            if (event == 'loadstart'){
                callback({
                    url: 'something. or whatever you're expecting,
                    originalEvent:{},

                });
            }
        },
        close: function(){}
    }
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744340660a4569382.html

相关推荐

  • javascript - Jasmine test, addEventListener - Stack Overflow

    I'm trying to write a test using jasmine. It's for an angular app where I'm opening a wi

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信