Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit b329d47

Browse files
author
vikasrohit
committedJan 18, 2016
Merge pull request #668 from appirio-tech/techdebt/unittests-for-services
Techdebt/unittests for services
2 parents 6b4dbde + ef38f7c commit b329d47

File tree

3 files changed

+364
-7
lines changed

3 files changed

+364
-7
lines changed
 

‎app/filters/npad.filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
if(inputStr.length >= n)
1515
return inputStr
16-
var zeros = "0".repeat(n);
16+
var zeros = new Array( n + 1 ).join("0");
1717
return (zeros + inputStr).slice(-1 * n)
1818
};
1919
}

‎app/services/helpers.service.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
answer: '' + q.answer
157157
};
158158

159-
if (q.comment.length > 0) {
159+
if (q.comment && q.comment.length > 0) {
160160
reviewItem.comments = [
161161
{
162162
content: '' + q.comment,
@@ -234,7 +234,7 @@
234234
});
235235
// now loop over all keys and replace with compiled value
236236
Object.keys(compiledMap).forEach(function(k) {
237-
template = template.replace(k, compiledMap[k])
237+
template = template.replace(k, (compiledMap[k] ? compiledMap[k] : ''));
238238
});
239239
}
240240
return template;
@@ -298,8 +298,8 @@
298298
}
299299

