Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 41 additions & 36 deletions lesson_03/quiz/src/lesson3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,57 @@ describe('Lesson3Test', () => {
maybeIt(
'checks multiple choice answers are configured correctly',
async () => {
for (const [providerName, questions] of quizQuestionsByProvider) {
for (const question of questions) {
if (!(question instanceof MultipleChoiceQuizQuestion)) {
continue;
}

// Assert that multiple choice questions have at least one correct answer.
const choices = question.getAnswerChoices();
const areAnswersValid = await Promise.all(
[...choices].map(async (choice) => {
return quizConfig.checkAnswer(
providerName,
question.getQuestionNumber(),
choice,
);
}),
);

expect(areAnswersValid.some((isCorrect) => isCorrect)).toBe(true);
const { providerName, questions } = getQuestionsFromCurrentProvider();
for (const question of questions) {
if (!(question instanceof MultipleChoiceQuizQuestion)) {
continue;
}

// Assert that multiple choice questions have at least one correct answer.
const choices = question.getAnswerChoices();
const areAnswersValid = await Promise.all(
[...choices].map(async (choice) => {
return quizConfig.checkAnswer(
providerName,
question.getQuestionNumber(),
choice,
);
}),
);

expect(areAnswersValid.some((isCorrect) => isCorrect)).toBe(true);
}
},
);

maybeIt('checks for correct answers', async () => {
const { providerName, questions } = getQuestionsFromCurrentProvider();
for (const question of questions) {
const actualAnswer = question.getAnswer();
softExpect(actualAnswer).not.toBe(AnswerChoice.UNANSWERED);
softExpect(
await quizConfig.checkAnswer(
providerName,
question.getQuestionNumber(),
actualAnswer,
),
).toBe(true);
}
});

function getQuestionsFromCurrentProvider(): {
providerName: string;
questions: QuizQuestion[];
} {
const targetProviderName = process.env.PROVIDER_NAME?.trim() || '';

if (!quizQuestionsByProvider.has(targetProviderName)) {
throw new Error(`Unknown provider name: ${targetProviderName}`);
}

for (const [providerName, questions] of quizQuestionsByProvider) {
if (providerName !== process.env.PROVIDER_NAME?.trim()) {
continue;
}
for (const question of questions) {
const actualAnswer = question.getAnswer();
softExpect(actualAnswer).not.toBe(AnswerChoice.UNANSWERED);
softExpect(
await quizConfig.checkAnswer(
providerName,
question.getQuestionNumber(),
actualAnswer,
),
).toBe(true);
}
}
});
return {
providerName: targetProviderName,
questions: quizQuestionsByProvider.get(targetProviderName) || [],
};
}
});