Skip to content

Commit

Permalink
updated quiz controller and added test cases for quiz controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiti committed Dec 28, 2012
1 parent cb43173 commit fe29a5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/js/controllers/quizCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ quizApp.controller('QuizCtrl', function QuizCtrl($rootScope, $scope, $resource,
});

$scope.hasNext = function () {
return ($scope.currentPosition >= $scope.quiz.questionnaire.length - 1);
return !($scope.currentPosition >= $scope.quiz.questionnaire.length - 1);
};

$scope.updatePage = function () {
Expand Down Expand Up @@ -49,7 +49,7 @@ quizApp.controller('QuizCtrl', function QuizCtrl($rootScope, $scope, $resource,

$scope.next = function () {
var valid = $scope.hasNext();
if (valid !== true) {
if (valid === true) {
$scope.currentResponse = "";
$scope.updatePage();
$rootScope.$broadcast('restart_timer');
Expand Down
28 changes: 26 additions & 2 deletions tests/unit/ControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ describe('App controllers', function () {

it('should be valid name', function () {
scope.userName = "testUser";
expect(scope.isValidUserName).toBeTruthy();
expect(scope.isValidUserName()).toBeTruthy();
});

it('should ignore all spaces as name', function () {
scope.userName = " ";
expect(scope.isValidUserName).toBeFalsy();
expect(scope.isValidUserName()).toBeFalsy();
});

});
Expand Down Expand Up @@ -62,6 +62,30 @@ describe('App controllers', function () {
expect(scope.quiz.questionnaire.length).toBe(2);
});

it('should be initialized correctly', function () {
$httpMock.flush();
expect(scope.currentPosition).toBe(0);
expect(scope.currentQuestion).toBeDefined();
expect(scope.hasNext()).toBeTruthy();
expect(scope.isAnswered()).toBeFalsy();
});

it('should add to the score for correct answer', function () {
$httpMock.flush();
scope.currentResponse = "false";
expect(scope.user.score).toBe(0);
scope.submitAns(scope.currentQuestion.id);
expect(scope.user.score).toBe(2);
});

it('should not add to the score for wrong answer', function () {
$httpMock.flush();
scope.currentResponse = "true";
expect(scope.user.score).toBe(0);
scope.submitAns(scope.currentQuestion.id);
expect(scope.user.score).toBe(0);
});

});

});
Expand Down

0 comments on commit fe29a5b

Please sign in to comment.