-
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.
Label-image: Extract validation out of scoring (#2016)
## 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-2609 ## Test plan: - Confirm checks pass - Confirm widget still works as expected Author: Myranae Reviewers: Myranae, handeyeco, jeremywiebe Required Reviewers: Approved By: handeyeco Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Check builds for changes in size (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), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x) Pull Request URL: #2016
- Loading branch information
Showing
5 changed files
with
66 additions
and
76 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 label-image widget (extracted from label-image 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
29 changes: 29 additions & 0 deletions
29
packages/perseus/src/widgets/label-image/validate-label-image.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,29 @@ | ||
import validateLabelImage from "./validate-label-image"; | ||
|
||
import type {PerseusLabelImageUserInput} from "../../validation.types"; | ||
|
||
describe("scoreLabelImage", () => { | ||
it("should not grade non-interacted widget", function () { | ||
const userInput: PerseusLabelImageUserInput = { | ||
markers: [{label: "England"}, {label: "Germany"}, {label: "Italy"}], | ||
} as const; | ||
|
||
const validationError = validateLabelImage(userInput); | ||
|
||
expect(validationError).toHaveInvalidInput(); | ||
}); | ||
|
||
it("should not grade widget with not all markers answered", function () { | ||
const userInput = { | ||
markers: [ | ||
{label: "England", selected: ["Fiat"]}, | ||
{label: "Germany", selected: ["Lamborghini"]}, | ||
{label: "Italy"}, | ||
], | ||
} as const; | ||
|
||
const validationError = validateLabelImage(userInput); | ||
|
||
expect(validationError).toHaveInvalidInput(); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/perseus/src/widgets/label-image/validate-label-image.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,25 @@ | ||
import type {PerseusScore} from "../../types"; | ||
import type {PerseusLabelImageUserInput} from "../../validation.types"; | ||
|
||
function validateLabelImage( | ||
userInput: PerseusLabelImageUserInput, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
let numAnswered = 0; | ||
for (let i = 0; i < userInput.markers.length; i++) { | ||
const userSelection = userInput.markers[i].selected; | ||
if (userSelection && userSelection.length > 0) { | ||
numAnswered++; | ||
} | ||
} | ||
// We expect all question markers to be answered before grading. | ||
if (numAnswered !== userInput.markers.length) { | ||
return { | ||
type: "invalid", | ||
message: null, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default validateLabelImage; |