-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Radio: Extract validation out of scoring (#1902)
## Summary: To complete server-side scoring, we are separating out validation logic from scoring logic. This PR separates validation logic that does not depend on answer information and also separates associated tests for the radio widget. Issue: LEMS-2594 ## Test plan: - Confirm checks pass - Confirm widget still works as expected - Confirm the validation logic remaining in the scoring function cannot be removed from scoring Author: Myranae Reviewers: Myranae, jeremywiebe, handeyeco Required Reviewers: Approved By: jeremywiebe Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ✅ gerald, ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1902
- Loading branch information
Showing
5 changed files
with
82 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": minor | ||
--- | ||
|
||
Introduces a validation function for the radio widget (extracted from the scoring function), though not all validation logic can be extracted. |
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,25 @@ | ||
import validateRadio from "./validate-radio"; | ||
|
||
import type {PerseusRadioUserInput} from "../../validation.types"; | ||
|
||
describe("validateRadio", () => { | ||
it("is invalid when no options are selected", () => { | ||
const userInput: PerseusRadioUserInput = { | ||
choicesSelected: [false, false, false, false], | ||
}; | ||
|
||
const validationError = validateRadio(userInput); | ||
|
||
expect(validationError).toHaveInvalidInput(); | ||
}); | ||
|
||
it("returns null when validation passes", () => { | ||
const userInput: PerseusRadioUserInput = { | ||
choicesSelected: [true, false, false, false], | ||
}; | ||
|
||
const validationError = validateRadio(userInput); | ||
|
||
expect(validationError).toBeNull(); | ||
}); | ||
}); |
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,29 @@ | ||
import type {PerseusScore} from "../../types"; | ||
import type {PerseusRadioUserInput} from "../../validation.types"; | ||
|
||
/** | ||
* Checks if the user has selected at least one option. Additional validation | ||
* is done in scoreRadio to check if the number of selected options is correct | ||
* and if the user has selected both a correct option and the "none of the above" | ||
* option. | ||
* @param userInput | ||
* @see `scoreRadio` for the additional validation logic and the scoring logic. | ||
*/ | ||
function validateRadio( | ||
userInput: PerseusRadioUserInput, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
const numSelected = userInput.choicesSelected.reduce((sum, selected) => { | ||
return sum + (selected ? 1 : 0); | ||
}, 0); | ||
|
||
if (numSelected === 0) { | ||
return { | ||
type: "invalid", | ||
message: null, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default validateRadio; |