Skip to content

Commit 935b092

Browse files
feat(QuizWidget.stories): add play for all correct answers in a quiz
1 parent 2957a57 commit 935b092

File tree

7 files changed

+114
-7
lines changed

7 files changed

+114
-7
lines changed

.storybook/i18next.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const baseLocales = {
1212
const ns = [
1313
"common",
1414
"glossary",
15+
"learn-quizzes",
1516
"page-about",
1617
"page-index",
1718
"page-learn",
@@ -48,6 +49,7 @@ const resources: Resource = ns.reduce((acc, n) => {
4849

4950
return acc
5051
}, {})
52+
console.log("🚀 ~ constresources:Resource=ns.reduce ~ resources:", resources)
5153

5254
i18n.use(initReactI18next).init({
5355
debug: true,

src/components/Quiz/QuizWidget/QuizButtonGroup.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ export const QuizButtonGroup = () => {
166166
<Translation id="learn-quizzes:try-again" />
167167
</Button>
168168
)}
169-
<Button onClick={handleContinue}>
169+
<Button
170+
onClick={handleContinue}
171+
data-testid={
172+
finishedQuiz ? "see-results-button" : "next-question-button"
173+
}
174+
>
170175
<Translation
171176
id={
172177
finishedQuiz
@@ -183,6 +188,7 @@ export const QuizButtonGroup = () => {
183188
<Button
184189
onClick={handleSubmitAnswer}
185190
isDisabled={!currentQuestionAnswerChoice}
191+
data-testid="check-answer-button"
186192
>
187193
<Translation id="learn-quizzes:submit-answer" />
188194
</Button>

src/components/Quiz/QuizWidget/QuizContent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ export const QuizContent = ({ children }: QuizContentProps) => {
2323

2424
return (
2525
<VStack spacing="4">
26-
<Text fontWeight="bold" textAlign="center" color={getTitleTextColor()}>
26+
<Text
27+
fontWeight="bold"
28+
textAlign="center"
29+
data-testid={`answer-status-${answerStatus}`}
30+
color={getTitleTextColor()}
31+
>
2732
{getTitleContent()}
2833
</Text>
2934
{children}

src/components/Quiz/QuizWidget/QuizRadioGroup.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ export const QuizRadioGroup = () => {
4343
onChange: handleSelection,
4444
})
4545

46-
const { answers, correctAnswerId, prompt } = questions[currentQuestionIndex]
46+
const {
47+
answers,
48+
correctAnswerId,
49+
prompt,
50+
id: questionId,
51+
} = questions[currentQuestionIndex]
4752

4853
// Memoized values
4954
const explanation = useMemo<TranslationKey>(() => {
@@ -73,7 +78,7 @@ export const QuizRadioGroup = () => {
7378
{t(prompt)}
7479
</Text>
7580

76-
<Stack spacing="4">
81+
<Stack spacing="4" data-testid="question-group" id={questionId}>
7782
{answers.map(({ id, label }, idx) => {
7883
const display =
7984
!answerStatus || id === selectedAnswer ? "inline-flex" : "none"
@@ -140,6 +145,8 @@ const CustomRadio = ({
140145
color: isAnswerVisible ? getAnswerColor() : undefined,
141146
}
142147

148+
const radioInputProps = getInputProps({ id: INPUT_ID })
149+
143150
return (
144151
<>
145152
<chakra.label
@@ -149,6 +156,8 @@ const CustomRadio = ({
149156
w="full"
150157
>
151158
<HStack
159+
data-testid="quiz-question-answer"
160+
id={radioInputProps.value}
152161
{...getRadioProps()}
153162
// Override: `aria-hidden` is marked true in `getRadioProps`
154163
aria-hidden="false"
@@ -181,7 +190,7 @@ const CustomRadio = ({
181190
<span>{label}</span>
182191
</HStack>
183192
</chakra.label>
184-
<input {...getInputProps({ id: INPUT_ID })} />
193+
<input {...radioInputProps} />
185194
</>
186195
)
187196
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import type { Meta, StoryObj } from "@storybook/react"
2+
import { expect, userEvent, waitFor, within } from "@storybook/test"
3+
4+
import questionBank from "@/data/quizzes/questionBank"
5+
6+
import { StandaloneQuizWidget } from "./"
7+
8+
const layer2QuestionBank = Object.entries(questionBank).reduce<
9+
{ id: string; correctAnswer: string }[]
10+
>((arr, curr) => {
11+
if (!curr[0].startsWith("g")) return [...arr]
12+
13+
return [
14+
...arr,
15+
{
16+
id: curr[0],
17+
correctAnswer: curr[1].correctAnswerId,
18+
},
19+
]
20+
}, [])
21+
22+
type QuizWidgetType = typeof StandaloneQuizWidget
23+
24+
const meta = {
25+
title: "QuizWidget",
26+
component: StandaloneQuizWidget,
27+
} satisfies Meta<QuizWidgetType>
28+
29+
export default meta
30+
31+
export const QuizWidgetAllCorrect: StoryObj<typeof meta> = {
32+
args: {
33+
quizKey: "layer-2",
34+
},
35+
render: (args) => <StandaloneQuizWidget {...args} />,
36+
37+
play: async ({ canvasElement, step }) => {
38+
const canvas = within(canvasElement)
39+
40+
const quizWidget = canvas.getByTestId("quiz-widget")
41+
await expect(quizWidget).toBeInTheDocument()
42+
43+
// TODO: would like to add this check to confirm translation is working
44+
// currently broken here, despite it working fine in prod
45+
// await waitFor(() =>
46+
// expect(canvas.getByTestId("answer-status-null")).toHaveTextContent(
47+
// "Layer 2"
48+
// )
49+
// )
50+
51+
await waitFor(() =>
52+
expect(canvas.getByTestId("check-answer-button")).toBeDisabled()
53+
)
54+
55+
await step("Answer all questions correctly", async () => {
56+
for (let i = 0; i < layer2QuestionBank.length; i++) {
57+
const questionGroupId = canvas.getByTestId("question-group").id
58+
const questionAnswers = canvas.getAllByTestId("quiz-question-answer")
59+
const currentQuestionBank = layer2QuestionBank.find(
60+
({ id }) => id === questionGroupId
61+
)!
62+
await userEvent.click(
63+
questionAnswers.find(
64+
(answer) => answer.id === currentQuestionBank.correctAnswer
65+
)!
66+
)
67+
68+
await userEvent.click(canvas.getByTestId("check-answer-button"))
69+
await expect(
70+
canvas.getByTestId("answer-status-correct")
71+
).toBeInTheDocument()
72+
73+
if (i === layer2QuestionBank.length - 1) {
74+
await userEvent.click(canvas.getByTestId("see-results-button"))
75+
} else {
76+
await userEvent.click(canvas.getByTestId("next-question-button"))
77+
}
78+
}
79+
})
80+
81+
await step("Check for successful results page", async () => {
82+
await expect(canvasElement).toHaveTextContent("You passed the quiz!")
83+
})
84+
},
85+
}

src/components/Quiz/QuizWidget/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const QuizWidget = ({
114114
}
115115

116116
return (
117-
<VStack spacing="12" width="full" maxW="600px">
117+
<VStack data-testid="quiz-widget" spacing="12" width="full" maxW="600px">
118118
<Stack
119119
w="full"
120120
gap="8"

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4136,7 +4136,7 @@
41364136
fs-extra "^11.1.0"
41374137
read-pkg-up "^7.0.1"
41384138

4139-
"@storybook/test@^8.0.0":
4139+
"@storybook/[email protected]":
41404140
version "8.0.0"
41414141
resolved "https://registry.yarnpkg.com/@storybook/test/-/test-8.0.0.tgz#645ea453174d89299cc933c1152f8014999ab3b0"
41424142
integrity sha512-am0Pj1wqgsOUpW4RCfZtVGMIF8ddwMCbortOezEKcFuwAaNPE+p62alG+vOVIR0D19fs0XouWJj8rGo3OhzJRA==

0 commit comments

Comments
 (0)