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 = 'http://ift.tt/10y7hK8?';
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 welcome 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://ift.tt/1HPovnu"12313123');
// window.postMessage('exit', '*');
expect(accountsService.setPro).toHaveBeenCalledWith(scope.pro);
});
Any idea how I'll make it call that event listener?
Aucun commentaire:
Enregistrer un commentaire