Skip to content

Commit

Permalink
Fix types in input-number-editor and remove useless tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremywiebe committed Apr 13, 2023
1 parent d8f0c67 commit 1456267
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 108 deletions.
44 changes: 17 additions & 27 deletions packages/perseus-editor/src/widgets/input-number-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable react/sort-comp */
import {components, Util, InputNumber} from "@khanacademy/perseus";
import {components, Util} from "@khanacademy/perseus";
import * as React from "react";
import ReactDOM from "react-dom";
import _ from "underscore";
Expand Down Expand Up @@ -47,30 +47,20 @@ const answerTypes = {

type Props = {
value: number;
simplify: JSX.LibraryManagedAttributes<
typeof InputNumber.widget,
React.ComponentProps<typeof InputNumber.widget>
>["simplify"];
size: JSX.LibraryManagedAttributes<
typeof InputNumber.widget,
React.ComponentProps<typeof InputNumber.widget>
>["size"];
inexact: JSX.LibraryManagedAttributes<
typeof InputNumber.widget,
React.ComponentProps<typeof InputNumber.widget>
>["reviewModeRubric"]["inexact"];
maxError: JSX.LibraryManagedAttributes<
typeof InputNumber.widget,
React.ComponentProps<typeof InputNumber.widget>
>["reviewModeRubric"]["maxError"];
answerType: JSX.LibraryManagedAttributes<
typeof InputNumber.widget,
React.ComponentProps<typeof InputNumber.widget>
>["answerType"];
rightAlign: JSX.LibraryManagedAttributes<
typeof InputNumber.widget,
React.ComponentProps<typeof InputNumber.widget>
>["rightAlign"];
simplify: "required" | "optional" | "enforced";
size: "normal" | "small";
inexact: boolean;
maxError: number | string;
answerType:
| "number"
| "decimal"
| "integer"
| "rational"
| "improper"
| "mixed"
| "percent"
| "pi";
rightAlign: boolean;
onChange: (arg1: {
value?: ParsedValue | 0;
simplify?: Props["simplify"];
Expand Down Expand Up @@ -144,7 +134,7 @@ class InputNumberEditor extends React.Component<Props> {
value={this.props.simplify}
onChange={(e) => {
this.props.onChange({
// @ts-expect-error [FEI-5003] - TS2322 - Type 'string' is not assignable to type '"optional" | "required" | "enforced" | undefined'.
// @ts-expect-error TS2322 - Type 'string' is not assignable to type '"required" | "optional" | "enforced" | undefined'.
simplify: e.target.value,
});
}}
Expand Down Expand Up @@ -218,7 +208,7 @@ class InputNumberEditor extends React.Component<Props> {
<select
value={this.props.answerType}
onChange={(e) => {
// @ts-expect-error [FEI-5003] - TS2322 - Type 'string' is not assignable to type '"number" | "integer" | "mixed" | "decimal" | "improper" | "percent" | "pi" | "rational" | undefined'.
// @ts-expect-error TS2322 - Type 'string' is not assignable to type '"number" | "decimal" | "integer" | "pi" | "rational" | "improper" | "mixed" | "percent" | undefined'
this.props.onChange({answerType: e.target.value});
}}
>
Expand Down
81 changes: 0 additions & 81 deletions packages/perseus/src/widgets/__tests__/input-number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,87 +207,6 @@ describe("input-number", function () {
expect(renderer).toHaveBeenAnsweredIncorrectly();
});
});

it("transform should remove the `value` field", function () {
const editorProps = {
value: 5,
simplify: "required",
size: "normal",
inexact: false,
maxError: 0.1,
answerType: "number",
} as const;
if (!transform) {
throw new Error("transform not defined");
}
const widgetProps = transform(editorProps);
expect(_.has(widgetProps, "value")).toBe(false);
});
});

describe("invalid", function () {
beforeEach(() => {
jest.spyOn(Dependencies, "getDependencies").mockReturnValue(
testDependencies,
);
});

it("should handle invalid answers with no error callback", function () {
const err = InputNumber.widget.validate({currentValue: "x+1"}, options);
expect(err).toStrictEqual({
message: errors.EXTRA_SYMBOLS_ERROR,
type: "invalid",
});
});
});

describe("getOneCorrectAnswerFromRubric", () => {
beforeEach(() => {
jest.spyOn(Dependencies, "getDependencies").mockReturnValue(
testDependencies,
);
});

it("should return undefined if rubric.value is null/undefined", () => {
// Arrange
const rubric: Record<string, any> = {};

// Act
const result = InputNumber.widget.getOneCorrectAnswerFromRubric(rubric);

// Assert
expect(result).toBeUndefined();
});

it("should return rubric.value if inexact is false", () => {
// Arrange
const rubric = {
value: 0,
maxError: 0.1,
inexact: false,
} as const;

// Act
const result = InputNumber.widget.getOneCorrectAnswerFromRubric(rubric);

// Assert
expect(result).toEqual("0");
});

it("should return rubric.value with an error band if inexact is true", () => {
// Arrange
const rubric = {
value: 0,
maxError: 0.1,
inexact: true,
} as const;

// Act
const result = InputNumber.widget.getOneCorrectAnswerFromRubric(rubric);

// Assert
expect(result).toEqual("0 ± 0.1");
});
});

describe("rendering", () => {
Expand Down

0 comments on commit 1456267

Please sign in to comment.