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

Sorter: Extract validation out of scoring #1876

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/sweet-trainers-drop.md
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.
12 changes: 0 additions & 12 deletions packages/perseus/src/widgets/sorter/score-sorter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,4 @@ describe("scoreSorter", () => {
const result = scoreSorter(userInput, rubric);
expect(result).toHaveBeenAnsweredIncorrectly();
});

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 = scoreSorter(userInput, rubric);
expect(result).toHaveInvalidInput();
});
Copy link
Collaborator

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:

// Top of file 
import * as SorterValidator from './validate-sorter'

    // Inside describe() block...
    it("should score if validator passes", () => {
        // Mock validator saying "all good" 👍 
        const mockValidate = jest
            .spyOn(SorterValidator, "default")
            .mockReturnValue(null);


        const score = scoreSorter(userInput, rubric);

        // Assert
        expect(mockValidate).toHaveBeenCalled();
        expect(score).toHaveBeenAnsweredCorrectly();
    });

    it("should abort if validator returns invalid", () => {
        // Mock validator saying "all good" 👍 
        const mockValidate = jest
            .spyOn(SorterValidator, "default")
            .mockReturnValue({type: "invalid", message: null});

        const score = scoreSorter(userInput, rubric);

        // Assert
        expect(mockValidate).toHaveBeenCalled();
        expect(score).toHaveInvalidInput()
    });

});
16 changes: 5 additions & 11 deletions packages/perseus/src/widgets/sorter/score-sorter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Util from "../../util";

import validateSorter from "./validate-sorter";

import type {PerseusScore} from "../../types";
import type {
PerseusSorterRubric,
Expand All @@ -10,17 +12,9 @@ function scoreSorter(
userInput: PerseusSorterUserInput,
rubric: PerseusSorterRubric,
): PerseusScore {
// 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,
};
const validationError = validateSorter(userInput, rubric);
if (validationError) {
return validationError;
}

const correct = Util.deepEq(userInput.options, rubric.correct);
Expand Down
32 changes: 32 additions & 0 deletions packages/perseus/src/widgets/sorter/validate-sorter.test.ts
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();
});
});
26 changes: 26 additions & 0 deletions packages/perseus/src/widgets/sorter/validate-sorter.ts
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;