-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2349 from bryanjenningz/show-next-question-button…
…-after-user-answers-exercise-correctly Show next question button after user answers exercise correctly
- Loading branch information
Showing
6 changed files
with
162 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,133 @@ | ||
import '@testing-library/jest-dom' | ||
import React from 'react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import ExerciseCard from './ExerciseCard' | ||
import ExerciseCard, { Message } from './ExerciseCard' | ||
|
||
describe('ExerciseCard component', () => { | ||
it('Should render an exercise card', () => { | ||
const exampleProblem = `let a = 5 | ||
const exampleProblem = `let a = 5 | ||
a = a + 10 | ||
// what is a?` | ||
const exampleAnswer = '15' | ||
const exampleExplanation = `You can reassign variables that are initialized with "let".` | ||
const exampleAnswer = '15' | ||
const exampleExplanation = `You can reassign variables that are initialized with "let".` | ||
|
||
const { getByRole, queryByText, getByLabelText } = render( | ||
describe('ExerciseCard component', () => { | ||
it('Should render an exercise card', async () => { | ||
const setAnswerShown = jest.fn() | ||
const setMessage = jest.fn() | ||
|
||
const { getByRole, queryByText } = render( | ||
<ExerciseCard | ||
problem={exampleProblem} | ||
answer={exampleAnswer} | ||
explanation={exampleExplanation} | ||
answerShown={false} | ||
setAnswerShown={setAnswerShown} | ||
message={Message.EMPTY} | ||
setMessage={setMessage} | ||
/> | ||
) | ||
|
||
// Test that an error message shows if the user is wrong | ||
|
||
const errorMessage = 'Your answer is incorrect - please try again.' | ||
|
||
expect(queryByText(errorMessage)).not.toBeInTheDocument() | ||
expect(queryByText(Message.ERROR)).not.toBeInTheDocument() | ||
|
||
const submitButton = getByRole('button', { name: 'SUBMIT' }) | ||
fireEvent.click(submitButton) | ||
|
||
expect(queryByText(errorMessage)).toBeInTheDocument() | ||
expect(setAnswerShown).toBeCalledTimes(0) | ||
expect(setMessage).toBeCalledWith(Message.ERROR) | ||
expect(setMessage).toBeCalledTimes(1) | ||
}) | ||
|
||
// Test that a success message shows and the answer is shown if the user is right | ||
it('Should render an error message', () => { | ||
const setAnswerShown = jest.fn() | ||
const setMessage = jest.fn() | ||
|
||
const successMessage = '🎉 Your answer is correct!' | ||
const { getByRole, queryByText, getByLabelText } = render( | ||
<ExerciseCard | ||
problem={exampleProblem} | ||
answer={exampleAnswer} | ||
explanation={exampleExplanation} | ||
answerShown={false} | ||
setAnswerShown={setAnswerShown} | ||
message={Message.ERROR} | ||
setMessage={setMessage} | ||
/> | ||
) | ||
|
||
expect(queryByText(successMessage)).not.toBeInTheDocument() | ||
expect(queryByText(Message.ERROR)).toBeInTheDocument() | ||
expect(queryByText(Message.SUCCESS)).not.toBeInTheDocument() | ||
|
||
const inputBox = getByLabelText('User answer') | ||
fireEvent.change(inputBox, { | ||
target: { value: '15' } | ||
}) | ||
|
||
// Test that the submit button shows the success message and the answer explanation | ||
|
||
const submitButton = getByRole('button', { name: 'SUBMIT' }) | ||
fireEvent.click(submitButton) | ||
|
||
expect(queryByText(successMessage)).toBeInTheDocument() | ||
expect(setAnswerShown).toBeCalledWith(true) | ||
expect(setAnswerShown).toBeCalledTimes(1) | ||
expect(setMessage).toBeCalledWith(Message.SUCCESS) | ||
expect(setMessage).toBeCalledTimes(1) | ||
}) | ||
|
||
it('Should render a success message', () => { | ||
const setAnswerShown = jest.fn() | ||
const setMessage = jest.fn() | ||
|
||
const { getByRole, queryByText } = render( | ||
<ExerciseCard | ||
problem={exampleProblem} | ||
answer={exampleAnswer} | ||
explanation={exampleExplanation} | ||
answerShown={true} | ||
setAnswerShown={setAnswerShown} | ||
message={Message.SUCCESS} | ||
setMessage={setMessage} | ||
/> | ||
) | ||
|
||
expect(queryByText(Message.SUCCESS)).toBeInTheDocument() | ||
expect(queryByText(exampleExplanation)).toBeInTheDocument() | ||
|
||
// Test that the hide button hides the answer explanation | ||
|
||
const hideButton = getByRole('button', { name: 'Hide Answer' }) | ||
fireEvent.click(hideButton) | ||
|
||
expect(setAnswerShown).toBeCalledWith(false) | ||
expect(setAnswerShown).toBeCalledTimes(1) | ||
expect(setMessage).toBeCalledTimes(0) | ||
}) | ||
|
||
it('Should hide the answer', () => { | ||
const setAnswerShown = jest.fn() | ||
const setMessage = jest.fn() | ||
|
||
const { queryByText, getByRole } = render( | ||
<ExerciseCard | ||
problem={exampleProblem} | ||
answer={exampleAnswer} | ||
explanation={exampleExplanation} | ||
answerShown={false} | ||
setAnswerShown={setAnswerShown} | ||
message={Message.SUCCESS} | ||
setMessage={setMessage} | ||
/> | ||
) | ||
|
||
expect(queryByText(Message.SUCCESS)).toBeInTheDocument() | ||
expect(queryByText(exampleExplanation)).not.toBeInTheDocument() | ||
|
||
// Test that the show button shows the answer explanation | ||
|
||
const hideButton = getByRole('button', { name: 'Show Answer' }) | ||
fireEvent.click(hideButton) | ||
|
||
expect(setAnswerShown).toBeCalledWith(true) | ||
expect(setAnswerShown).toBeCalledTimes(1) | ||
expect(setMessage).toBeCalledTimes(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default, type ExerciseCardProps } from './ExerciseCard' | ||
export { default, type ExerciseCardProps, Message } from './ExerciseCard' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5a6e048
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
c0d3-app – ./
c0d3-app-git-master-c0d3-prod.vercel.app
c0d3-app.vercel.app
www.c0d3.com
v2.c0d3.app
c0d3-app-c0d3-prod.vercel.app