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

Commit d90c5f9

Browse files
author
vikasrohit
committed
Unit tests for getParameterByName, storeById, parseQuestions and parseAnswers.
1 parent 36eeffa commit d90c5f9

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

app/services/helpers.service.spec.js

Lines changed: 78 additions & 1 deletion
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");
@@ -25,7 +28,7 @@ describe('Helper Service', function() {
2528
$provide.value('$location', fakeLocation);
2629
});
2730

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

3134
describe("isEmail()", function() {
@@ -234,4 +237,78 @@ describe('Helper Service', function() {
234237
expect(title).to.exist.to.equal('Mock Page | TopCoder');
235238
});
236239
});
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+
});
237314
});

0 commit comments

Comments
 (0)