-
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.
Sorter: Extract validation out of scoring (#1876)
## Summary: To complete server-side scoring, we are separating out validation logic from scoring logic. This separates that logic for the sorter widget and updates associated tests. Issue: LEMS-2605 ## Test plan: - Confirm all checks pass - Confirm widget still works as expected via Storybook Author: Myranae Reviewers: jeremywiebe, handeyeco, Myranae Required Reviewers: Approved By: jeremywiebe, handeyeco Checks: ✅ Lint, Typecheck, Format, and Test (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), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ gerald, ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1876
- Loading branch information
Showing
5 changed files
with
118 additions
and
12 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 | ||
--- | ||
|
||
Split out validation function for the `sorter` widget. This can be used to check if the user has made any changes to the sorting order, confirming whether or not the question can be scored. |
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
33 changes: 33 additions & 0 deletions
33
packages/perseus/src/widgets/sorter/validate-sorter.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,33 @@ | ||
import validateSorter from "./validate-sorter"; | ||
|
||
import type {PerseusSorterUserInput} from "../../validation.types"; | ||
|
||
describe("validateSorter", () => { | ||
it("is invalid when the user has not made any changes", () => { | ||
// Arrange | ||
const userInput: PerseusSorterUserInput = { | ||
options: ["$15$ grams", "$55$ grams", "$0.005$ kilograms"], | ||
changed: false, | ||
}; | ||
|
||
// Act | ||
const result = validateSorter(userInput); | ||
|
||
// Assert | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("returns null when the user has made any changes", () => { | ||
// Arrange | ||
const userInput: PerseusSorterUserInput = { | ||
options: ["$55$ grams", "$0.005$ kilograms", "$15$ grams"], | ||
changed: true, | ||
}; | ||
|
||
// Act | ||
const result = validateSorter(userInput); | ||
|
||
// Assert | ||
expect(result).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 {PerseusSorterUserInput} from "../../validation.types"; | ||
|
||
/** | ||
* Checks user input for the sorter widget to ensure that the user has made | ||
* changes before attempting to score the widget. | ||
* @param userInput | ||
* @see 'scoreSorter' in 'packages/perseus/src/widgets/sorter/score-sorter.ts' | ||
* for more details on how the sorter widget is scored. | ||
*/ | ||
function validateSorter( | ||
userInput: PerseusSorterUserInput, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
// If the sorter widget hasn't been changed yet, we treat it as "empty" which | ||
// prevents the "Check" button from becoming active. We want the user | ||
// to make a change before trying to move forward. This makes an | ||
// assumption that the initial order isn't the correct order! However, | ||
// this should be rare if it happens, and interacting with the list | ||
// will enable the button, so they won't be locked out of progressing. | ||
if (!userInput.changed) { | ||
return { | ||
type: "invalid", | ||
message: null, | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
export default validateSorter; |