-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Matrix: Extract validation from scorer (#1883)
## Summary: This PR extracts validation from the `matrix`'s scorer function. In reality, it's an empty function, but it follows our conventions for having the scorer call a validator first. I've created the standard tests to ensure that scorer calls the validator. Issue: LEMS-2602 ## Test plan: `yarn test` `yarn typecheck` Author: jeremywiebe Reviewers: Myranae, handeyeco, jeremywiebe Required Reviewers: Approved By: Myranae, handeyeco Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (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), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1883
- Loading branch information
1 parent
55a5321
commit adad642
Showing
6 changed files
with
176 additions
and
20 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 matrix widget (extracted from matrix 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
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
50 changes: 50 additions & 0 deletions
50
packages/perseus/src/widgets/matrix/validate-matrix.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,50 @@ | ||
import {mockStrings} from "../../strings"; | ||
|
||
import validateMatrix from "./validate-matrix"; | ||
|
||
import type {PerseusMatrixUserInput} from "../../validation.types"; | ||
|
||
describe("matrixValidator", () => { | ||
it("should return invalid when answers is completely empty", () => { | ||
// Arrange | ||
const userInput: PerseusMatrixUserInput = { | ||
answers: [[]], | ||
}; | ||
|
||
// Act | ||
const result = validateMatrix(userInput, {}, mockStrings); | ||
|
||
// Assert | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("should return invalid when any answer row is empty", () => { | ||
// Arrange | ||
const userInput: PerseusMatrixUserInput = { | ||
answers: [[1, 2, 3], [], [7, 8, 9]], | ||
}; | ||
|
||
// Act | ||
const result = validateMatrix(userInput, {}, mockStrings); | ||
|
||
// Assert | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("should return null for non-empty user input", () => { | ||
// Arrange | ||
const userInput: PerseusMatrixUserInput = { | ||
answers: [ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9], | ||
], | ||
}; | ||
|
||
// Act | ||
const result = validateMatrix(userInput, {}, mockStrings); | ||
|
||
// 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,45 @@ | ||
import _ from "underscore"; | ||
|
||
import {getMatrixSize} from "./matrix"; | ||
|
||
import type {PerseusStrings} from "../../strings"; | ||
import type {PerseusScore} from "../../types"; | ||
import type { | ||
PerseusMatrixUserInput, | ||
PerseusMatrixValidationData, | ||
} from "../../validation.types"; | ||
|
||
/** | ||
* Checks user input from the matrix widget to see if it is scorable. | ||
* | ||
* Note: The matrix widget cannot do much validation without the Scoring | ||
* Data because of its use of KhanAnswerTypes as a core part of scoring. | ||
* | ||
* @see `scoreMatrix()` for more details. | ||
*/ | ||
function validateMatrix( | ||
userInput: PerseusMatrixUserInput, | ||
validationData: PerseusMatrixValidationData, | ||
strings: PerseusStrings, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
const supplied = userInput.answers; | ||
const suppliedSize = getMatrixSize(supplied); | ||
|
||
for (let row = 0; row < suppliedSize[0]; row++) { | ||
for (let col = 0; col < suppliedSize[1]; col++) { | ||
if ( | ||
supplied[row][col] == null || | ||
supplied[row][col].toString().length === 0 | ||
) { | ||
return { | ||
type: "invalid", | ||
message: strings.fillAllCells, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default validateMatrix; |