-
Notifications
You must be signed in to change notification settings - Fork 352
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
Dropdown: Extract validation out of scoring #1898
Open
Myranae
wants to merge
5
commits into
main
Choose a base branch
from
tb/LEMS-2597/dropdown-split-out-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−27
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c718484
Add changeset
Myranae 8eef008
Split validation test and add null test
Myranae ef22f34
Split out validation logic to own function
Myranae 1f04130
Merge branch 'main' into tb/LEMS-2597/dropdown-split-out-validation
Myranae d5bb04c
Clarify valid state in doc comment
Myranae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)", () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tiny improvement suggestion as
Suggested change
|
||||||
// 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(); | ||||||
}); | ||||||
}); |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
import type {PerseusScore} from "../../types"; | ||
import type {PerseusDropdownUserInput} from "../../validation.types"; | ||
|
||
/** | ||
* Checks if the user has selected an item from the dropdown before scoring. | ||
* @param userInput | ||
* @see `scoreDropdown` for details on scoring | ||
*/ | ||
function validateDropdown( | ||
userInput: PerseusDropdownUserInput, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
if (userInput.value === 0) { | ||
return { | ||
type: "invalid", | ||
message: null, | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
export default validateDropdown; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't keep a check for this in scoring test since it looks like we might be doing validation before scoring (love that)