Skip to content
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

Dropdown: Extract validation out of scoring #1898

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-horses-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": minor
---

Introduces a validation function for the dropdown widget (extracted from dropdown scoring function).
24 changes: 4 additions & 20 deletions packages/perseus/src/widgets/dropdown/score-dropdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ import type {
} from "../../validation.types";

describe("scoreDropdown", () => {
it("returns invalid for user input of 0", () => {
// Arrange
const userInput: PerseusDropdownUserInput = {
value: 0,
};
const rubric: PerseusDropdownRubric = {
choices: question1.widgets["dropdown 1"].options.choices,
};

// Act
const result = scoreDropdown(userInput, rubric);

// Assert
expect(result).toHaveInvalidInput();
});
Comment on lines -10 to -24
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't keep a check for this in scoring test since it looks like we might be doing validation before scoring (love that)


it("returns 0 points for incorrect answer", () => {
// Arrange
const userInput: PerseusDropdownUserInput = {
Expand All @@ -33,10 +17,10 @@ describe("scoreDropdown", () => {
};

// Act
const result = scoreDropdown(userInput, rubric);
const score = scoreDropdown(userInput, rubric);

// Assert
expect(result).toHaveBeenAnsweredIncorrectly();
expect(score).toHaveBeenAnsweredIncorrectly();
});

it("returns 1 point for correct answer", () => {
Expand All @@ -49,9 +33,9 @@ describe("scoreDropdown", () => {
};

// Act
const result = scoreDropdown(userInput, rubric);
const score = scoreDropdown(userInput, rubric);

// Assert
expect(result).toHaveBeenAnsweredCorrectly();
expect(score).toHaveBeenAnsweredCorrectly();
});
});
13 changes: 6 additions & 7 deletions packages/perseus/src/widgets/dropdown/score-dropdown.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import validateDropdown from "./validate-dropdown";

import type {PerseusScore} from "../../types";
import type {
PerseusDropdownRubric,
Expand All @@ -8,14 +10,11 @@ function scoreDropdown(
userInput: PerseusDropdownUserInput,
rubric: PerseusDropdownRubric,
): PerseusScore {
const selected = userInput.value;
if (selected === 0) {
return {
type: "invalid",
message: null,
};
const validationError = validateDropdown(userInput);
if (validationError) {
return validationError;
}
const correct = rubric.choices[selected - 1].correct;
const correct = rubric.choices[userInput.value - 1].correct;
return {
type: "points",
earned: correct ? 1 : 0,
Expand Down
31 changes: 31 additions & 0 deletions packages/perseus/src/widgets/dropdown/validate-dropdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import validateDropdown from "./validate-dropdown";

import type {PerseusDropdownUserInput} from "../../validation.types";

describe("validateDropdown", () => {
it("returns invalid for invalid input (user input of 0)", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny improvement suggestion as 0 is the selected index. 🤷‍♂️ You decide. :)

Suggested change
it("returns invalid for invalid input (user input of 0)", () => {
it("returns invalid when nothing selected (user input of 0)", () => {

// Arrange
const userInput: PerseusDropdownUserInput = {
value: 0,
};

// Act
const validationError = validateDropdown(userInput);

// Assert
expect(validationError).toHaveInvalidInput();
});

it("returns null for a valid answer (user input that is not 0)", () => {
// Arrange
const userInput: PerseusDropdownUserInput = {
value: 2,
};

// Act
const validationError = validateDropdown(userInput);

// Assert
expect(validationError).toBeNull();
});
});
21 changes: 21 additions & 0 deletions packages/perseus/src/widgets/dropdown/validate-dropdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type {PerseusScore} from "../../types";
import type {PerseusDropdownUserInput} from "../../validation.types";

/**
* Checks if the user has selected an item from the dropdown before scoring.
* @param userInput
* @see `scoreDropdown` for details on scoring
*/
function validateDropdown(
userInput: PerseusDropdownUserInput,
): Extract<PerseusScore, {type: "invalid"}> | null {
if (userInput.value === 0) {
return {
type: "invalid",
message: null,
};
}
return null;
}

export default validateDropdown;