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

Commit ef38f7c

Browse files
author
vikasrohit
committed
Unit tests for compileReviewItems, npad, setupLoginEventMetrics and getCountyObjFromIP methods. Tests for getCountyObjFromIP are not working as expected so they are excluded for now.
1 parent d90c5f9 commit ef38f7c

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
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: 3 additions & 3 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,
@@ -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: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ describe('Helper Service', function() {
2222
sinon.spy(fakeState, "go");
2323

2424
beforeEach(function() {
25-
module('tc.services', function($provide) {
25+
module('topcoder', function($provide) {
2626
$provide.value('$window', fakeWindow);
2727
$provide.value('$state', fakeState);
2828
$provide.value('$location', fakeLocation);
2929
});
3030

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

3434
describe("isEmail()", function() {
@@ -310,5 +310,105 @@ describe('Helper Service', function() {
310310
expect(questions.q3.reviewItemId).to.exist.to.equal('a3');
311311
expect(questions.q3.comment).to.exist.to.equal('good');
312312
});
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+
});
313413
});
314414
});

0 commit comments

Comments
 (0)