Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1074 - Unit tests: UI: Expand unit tests for github code - Part I #709

Merged
merged 4 commits into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,23 @@
win.focus();

return this.$q(function (resolve, reject) {
that.$window.addEventListener('message', function (event) {
var message = angular.fromJson(event.data);
that.$window.addEventListener('message', onMessage);

that.eventService.$on('vcs.OAUTH_CANCELLED', function () {
that.$window.removeEventListener('message', onMessage);
reject('VCS_OAUTH_CANCELLED');
});

function onMessage(event) {
var message = angular.fromJson(event.data);
if (message.name === 'VCS OAuth - success') {
resolve();
win.close();
} else if (message.name === 'VCS OAuth - failure') {
reject();
}
});

that.eventService.$on('vcs.OAUTH_CANCELLED', function () {
reject('VCS_OAUTH_CANCELLED');
});
that.$window.removeEventListener('message', onMessage);
}
});
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(function () {
'use strict';
/* eslint-disable angular/no-private-call */
/* eslint-disable angular/timeout-service */

describe('github.view.githubOauthService - ', function () {
var that;

beforeEach(module('green-box-console'));
beforeEach(inject(function ($injector) {
that = $injector.get('github.view.githubOauthService');
}));

it('should be defined', function () {
expect(that).toBeDefined();
});

it('should have right properties', function () {
expect(that.GITHUB_ENDPOINTS).toEqual({
URL: 'https://github.com',
API_URL: 'https://api.github.com'
});
});

it('should have right interface', function () {
expect(that.start).toBeDefined();
expect(that.cancel).toBeDefined();
});

it('#cancel', function () {
spyOn(that.eventService, '$emit');
that.cancel();
expect(that.eventService.$emit).toHaveBeenCalledWith('vcs.OAUTH_CANCELLED');
});

it('#start, promise should be rejected when event `VCS_OAUTH_CANCELLED` is fired', function () {
var promise = that.start();
that.eventService.$emit('vcs.OAUTH_CANCELLED');
expectRejectedWith(promise, 'VCS_OAUTH_CANCELLED');
});

it('#start, promise should be resolved with message {"name": "VCS OAuth - success"}', function (done) {
var promise = that.start();
that.$window.postMessage('{"name": "VCS OAuth - success"}', '*');
setTimeout(function () {
expectResolveWith(promise, undefined);
done();
}, 0);
});

it('#start, promise should be rejected with message {"name": "VCS OAuth - failure"}', function (done) {
var promise = that.start();
that.$window.postMessage('{"name": "VCS OAuth - failure"}', '*');
setTimeout(function () {
expectRejectedWith(promise, undefined);
done();
}, 0);
});

it('#start, promise should be ignored with other messages', function (done) {
var promise = that.start();
that.$window.postMessage('{}', '*');
setTimeout(function () {
expect(promise.$$state.status).toBe(0);
done();
}, 0);
});
});

function expectResolveWith(promise, value) {
expect(promise.$$state.status).toBe(1);
expect(promise.$$state.value).toBe(value);
}

function expectRejectedWith(promise, value) {
expect(promise.$$state.status).toBe(2);
expect(promise.$$state.value).toBe(value);
}

/* eslint-enable angular/timeout-service */
/* eslint-enable angular/no-private-call */

})();