300300
function setupLoginEventMetrics (usernameOrEmail) {
301-
if (_kmq) {
302-
_kmq.push(['identify', usernameOrEmail ]);
301+
if ($window._kmq) {
302+
$window._kmq.push(['identify', usernameOrEmail ]);
303303
}
304304
}
305305

‎app/services/helpers.service.spec.js

Lines changed: 359 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ describe('Helper Service', function() {
44
var fakeWindow = {
55
location: {
66
href: "/"
7+
},
8+
decodeURIComponent: function(param) {
9+
return decodeURIComponent(param);
710
}
811
};
912
// sinon.spy(fakeWindow.location, "href");
@@ -19,13 +22,13 @@ describe('Helper Service', function() {
1922
sinon.spy(fakeState, "go");
2023

2124
beforeEach(function() {
22-
module('tc.services', function($provide) {
25+
module('topcoder', function($provide) {
2326
$provide.value('$window', fakeWindow);
2427
$provide.value('$state', fakeState);
2528
$provide.value('$location', fakeLocation);
2629
});
2730

28-
bard.inject(this, 'Helpers', '$state', '$location');
31+
bard.inject(this, 'Helpers', '$rootScope', '$state', '$location', '$window', '$httpBackend');
2932
});
3033

3134
describe("isEmail()", function() {
@@ -54,4 +57,358 @@ describe('Helper Service', function() {
5457
expect($location.url).to.have.been.calledWith("/members/test1/");
5558
});
5659
});
60+
61+
describe("getSocialUserData()", function() {
62+
var mockProfile;
63+
beforeEach(function() {
64+
mockProfile = mockData.getMockAuth0Profile();
65+
});
66+
it("should get JSON for facebook user data ", function() {
67+
mockProfile.identities[0].connection = 'facebook';
68+
var socialData = Helpers.getSocialUserData(mockProfile, "");
69+
expect(socialData).to.exist.not.null;
70+
expect(socialData.socialUserId).to.exist.to.equal('123456');
71+
// TODO cross check population of username for all networks
72+
expect(socialData.username).to.exist.to.equal(mockProfile.first_name + '.' + mockProfile.last_name);
73+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
74+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
75+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
76+
expect(socialData.socialProvider).to.exist.to.equal('facebook');
77+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
78+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
79+
});
80+
81+
it("should get JSON for github user data ", function() {
82+
mockProfile.identities[0].connection = 'github';
83+
var socialData = Helpers.getSocialUserData(mockProfile, "");
84+
expect(socialData).to.exist.not.null;
85+
expect(socialData.socialUserId).to.exist.to.equal('123456');
86+
// TODO cross check population of username for all networks
87+
expect(socialData.username).to.exist.to.equal(mockProfile.nickname);
88+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
89+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
90+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
91+
expect(socialData.socialProvider).to.exist.to.equal('github');
92+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
93+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
94+
});
95+
96+
it("should get JSON for github user data without lastname ", function() {
97+
mockProfile.identities[0].connection = 'github';
98+
mockProfile.name = 'mock';
99+
var socialData = Helpers.getSocialUserData(mockProfile, "");
100+
expect(socialData).to.exist.not.null;
101+
expect(socialData.socialUserId).to.exist.to.equal('123456');
102+
// TODO cross check population of username for all networks
103+
expect(socialData.username).to.exist.to.equal(mockProfile.nickname);
104+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
105+
expect(socialData.lastname).to.exist.to.equal('');
106+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
107+
expect(socialData.socialProvider).to.exist.to.equal('github');
108+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
109+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
110+
});
111+
112+
it("should get JSON for bitbucket user data ", function() {
113+
mockProfile.identities[0].connection = 'bitbucket';
114+
var socialData = Helpers.getSocialUserData(mockProfile, "");
115+
expect(socialData).to.exist.not.null;
116+
expect(socialData.socialUserId).to.exist.to.equal('123456');
117+
// TODO cross check population of username for all networks
118+
expect(socialData.username).to.exist.to.equal(mockProfile.username);
119+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
120+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
121+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
122+
expect(socialData.socialProvider).to.exist.to.equal('bitbucket');
123+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
124+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
125+
});
126+
127+
it("should get JSON for stackoverflow user data ", function() {
128+
mockProfile.identities[0].connection = 'stackoverflow';
129+
var socialData = Helpers.getSocialUserData(mockProfile, "");
130+
expect(socialData).to.exist.not.null;
131+
expect(socialData.socialUserId).to.exist.to.equal('123456');
132+
// TODO cross check population of username for all networks
133+
expect(socialData.username).to.exist.to.equal('123456');
134+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
135+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
136+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
137+
expect(socialData.socialProvider).to.exist.to.equal('stackoverflow');
138+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
139+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
140+
});
141+
142+
it("should get JSON for dribbble user data ", function() {
143+
mockProfile.identities[0].connection = 'dribbble';
144+
var socialData = Helpers.getSocialUserData(mockProfile, "");
145+
expect(socialData).to.exist.not.null;
146+
expect(socialData.socialUserId).to.exist.to.equal('123456');
147+
// TODO cross check population of username for all networks
148+
expect(socialData.username).to.exist.to.equal('123456');
149+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
150+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
151+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
152+
expect(socialData.socialProvider).to.exist.to.equal('dribbble');
153+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
154+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
155+
});
156+
157+
it("should get JSON for twitter user data ", function() {
158+
mockProfile.identities[0].connection = 'twitter';
159+
mockProfile.screen_name = mockProfile.username;
160+
var socialData = Helpers.getSocialUserData(mockProfile, "");
161+
expect(socialData).to.exist.not.null;
162+
expect(socialData.socialUserId).to.exist.to.equal('123456');
163+
// TODO cross check population of username for all networks
164+
expect(socialData.username).to.exist.to.equal(mockProfile.username);
165+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
166+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
167+
// Twitter does not give email
168+
expect(socialData.email).to.exist.to.equal('');
169+
expect(socialData.socialProvider).to.exist.to.equal('twitter');
170+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
171+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
172+
});
173+
174+
it("should get JSON for twitter user data without lastname ", function() {
175+
mockProfile.identities[0].connection = 'twitter';
176+
mockProfile.name = 'mock';
177+
mockProfile.screen_name = mockProfile.username;
178+
var socialData = Helpers.getSocialUserData(mockProfile, "");
179+
expect(socialData).to.exist.not.null;
180+
expect(socialData.socialUserId).to.exist.to.equal('123456');
181+
// TODO cross check population of username for all networks
182+
expect(socialData.username).to.exist.to.equal(mockProfile.username);
183+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
184+
expect(socialData.lastname).to.exist.to.equal('');
185+
// Twitter does not give email
186+
expect(socialData.email).to.exist.to.equal('');
187+
expect(socialData.socialProvider).to.exist.to.equal('twitter');
188+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
189+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
190+
});
191+
192+
it("should get JSON for google-oauth2 user data ", function() {
193+
mockProfile.identities[0].connection = 'google-oauth2';
194+
var socialData = Helpers.getSocialUserData(mockProfile, "");
195+
expect(socialData).to.exist.not.null;
196+
expect(socialData.socialUserId).to.exist.to.equal('123456');
197+
// TODO cross check population of username for all networks
198+
expect(socialData.username).to.exist.to.equal(mockProfile.nickname);
199+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
200+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
201+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
202+
expect(socialData.socialProvider).to.exist.to.equal('google-oauth2');
203+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
204+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
205+
});
206+
});
207+
208+
describe("getPageTitle()", function() {
209+
210+
it("should get page title from state ", function() {
211+
var state = { data: {title: 'Mock Page'}};
212+
var title = Helpers.getPageTitle(state, null);
213+
expect(title).to.exist.to.equal('Mock Page | TopCoder');
214+
});
215+
216+
it("should get default page title when state does not have page title ", function() {
217+
var state = {};
218+
var title = Helpers.getPageTitle(state, null);
219+
expect(title).to.exist.to.equal('TopCoder');
220+
});
221+
222+
it("should get page title from state with dynamic data ", function() {
223+
var state = { data: {title: 'Mock Page {{a.b.c}}'}};
224+
var title = Helpers.getPageTitle(state, {locals : {resolve: {$$values : {a: {b : {c: 'Title'}}}}}});
225+
expect(title).to.exist.to.equal('Mock Page Title | TopCoder');
226+
});
227+
228+
it("should get static page title from state with unknown expression for dynamic data ", function() {
229+
var state = { data: {title: 'Mock Page {a.b.c}'}};
230+
var title = Helpers.getPageTitle(state, {locals : {resolve: {$$values : {a: {b : {c: 'Title'}}}}}});
231+
expect(title).to.exist.to.equal('Mock Page {a.b.c} | TopCoder');
232+
});
233+
234+
it("should replace dynamic data with empty value when not available in current state ", function() {
235+
var state = { data: {title: 'Mock Page {{a.b.c}}'}};
236+
var title = Helpers.getPageTitle(state, {locals : {resolve: {$$values : {a: {b : {}}}}}});
237+
expect(title).to.exist.to.equal('Mock Page | TopCoder');
238+
});
239+
});
240+
241+
describe("getParameterByName()", function() {
242+
243+
it("should get params from the URL ", function() {
244+
var url = "https://topcoder.com/challenges?paramA=123&paramB=234";
245+
var paramA = Helpers.getParameterByName('paramA', url);
246+
var paramB = Helpers.getParameterByName('paramB', url);
247+
expect(paramA).to.exist.to.equal('123');
248+
expect(paramB).to.exist.to.equal('234');
249+
});
250+
251+
it("should get params with [ or ] in from the URL ", function() {
252+
var url = "https://topcoder.com/challenges?[paramA]=123&[paramB]=234";
253+
var paramA = Helpers.getParameterByName('[paramA]', url);
254+
var paramB = Helpers.getParameterByName('[paramB]', url);
255+
expect(paramA).to.exist.to.equal('123');
256+
expect(paramB).to.exist.to.equal('234');
257+
});
258+
259+
it("should get empty value for non existing param from the URL ", function() {
260+
var url = "https://topcoder.com/challenges?paramA=123&paramB=234";
261+
var paramA = Helpers.getParameterByName('paramC', url);
262+
expect(paramA).to.exist.to.equal('');
263+
});
264+
});
265+
266+
describe("peerReview module helpers ", function() {
267+
it("should store objects by id ", function() {
268+
var obj = {};
269+
var questions = [{id:1}, {id: 'bcd'}, {id: 'cde'}];
270+
Helpers.storeById(obj, questions);
271+
expect(obj['1']).to.exist;
272+
expect(obj.bcd).to.exist;
273+
expect(obj.cde).to.exist;
274+
});
275+
276+
it("should parse questions ", function() {
277+
var questions = [
278+
{id: 1, questionTypeId: 5, guideline: 'some guideline'},
279+
{id: 'bcd', questionTypeId: 4, guideline: 'some guideline\nsecond line'},
280+
{id: 'cde', questionTypeId: 5, guideline: 'some guideline\nsecond line\nthird line'}
281+
];
282+
Helpers.parseQuestions(questions);
283+
expect(questions[0].guidelines).to.exist.to.have.length(1);
284+
expect(questions[1].guidelines).not.to.exist;
285+
expect(questions[2].guidelines).to.exist.to.have.length(3);
286+
});
287+
288+
it("should parse answers ", function() {
289+
var questions = {
290+
q1 : {id: 'q1', questionTypeId: 5, guideline: 'some guideline'},
291+
q2 : {id: 'q2', questionTypeId: 5, guideline: 'some guideline\nsecond line'},
292+
q3 : {id: 'q3', questionTypeId: 5, guideline: 'some guideline\nsecond line\nthird line'}
293+
};
294+
var answers = [
295+
{id: 'a1', scorecardQuestionId: 'q1', answer: 3, comments:[ {content: 'perfect'}]},
296+
{id: 'a2', scorecardQuestionId: 'q2', answer: 1, comments:[]},
297+
{id: 'a3', scorecardQuestionId: 'q3', answer: 2, comments:[ {content: 'good'}]}
298+
];
299+
Helpers.parseAnswers(questions, answers);
300+
// validate q1
301+
expect(questions.q1.answer).to.exist.to.equal(3);
302+
expect(questions.q1.reviewItemId).to.exist.to.equal('a1');
303+
expect(questions.q1.comment).to.exist.to.equal('perfect');
304+
// validate q2
305+
expect(questions.q2.answer).to.exist.to.equal(1);
306+
expect(questions.q2.reviewItemId).to.exist.to.equal('a2');
307+
expect(questions.q2.comment).not.to.exist;
308+
// validate q3
309+
expect(questions.q3.answer).to.exist.to.equal(2);
310+
expect(questions.q3.reviewItemId).to.exist.to.equal('a3');
311+
expect(questions.q3.comment).to.exist.to.equal('good');
312+
});
313+
314+
it("should compile review items for first time creation ", function() {
315+
var questions = {
316+
'1' : {id: '1', questionTypeId: 5, guideline: 'some guideline'},
317+
'2' : {id: '2', questionTypeId: 5, guideline: 'some guideline\nsecond line'},
318+
'3' : {id: '3', questionTypeId: 5, guideline: 'some guideline\nsecond line\nthird line'}
319+
};
320+
var answers = [
321+
{id: 'a1', scorecardQuestionId: '1', answer: 3, comments:[ {content: 'perfect'}]},
322+
{id: 'a2', scorecardQuestionId: '2', answer: 1, comments:[]},
323+
{id: 'a3', scorecardQuestionId: '3', answer: 2, comments:[ {content: 'good'}]}
324+
];
325+
// assumes parseAnswers to be working as expected
326+
Helpers.parseAnswers(questions, answers);
327+
328+
var review = {id: 'rev1', resourceId: 'res1', uploadId: 'u1'};
329+
var reviewItems = Helpers.compileReviewItems(questions, review, false);
330+
expect(reviewItems).to.exist.to.have.length(3);
331+
expect(reviewItems[0].reviewId).to.exist.to.equal(review.id);
332+
expect(reviewItems[0].uploadId).to.exist.to.equal(review.uploadId);
333+
// expect(reviewItems[0].id).to.exist.to.equal(answers[0].id);
334+
expect(reviewItems[0].answer).to.exist.to.equal(answers[0].answer.toString());
335+
expect(reviewItems[0].scorecardQuestionId).to.exist.to.equal(parseInt(answers[0].scorecardQuestionId));
336+
expect(reviewItems[0].comments).to.exist.to.have.length(answers[0].comments.length);
337+
});
338+
339+
it("should compile review items for updating existing review items ", function() {
340+
var questions = {
341+
'1' : {id: '1', questionTypeId: 5, guideline: 'some guideline', reviewItemId: 'revItem1'},
342+
'2' : {id: '2', questionTypeId: 5, guideline: 'some guideline\nsecond line', reviewItemId: 'revItem2'},
343+
'3' : {id: '3', questionTypeId: 5, guideline: 'some guideline\nsecond line\nthird line', reviewItemId: 'revItem3'}
344+
};
345+
var answers = [
346+
{id: 'a1', scorecardQuestionId: '1', answer: 3, comments:[ {content: 'perfect'}]},
347+
{id: 'a2', scorecardQuestionId: '2', answer: 1, comments:[]},
348+
{id: 'a3', scorecardQuestionId: '3', answer: 2, comments:[ {content: 'good'}]}
349+
];
350+
// assumes parseAnswers to be working as expected
351+
Helpers.parseAnswers(questions, answers);
352+
353+
var review = {id: 'rev1', resourceId: 'res1', uploadId: 'u1'};
354+
var reviewItems = Helpers.compileReviewItems(questions, review, true);
355+
expect(reviewItems).to.exist.to.have.length(3);
356+
expect(reviewItems[0].uploadId).to.exist.to.equal(review.uploadId);
357+
expect(reviewItems[0].id).to.exist.to.equal(answers[0].id);
358+
expect(reviewItems[0].answer).to.exist.to.equal(answers[0].answer.toString());
359+
expect(reviewItems[0].scorecardQuestionId).to.exist.to.equal(parseInt(answers[0].scorecardQuestionId));
360+
expect(reviewItems[0].comments).to.exist.to.have.length(answers[0].comments.length);
361+
});
362+
});
363+
364+
describe("npad ", function() {
365+
it("should pad string with 0 ", function() {
366+
var padded = Helpers.npad("123", 5);
367+
expect(padded).to.exist.to.equal('00123');
368+
});
369+
370+
it("should pad number with 0 ", function() {
371+
var padded = Helpers.npad(123, 5);
372+
expect(padded).to.exist.to.equal('00123');
373+
});
374+
375+
it("should not pad string with 0 ", function() {
376+
var padded = Helpers.npad("12345", 5);
377+
expect(padded).to.exist.to.equal('12345');
378+
});
379+
});
380+
381+
describe("setupLoginEventMetrics ", function() {
382+
it("should add object with identify key ", function() {
383+
$window._kmq = [];
384+
Helpers.setupLoginEventMetrics('mockuser');
385+
expect($window._kmq).to.have.length(1);
386+
expect($window._kmq[0][0]).to.exist.to.equal('identify');
387+
expect($window._kmq[0][1]).to.exist.to.equal('mockuser');
388+
});
389+
});
390+
391+
xdescribe("getCountyObjFromIP ", function() {
392+
it("should get valid country object ", function() {
393+
var mockLocation = {
394+
"ip": "123.63.151.213",
395+
"hostname": "No Hostname",
396+
"city": "New Delhi",
397+
"region": "National Capital Territory of Delhi",
398+
"country": "IN",
399+
"loc": "28.6000,77.2000",
400+
"org": "Mock Organization"
401+
};
402+
403+
$httpBackend
404+
.when('GET', 'http://ipinfo.io')
405+
.respond(200, mockLocation);
406+
407+
$rootScope.$apply();
408+
console.log(Helpers.getCountyObjFromIP().then(function(data) {
409+
console.log(data);
410+
}));
411+
$rootScope.$apply();
412+
});
413+
});
57414
});

0 commit comments

Comments
 (0)
This repository has been archived.