Skip to content

Commit

Permalink
fix: ensure that unit is stored and correctly displayed for numerical…
Browse files Browse the repository at this point in the history
… questions (#4123)
  • Loading branch information
sjschlapbach committed May 11, 2024
1 parent 8871e4a commit 907e257
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
Expand Up @@ -486,6 +486,7 @@ function QuestionEditModal({
options: {
hasSampleSolution: values.options?.hasSampleSolution,
accuracy: parseInt(values.options?.accuracy),
unit: values.options?.unit,
restrictions: {
min:
!values.options?.restrictions ||
Expand Down Expand Up @@ -1436,6 +1437,11 @@ function QuestionEditModal({
errors.options.accuracy
}`}</li>
)}
{errors.options && errors.options.unit && (
<li>{`${t('shared.generic.unit')}: ${
errors.options.unit
}`}</li>
)}
{errors.options &&
errors.options.solutionRanges &&
typeof errors.options.solutionRanges === 'string' && (
Expand Down
4 changes: 4 additions & 0 deletions cypress/cypress/e2e/D-questions-workflow.cy.ts
Expand Up @@ -225,13 +225,17 @@ describe('Create questions', () => {
cy.get('[data-cy="insert-question-text"]').click().type(question)
cy.get('[data-cy="set-numerical-minimum"]').click().type('0')
cy.get('[data-cy="set-numerical-maximum"]').click().type('100')
cy.get('[data-cy="set-numerical-unit"]').click().type('%')
cy.get('[data-cy="set-numerical-accuracy"]').click().type('0')
cy.get('[data-cy="save-new-question"]').click({ force: true })

cy.get(`[data-cy="question-item-${questionTitle}"]`).contains(question)
cy.get(`[data-cy="question-item-${questionTitle}"]`).contains(questionTitle)
cy.get(`[data-cy="edit-question-${questionTitle}"]`).click()
cy.get('[data-cy="input-numerical-minimum"]').contains('Min: 0')
cy.get('[data-cy="input-numerical-maximum"]').contains('Max: 100')
cy.get('[data-cy="input-numerical-accuracy"]').contains('Precision: 0')
cy.get('[data-cy="input-numerical-unit"]').contains('%')
})

it('creates a Free Text question', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/graphql/src/services/questions.ts
Expand Up @@ -33,6 +33,7 @@ function processElementOptions(elementType: DB.ElementType, options: any) {
case DB.ElementType.NUMERICAL: {
return {
hasSampleSolution: options?.hasSampleSolution ?? false,
unit: options?.unit ?? undefined,
accuracy: options?.accuracy ?? undefined,
placeholder: options?.placeholder ?? undefined,
restrictions: {
Expand Down
2 changes: 2 additions & 0 deletions packages/prisma/src/data/data/TEST.ts
Expand Up @@ -73,6 +73,8 @@ export const QUESTIONS = [
options: {
hasSampleSolution: true,
hasAnswerFeedbacks: false,
accuracy: 2,
unit: '%',
restrictions: {
min: -10,
max: 100,
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-components/src/NumericalQuestion.tsx
Expand Up @@ -67,7 +67,7 @@ function NumericalQuestion({
setResponse(newValue, valid)
}}
valid={existingResponse ? !!existingResponse : valid}
accuracy={options.accuracy ? options.accuracy : undefined}
accuracy={options.accuracy ?? undefined}
placeholder={options.placeholder ?? undefined}
unit={options.unit ?? undefined}
min={options.restrictions?.min ?? undefined}
Expand Down
Expand Up @@ -71,7 +71,10 @@ export function NUMERICALAnswerOptions({
data={{ cy: `input-numerical-${elementIx + 1}` }}
/>
{unit && (
<div className="flex flex-col items-center justify-center px-4 text-white rounded-r bg-slate-600 min-w-max">
<div
className="flex flex-col items-center justify-center px-4 text-white rounded-r bg-slate-600 min-w-max"
data-cy="input-numerical-unit"
>
{unit}
</div>
)}
Expand Down

0 comments on commit 907e257

Please sign in to comment.