Skip to content

Commit 12bf813

Browse files
authored
Merge pull request #24 from ironhack-labs/uros/feat/tests-check-answer-validation
Add a test to validate if answer is checked against the current question answer
2 parents 832c374 + d06b2ea commit 12bf813

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/quiz.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ describe("Quiz", () => {
194194
// Check if the correctAnswers property has been increased by 1
195195
expect(quiz.correctAnswers).toEqual(3);
196196
});
197+
198+
it("should check if the answer is correct by comparing it to the 'answer' property of the current question", () => {
199+
const testQuestions = [
200+
{ text: "Question 1", choices: ["a", "b", "c"], answer: "b" },
201+
{ text: "Question 2", choices: ["d", "e", "f"], answer: "f" },
202+
{ text: "Question 3", choices: ["x", "y", "z"], answer: "y" },
203+
];
204+
205+
// Get a random index from the test questions array
206+
const randomQuestionIndex = Math.floor(Math.random() * testQuestions.length);
207+
208+
// Instantiate a new Quiz object with the test questions
209+
const quiz = new Quiz(testQuestions, 60, 60);
210+
const initialCorrectAnswers = quiz.correctAnswers;
211+
212+
// Manualy set the `quiz.currentQuestionIndex` to the random index
213+
quiz.currentQuestionIndex = randomQuestionIndex;
214+
215+
// Call the checkAnswer() method with the correct answer for the current question
216+
quiz.checkAnswer(testQuestions[randomQuestionIndex].answer);
217+
218+
// Check if the correctAnswers property has been increased by 1
219+
expect(quiz.correctAnswers).toEqual(initialCorrectAnswers + 1);
220+
});
197221
});
198222

199223
describe("hasEnded() method", () => {

0 commit comments

Comments
 (0)