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

student feature: Add the ability to report exercises #2600

Merged
merged 6 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions __tests__/__snapshots__/storyshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6490,6 +6490,21 @@ a = a + 10
</div>
</div>
</div>
<div
className=""
>
<button
className="btn newButton container__reportBtn borderless"
onClick={[Function]}
style={
Object {
"color": "mute",
}
}
>
Report a problem
</button>
</div>
</section>
`;

Expand Down
93 changes: 53 additions & 40 deletions components/ExerciseCard/ExerciseCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '@testing-library/jest-dom'
import React from 'react'
import { fireEvent, render } from '@testing-library/react'
import ExerciseCard, { Message } from './ExerciseCard'
import { MockedProvider } from '@apollo/client/testing'

const exampleProblem = `let a = 5
a = a + 10
Expand All @@ -16,16 +17,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { getByRole, queryByText } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.EMPTY}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.EMPTY}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
exerciseId={1}
/>
</MockedProvider>
)

// Test that an error message shows if the user is wrong
Expand All @@ -48,16 +52,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { getByRole, queryByText, getByLabelText } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.ERROR}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.ERROR}
setMessage={setMessage}
exerciseId={1}
submitUserAnswer={submitUserAnswer}
/>
</MockedProvider>
)

expect(queryByText(Message.ERROR)).toBeInTheDocument()
Expand Down Expand Up @@ -87,16 +94,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { getByRole, queryByText } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={true}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
exerciseId={1}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={true}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
</MockedProvider>
)

expect(queryByText(Message.SUCCESS)).toBeInTheDocument()
Expand All @@ -119,16 +129,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { queryByText, getByRole } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
exerciseId={1}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
</MockedProvider>
)

expect(queryByText(Message.SUCCESS)).toBeInTheDocument()
Expand Down
6 changes: 5 additions & 1 deletion components/ExerciseCard/ExerciseCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Markdown from 'markdown-to-jsx'
import React, { useState } from 'react'
import ExerciseReportCard from '../ExerciseReportCard'
import { NewButton } from '../theme/Button'
import { Text } from '../theme/Text'
import styles from './exerciseCard.module.scss'
Expand All @@ -13,6 +14,7 @@ export type ExerciseCardProps = {
message: Message
setMessage: (message: Message) => void
submitUserAnswer: (userAnswer: string) => void
exerciseId: number
}

export enum Message {
Expand All @@ -29,7 +31,8 @@ const ExerciseCard = ({
setAnswerShown,
message,
setMessage,
submitUserAnswer
submitUserAnswer,
exerciseId
}: ExerciseCardProps) => {
const [studentAnswer, setStudentAnswer] = useState('')

Expand Down Expand Up @@ -102,6 +105,7 @@ const ExerciseCard = ({
</div>
</>
)}
<ExerciseReportCard exerciseId={exerciseId} answerShown={answerShown} />
</section>
)
}
Expand Down
1 change: 1 addition & 0 deletions pages/exercises/[lessonSlug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const Exercise = ({
})
submitUserAnswer(exercise.id, userAnswer)
}}
exerciseId={exercise.id}
/>
<div className="d-flex justify-content-between mt-4">
{hasPrevious ? (
Expand Down
28 changes: 16 additions & 12 deletions stories/components/ExerciseCard.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MockedProvider } from '@apollo/client/testing'
import React, { useState } from 'react'
import ExerciseCard, { Message } from '../../components/ExerciseCard'

Expand All @@ -19,17 +20,20 @@ export const Basic = () => {
const [message, setMessage] = useState(Message.EMPTY)

return (
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={answerShown}
setAnswerShown={setAnswerShown}
message={message}
setMessage={setMessage}
submitUserAnswer={userAnswer => {
console.log(`User answer submitted: ${userAnswer}`)
}}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={answerShown}
setAnswerShown={setAnswerShown}
message={message}
setMessage={setMessage}
submitUserAnswer={userAnswer => {
console.log(`User answer submitted: ${userAnswer}`)
}}
exerciseId={1}
/>
</MockedProvider>
)
}