-
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
Sorter: Extract validation out of scoring #1876
Open
Myranae
wants to merge
11
commits into
main
Choose a base branch
from
tb/LEMS-2605/sorter-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.
+118
−12
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2420ea3
Split out validation logic to new function
Myranae a46486c
Update tests
Myranae a9351e9
Add changeset
Myranae e602bfa
Confirm scoring handles validation responses
Myranae 9e8c6c7
Remove rubric from validation as its not needed
Myranae 0837d70
Update with expected input
Myranae 71e4a6f
Rearrange test for PR clarity
Myranae 7fe7859
Update value
Myranae 9a323dd
Keep vocab consistent
Myranae 5327ece
Add arrange, act, assert comments
Myranae 61de24e
Add validation function 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 | ||
--- | ||
|
||
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
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
import validateSorter from "./validate-sorter"; | ||
|
||
import type { | ||
PerseusSorterRubric, | ||
PerseusSorterUserInput, | ||
} from "../../validation.types"; | ||
|
||
describe("validateSorter", () => { | ||
it("is invalid when the user has not made any changes", () => { | ||
const userInput: PerseusSorterUserInput = { | ||
options: ["$15$ grams", "$55$ grams", "$0.005$ kilograms"], | ||
changed: false, | ||
}; | ||
const rubric: PerseusSorterRubric = { | ||
correct: ["$0.005$ kilograms", "$15$ grams", "$55$ grams"], | ||
}; | ||
const result = validateSorter(userInput, rubric); | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("returns null when the user has made any changes", () => { | ||
const userInput: PerseusSorterUserInput = { | ||
options: ["$55$ grams", "$0.005$ kilograms", "$15$ grams"], | ||
changed: true, | ||
}; | ||
const rubric: PerseusSorterRubric = { | ||
correct: ["$0.005$ kilograms", "$15$ grams", "$55$ grams"], | ||
}; | ||
const result = validateSorter(userInput, rubric); | ||
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,26 @@ | ||
import type {PerseusScore} from "../../types"; | ||
import type { | ||
PerseusSorterRubric, | ||
PerseusSorterUserInput, | ||
} from "../../validation.types"; | ||
|
||
function validateSorter( | ||
userInput: PerseusSorterUserInput, | ||
rubric: PerseusSorterRubric, | ||
): 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; |
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.
I think we could keep a variant of this test in each scorer's test suite. But, we can use mocking to ensure that
validate
is called first... something like:I think we could keep a variant of this test in each scorer's test suite. But, we can use mocking to ensure that
validate
is called first... something like: