-
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.
Dropdown: Extract validation out of scoring (#1898)
## Summary: To complete server-side scoring, we are separating out validation logic from scoring logic. This PR separates that logic and updates associated tests. Issue: LEMS-2597 ## Test plan: - Confirm all checks pass - Confirm widget still works as expected Author: Myranae Reviewers: Myranae, jeremywiebe, handeyeco Required Reviewers: Approved By: jeremywiebe Checks: ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1898
- Loading branch information
Showing
5 changed files
with
66 additions
and
27 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 dropdown widget (extracted from dropdown scoring function). |
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
31 changes: 31 additions & 0 deletions
31
packages/perseus/src/widgets/dropdown/validate-dropdown.test.ts
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,31 @@ | ||
import validateDropdown from "./validate-dropdown"; | ||
|
||
import type {PerseusDropdownUserInput} from "../../validation.types"; | ||
|
||
describe("validateDropdown", () => { | ||
it("returns invalid for invalid input (user input of 0)", () => { | ||
// Arrange | ||
const userInput: PerseusDropdownUserInput = { | ||
value: 0, | ||
}; | ||
|
||
// Act | ||
const validationError = validateDropdown(userInput); | ||
|
||
// Assert | ||
expect(validationError).toHaveInvalidInput(); | ||
}); | ||
|
||
it("returns null for a valid answer (user input that is not 0)", () => { | ||
// Arrange | ||
const userInput: PerseusDropdownUserInput = { | ||
value: 2, | ||
}; | ||
|
||
// Act | ||
const validationError = validateDropdown(userInput); | ||
|
||
// Assert | ||
expect(validationError).toBeNull(); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/perseus/src/widgets/dropdown/validate-dropdown.ts
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,20 @@ | ||
import type {PerseusScore} from "../../types"; | ||
import type {PerseusDropdownUserInput} from "../../validation.types"; | ||
|
||
/** | ||
* Checks if the user has selected an item from the dropdown before scoring. | ||
* This is shown with a userInput value / index other than 0. | ||
*/ | ||
function validateDropdown( | ||
userInput: PerseusDropdownUserInput, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
if (userInput.value === 0) { | ||
return { | ||
type: "invalid", | ||
message: null, | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
export default validateDropdown; |