Skip to content

Commit

Permalink
remove scoreInput
Browse files Browse the repository at this point in the history
  • Loading branch information
handeyeco committed Dec 5, 2024
1 parent 22faad9 commit 1608114
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 78 deletions.
1 change: 0 additions & 1 deletion packages/perseus-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type State = any;
export interface RendererInterface {
getSerializedState(): State;
restoreSerializedState(state: State, callback?: () => void): void;
scoreInput(): KEScore;
blur(): void;
focus(): boolean | null | undefined;
props: any;
Expand Down
25 changes: 0 additions & 25 deletions packages/perseus/src/__tests__/server-item-renderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,6 @@ describe("server item renderer", () => {
expect(screen.getByRole("textbox")).toBeVisible();
});

it("should be invalid if no input provided", async () => {
// Arrange
const {renderer} = renderQuestion(itemWithInput);

// Act
const score = await act(() => renderer.scoreInput());

// Assert
expect(score.correct).toBe(false);
expect(score.empty).toBe(true);
});

it("should be answerable", async () => {
// Arrange
const {renderer} = renderQuestion(itemWithInput);
await userEvent.type(screen.getByRole("textbox"), "-42");

// Act
const score = await act(() => renderer.scoreInput());

// Assert
expect(score.correct).toBe(true);
expect(score.empty).toBe(false);
});

it("should pass showSolutions to the widgets", () => {
// Arrange
renderQuestion(itemWithRadioAndExpressionWidgets, Object.freeze({}), {
Expand Down
36 changes: 0 additions & 36 deletions packages/perseus/src/server-item-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import HintsRenderer from "./hints-renderer";
import LoadingContext from "./loading-context";
import {ApiOptions} from "./perseus-api";
import Renderer from "./renderer";
import {scorePerseusItem} from "./renderer-util";
import Util from "./util";

import type {PerseusItem, ShowSolutions} from "./perseus-types";
Expand Down Expand Up @@ -347,41 +346,6 @@ export class ServerItemRenderer
return this.questionRenderer.getUserInputMap();
}

/**
* Grades the item.
*
* @deprecated use scorePerseusItem
*/
scoreInput(): KEScore {
const guess = this.getUserInput();
const score = scorePerseusItem(
this.props.item.question,
guess,
this.context.strings,
this.context.locale,
);

// Continue to include an empty guess for the now defunct answer area.
// TODO(alex): Check whether we rely on the format here for
// analyzing ProblemLogs. If not, remove this layer.
const maxCompatGuess = [this.questionRenderer.getUserInput(), []];

const keScore = Util.keScoreFromPerseusScore(
score,
maxCompatGuess,
this.questionRenderer.getSerializedState(),
);

const emptyQuestionAreaWidgets = this.questionRenderer.emptyWidgets();

this.setState({
questionCompleted: keScore.correct,
questionHighlightedWidgets: emptyQuestionAreaWidgets,
});

return keScore;
}

/**
* Returns an array of all widget IDs in the order they occur in
* the question content.
Expand Down
2 changes: 1 addition & 1 deletion packages/perseus/src/widget-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class WidgetContainer extends React.Component<Props, State> {
: {...this.props.linterContext, highlightLint: false};

// Note: if you add more props here, please consider whether or not
// it should be auto-serialized (e.g. used in scoreInput()). See
// it should be auto-serialized. See
// widget-jsonify-deprecated.jsx and widget-prop-denylist.jsx

// We default to an empty object for style instead of null
Expand Down
3 changes: 0 additions & 3 deletions packages/perseus/src/widgets/expression/score-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ function scoreExpression(
// We matched a graded answer form, so we can now tell the user
// whether their input was correct or incorrect, and hand out
// points accordingly
// TODO(eater): Seems silly to translate result to this
// invalid/points thing and immediately translate it back in
// ItemRenderer.scoreInput()
return {
type: "points",
earned: matchingAnswerForm.considered === "correct" ? 1 : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ function scoreInputNumber(

const result = val(currentValue);

// TODO(eater): Seems silly to translate result to this invalid/points
// thing and immediately translate it back in ItemRenderer.scoreInput()
if (result.empty) {
return {
type: "invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ function scoreNumericInput(
guess: localValue,
};

// TODO(eater): Seems silly to translate result to this
// invalid/points thing and immediately translate it
// back in ItemRenderer.scoreInput()
if (result.empty) {
return {
type: "invalid",
Expand Down
28 changes: 27 additions & 1 deletion testing/server-item-renderer-with-debug-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Strut} from "@khanacademy/wonder-blocks-layout";
import * as React from "react";

import * as Perseus from "../packages/perseus/src/index";
import {mockStrings} from "../packages/perseus/src/strings";

import KEScoreUI from "./ke-score-ui";
import SideBySide from "./side-by-side";
Expand All @@ -29,6 +30,31 @@ export const ServerItemRendererWithDebugUI = ({
const [state, setState] = React.useState<KEScore | null | undefined>(null);
const options = apiOptions || Object.freeze({});

const getKeScore = () => {
const renderer = ref.current;
if (!renderer) {
return;
}

const userInput = renderer.getUserInput();
const score = Perseus.scorePerseusItem(
item.question,
userInput,
mockStrings,
"en",
);

// Continue to include an empty guess for the now defunct answer area.
// TODO(alex): Check whether we rely on the format here for
// analyzing ProblemLogs. If not, remove this layer.
const maxCompatGuess = [renderer.getUserInputLegacy(), []];
return Perseus.Util.keScoreFromPerseusScore(
score,
maxCompatGuess,
renderer.getSerializedState().question,
);
};

return (
<SideBySide
leftTitle="Renderer"
Expand All @@ -48,7 +74,7 @@ export const ServerItemRendererWithDebugUI = ({
if (!ref.current) {
return;
}
setState(ref.current.scoreInput());
setState(getKeScore());
}}
>
Check
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5166,16 +5166,11 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001332, caniuse-lite@^1.0.30001541, caniuse-lite@^1.0.30001587:
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001332, caniuse-lite@^1.0.30001541, caniuse-lite@^1.0.30001587, caniuse-lite@^1.0.30001669:
version "1.0.30001685"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001685.tgz"
integrity sha512-e/kJN1EMyHQzgcMEEgoo+YTCO1NGCmIYHk5Qk8jT6AazWemS5QFKJ5ShCJlH3GZrNIdZofcNCEwZqbMjjKzmnA==

caniuse-lite@^1.0.30001669:
version "1.0.30001685"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001685.tgz#2d10d36c540a9a5d47ad6ab9e1ed5f61fdeadd8c"
integrity sha512-e/kJN1EMyHQzgcMEEgoo+YTCO1NGCmIYHk5Qk8jT6AazWemS5QFKJ5ShCJlH3GZrNIdZofcNCEwZqbMjjKzmnA==

caseless@~0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
Expand Down

0 comments on commit 1608114

Please sign in to comment.