-
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.
Table: Extract validation out of scoring (#1849)
## Summary: This PR is a test run for extracting validation logic from scoring (using `table` as the test subject). I've used a pattern where the validator still returns a `PerseusScore`. This means that we can call the validator as the first step in scoring and return the score if it's `invalid`. Issue: LEMS-2606 ## Test plan: All tests should still pass. Author: jeremywiebe Reviewers: jeremywiebe, Myranae, handeyeco Required Reviewers: Approved By: handeyeco, Myranae Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (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), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1849
- Loading branch information
1 parent
46dc13f
commit f7160d6
Showing
9 changed files
with
103 additions
and
48 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 | ||
--- | ||
|
||
Introduce validation function for `table` widget - useful for checking if the learner has filled in the table sufficiently to score it (fully client-side) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Filters the given table (modelled as a 2D array) to remove any rows that are | ||
* completely empty. | ||
* | ||
* @returns A new table with only non-empty rows. | ||
*/ | ||
export const filterNonEmpty = function ( | ||
table: ReadonlyArray<ReadonlyArray<string>>, | ||
) { | ||
return table.filter(function (row) { | ||
// Return only rows that are non-empty. | ||
return row.some((cell) => cell); | ||
}); | ||
}; |
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 validateTable from "./validate-table"; | ||
|
||
import type {PerseusTableUserInput} from "../../validation.types"; | ||
|
||
describe("tableValidator", () => { | ||
it("is invalid if there is an empty cell", () => { | ||
// Arrange | ||
const userInput: PerseusTableUserInput = [ | ||
["1", ""], | ||
["3", "4"], | ||
]; | ||
|
||
// Act | ||
const result = validateTable(userInput); | ||
|
||
// Assert | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("is null if all cells are provided", () => { | ||
// Arrange | ||
const userInput: PerseusTableUserInput = [ | ||
["1", "2"], | ||
["3", "4"], | ||
]; | ||
|
||
// Act | ||
const result = validateTable(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,27 @@ | ||
import {filterNonEmpty} from "./utils"; | ||
|
||
import type {PerseusScore} from "../../types"; | ||
import type {PerseusTableUserInput} from "../../validation.types"; | ||
|
||
function validateTable( | ||
userInput: PerseusTableUserInput, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
const supplied = filterNonEmpty(userInput); | ||
|
||
const hasEmptyCell = supplied.some(function (row) { | ||
return row.some(function (cell) { | ||
return cell === ""; | ||
}); | ||
}); | ||
|
||
if (hasEmptyCell || !supplied.length) { | ||
return { | ||
type: "invalid", | ||
message: null, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default validateTable; |