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

Make DOJO exercises not nullable and fix React key bug #2336

Merged
merged 2 commits into from
Sep 21, 2022
Merged

Make DOJO exercises not nullable and fix React key bug #2336

merged 2 commits into from
Sep 21, 2022

Conversation

bryanjenningz
Copy link
Collaborator

This pull request doesn't affect any user-facing pages.

This pull request makes the exercises type definition on the backend non-nullable (i.e. [Exercise!]!) which makes the data easier to deal with. This won't cause any problems because we never return any null values in the exercises array.

This pull request also fixes a small React key bug which made it possible to previously press "Show Answer" then "Skip" which would make it go to the next exercise but the next exercise would already be showing the answer. Now if you press "Show Answer" then "Skip" the next answer will not be showing because we added a key={exerciseIndex} property to the Exercise component which causes a rerender when exerciseIndex changes.

We also safely handle the case where there is no exercise explanation (notice the bottom right is empty because this exercise happens to not have an explanation):
empty-explanation

How to test:

  • Create a module at /admin/lessons/js0/modules
  • Create exercises at /curriculum/js0/mentor
  • Go to /exercises/js0 and click on the "SOLVE EXERCISES" button on the right and verify the exercises still work

This pull request is a part of #2253

@vercel
Copy link

vercel bot commented Sep 21, 2022

@bryanjenningz is attempting to deploy a commit to the c0d3-prod Team on Vercel.

A member of the Team first needs to authorize it.

@codecov
Copy link

codecov bot commented Sep 21, 2022

Codecov Report

Merging #2336 (3a6f6a6) into master (27b9d46) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2336   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          181       181           
  Lines         3179      3180    +1     
  Branches       850       851    +1     
=========================================
+ Hits          3179      3180    +1     
Impacted Files Coverage Δ
__dummy__/getExercisesData.ts 100.00% <ø> (ø)
pages/exercises/[lessonSlug].tsx 100.00% <100.00%> (ø)

@@ -66,6 +66,7 @@ const Exercises: React.FC<QueryDataProps<GetExercisesQuery>> = ({
<Layout title={currentLesson.title}>
{exercise ? (
<Exercise
key={exerciseIndex}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line fixes a small React key bug which previously would make it possible to press "Show Answer" (to show the current exercise answer) then "Skip" (to skip to the next exercise) which caused the next exercise's answer to be shown.

challengeName: exercise.module.name,
problem: exercise.description,
answer: exercise.answer,
explanation: exercise.explanation || ''
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exercise explanations are nullable so we have to handle it here by defauting to an empty string, which works fine and looks good (see pull request description for a picture of what it looks like).

fireEvent.click(skipButton)
expect(queryByRole('button', { name: 'PREVIOUS' })).toBeInTheDocument()

skipButton = getByRole('button', { name: 'SKIP' })
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why we need to reassign skipButton to the current skip button in the DOM is because we are using a React key prop which makes it so the Exercise component rerenders.

@@ -87,7 +87,7 @@ const getExercisesData: GetExercisesQuery = {
},
description: '```\nlet a = 1\na -= 2\n// what is a?\n```',
answer: '-1',
explanation: '`a -= 2` is a shorter way to write `a = a - 2`'
explanation: null
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a null exercise explanation to the mock exercise data so we can get full test coverage.

@vercel
Copy link

vercel bot commented Sep 21, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
c0d3-app ✅ Ready (Inspect) Visit Preview Sep 21, 2022 at 11:15AM (UTC)

@bryanjenningz bryanjenningz merged commit c066f24 into garageScript:master Sep 21, 2022
@bryanjenningz bryanjenningz deleted the make-dojo-exercises-not-nullable branch September 21, 2022 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants