-
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
base: main
Are you sure you want to change the base?
Changes from all commits
c718484
8eef008
ef22f34
1f04130
d5bb04c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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). |
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(); | ||||||
}); | ||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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. | ||
* @param userInput | ||
Comment on lines
+6
to
+7
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. I would suggest removing these two lines: The first one documents the internal implementation of the function (how it checks validation) which you can see by reading the code. It also presents something that can easily get out of sync with the code. A great guide for comments that I was told one time was "document why, not what". :) The |
||
* @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; |
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)