Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quiz-score-bug-fix #72

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions src/app/components/quiz.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,29 @@ describe('MultipleChoiceQuiz', () => {
const choice2 = getByLabelText('Question 1 Choice 2')
assertIsBefore(choice2, choice1)
})

it('should score of playing swapped question set be calculated correctly', async () => {
const questionSet = new QuestionSetBuilderForTest()
.appendQuestion({
mc: new MultipleChoiceBuilder()
.setCorrectChoiceIndex(1)
.appendNonFixedChoice('Question 1 Choice 1')
.appendNonFixedChoice('Question 1 Choice 2 (Correct)')
.build(),
})
.build()

const {
renderResult: { getByText, getByLabelText, findByText },
} = renderMultipleChoicePage({
questionSet: questionSet.newSwappedChoicesQuestionSet(),
})

fireEvent.click(getByLabelText('Question 1 Choice 1'))
fireEvent.click(getByText('Submit'))

expect(await findByText('Your score: 0/1')).toBeVisible()
})
})

function renderMultipleChoicePage({
Expand Down
17 changes: 10 additions & 7 deletions src/app/components/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export class MultipleChoiceQuizUIService {
}
return (
<MultipleChoiceQuiz
questionSet={questionSet}
questionSetName={questionSet.name}
questions={questionSet.getCurrentPlayQuestions()}
onSubmit={() => {
this.questionSetRepo.upsertQuestionSet(
questionSet.newSwappedChoicesQuestionSet(),
Expand All @@ -79,10 +80,12 @@ export class MultipleChoiceQuizUIService {
}

function MultipleChoiceQuiz({
questionSet,
questionSetName,
questions,
onSubmit,
}: {
questionSet: QuestionSet
questionSetName: string
questions: ReadonlyArray<Question>
onSubmit: () => void
}) {
const [score, setScore] = useState<number | null>(null)
Expand All @@ -106,7 +109,7 @@ function MultipleChoiceQuiz({

const calculateScore = () => {
let score = 0
questionSet.questions.forEach((question, questionIndex) => {
questions.forEach((question, questionIndex) => {
if (
question.mc.correctChoiceIndex ===
questionToCheckedChoiceMap.get(questionIndex)
Expand All @@ -119,8 +122,8 @@ function MultipleChoiceQuiz({

return (
<div className="p-4">
<h1 className="text-xl font-semibold mb-6">{questionSet.name}</h1>
{questionSet.getCurrentPlayQuestions().map((question, questionIndex) => (
<h1 className="text-xl font-semibold mb-6">{questionSetName}</h1>
{questions.map((question, questionIndex) => (
<QuestionForm
key={questionIndex}
question={question}
Expand All @@ -143,7 +146,7 @@ function MultipleChoiceQuiz({
{isSubmitted() && (
<div className="mt-4">
<p className="font-bold">
Your score: {score}/{questionSet.questions.length}
Your score: {score}/{questions.length}
</p>
</div>
)}
Expand Down
Loading