-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* modify preview card styles * change color of card when is included * resolve comments - fix style issues * change props in storybook of Exercise * Add prop method for details button * Refactor props of ExercisePreview
- Loading branch information
1 parent
1265cd5
commit 946c936
Showing
14 changed files
with
935 additions
and
82 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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ExercisePreview } from './ExercisePreview'; | ||
import renderer from 'react-test-renderer'; | ||
import { ExerciseData } from 'src/types'; | ||
|
||
describe('ExercisePreview', () => { | ||
describe('using step data', () => { | ||
|
||
let exercise: ExerciseData; | ||
|
||
beforeEach(() => { | ||
exercise = { | ||
uid: '1@1', | ||
uuid: 'e4e27897-4abc-40d3-8565-5def31795edc', | ||
group_uuid: '20e82bf6-232e-40c8-ba68-2d22c6498f69', | ||
number: 1, | ||
version: 1, | ||
published_at: '2022-09-06T20:32:21.981Z', | ||
context: 'Context', | ||
stimulus_html: '<b>Stimulus HTML</b>', | ||
tags: [], | ||
authors: [{ user_id: 1, name: 'OpenStax' }], | ||
copyright_holders: [{ user_id: 1, name: 'OpenStax' }], | ||
derived_from: [], | ||
is_vocab: false, | ||
solutions_are_public: false, | ||
versions: [1], | ||
questions: [{ | ||
id: '1234@5', | ||
collaborator_solutions: [], | ||
formats: ['true-false'], | ||
stimulus_html: '', | ||
stem_html: '', | ||
is_answer_order_important: false, | ||
answers: [{ | ||
id: '1', | ||
correctness: undefined, | ||
content_html: 'True', | ||
}, { | ||
id: '2', | ||
correctness: undefined, | ||
content_html: 'False', | ||
}], | ||
}], | ||
}; | ||
}); | ||
|
||
it.each` | ||
enableOverlay | selected | description | ||
${true} | ${true} | ${'with overlay and selected true'} | ||
${true} | ${true} | ${'with overlay and selected false'} | ||
${false} | ${false} | ${'without overlay'} | ||
`('matches snapshot %description', ({ enableOverlay, selected }: { enableOverlay: boolean, selected: boolean }) => { | ||
const onIncludeMock = jest.fn(); | ||
const onRemoveMock = jest.fn(); | ||
const onDetailsMock = jest.fn(); | ||
const tree = renderer.create( | ||
<ExercisePreview | ||
exercise={exercise} | ||
enableOverlay={enableOverlay} | ||
selected={selected} | ||
onIncludeHandler={onIncludeMock} | ||
onRemoveHandler={onRemoveMock} | ||
onClickDetails={onDetailsMock} | ||
/> | ||
).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React from "react"; | ||
import { ExerciseData, ExerciseQuestionData, StepBase } from "src/types"; | ||
import { IncludeRemoveQuestion } from "./IncludeRemoveQuestion"; | ||
import { Exercise } from "./Exercise"; | ||
|
||
export interface ExercisePreviewProps { | ||
exercise: ExerciseData; | ||
selected: boolean; | ||
onIncludeHandler: () => void; | ||
onRemoveHandler: () => void; | ||
onClickDetails: () => void; | ||
enableOverlay?: boolean; | ||
} | ||
|
||
/** | ||
* An Exercise version with less interaction with card and grants an Overlay with Include/Remove component | ||
*/ | ||
export const ExercisePreview = ( | ||
{ | ||
exercise, | ||
selected, | ||
onIncludeHandler, | ||
onRemoveHandler, | ||
onClickDetails, | ||
enableOverlay = false, | ||
}: ExercisePreviewProps) => { | ||
|
||
const exercisePreviewProps = (exercise: ExerciseData) => { | ||
const formatAnswerData = (questions: ExerciseQuestionData[]) => questions.map((q) => ( | ||
{ id: q.id, correct_answer_id: (q.answers.find((a) => a.correctness === '1.0')?.id || '') })); | ||
|
||
const questionStateFields = { | ||
available_points: '1.0', | ||
is_completed: true, | ||
answer_id: '1', | ||
free_response: '', | ||
feedback_html: '', | ||
correct_answer_feedback_html: '', | ||
attempts_remaining: 0, | ||
attempt_number: 1, | ||
incorrectAnswerId: 0 | ||
} | ||
|
||
const questionStates = formatAnswerData(exercise.questions).reduce((acc, answer) => { | ||
const { id, correct_answer_id } = answer; | ||
return { ...acc, [id]: { ...questionStateFields, correct_answer_id } }; | ||
}, {}); | ||
|
||
const step: StepBase = { | ||
id: 1, | ||
uid: exercise.uid, | ||
available_points: '1.0', | ||
}; | ||
|
||
return { | ||
canAnswer: true, | ||
needsSaved: true, | ||
hasMultipleAttempts: false, | ||
onAnswerChange: () => undefined, | ||
onAnswerSave: () => undefined, | ||
onNextStep: () => undefined, | ||
apiIsPending: false, | ||
canUpdateCurrentStep: false, | ||
step: step, | ||
questionNumber: exercise.number as number, | ||
numberOfQuestions: exercise.questions.length, | ||
questionStates: questionStates, | ||
show_all_feedback: false, // Hide all feedback | ||
}; | ||
}; | ||
|
||
const includeRemoveQuestionComponent = React.useMemo(() => | ||
<IncludeRemoveQuestion | ||
buttonVariant={selected ? 'remove' : 'include'} | ||
onIncludeHandler={onIncludeHandler} | ||
onRemoveHandler={onRemoveHandler} | ||
onClickDetails={onClickDetails} | ||
/> | ||
, [selected, onIncludeHandler, onRemoveHandler, onClickDetails]); | ||
|
||
return ( | ||
<Exercise | ||
exercise={exercise} | ||
className={selected ? 'preview-card is-selected' : 'preview-card'} | ||
{ | ||
...(enableOverlay | ||
? { | ||
overlayChildren: includeRemoveQuestionComponent, | ||
} | ||
: {}) | ||
} | ||
{...exercisePreviewProps(exercise)} | ||
/> | ||
); | ||
}; |
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
Oops, something went wrong.