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

Refine Radio's Rubric and UserInput types #1758

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/odd-dancers-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Refine Radio's Rubric and UserInput types
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ describe("multi-item renderer", () => {
true,
false,
],
"countChoices": false,
"noneOfTheAboveIndex": null,
"noneOfTheAboveSelected": false,
"numCorrect": 1,
Expand Down Expand Up @@ -785,7 +784,6 @@ describe("multi-item renderer", () => {
true,
false,
],
"countChoices": false,
"noneOfTheAboveIndex": null,
"noneOfTheAboveSelected": false,
"numCorrect": 1,
Expand Down
8 changes: 5 additions & 3 deletions packages/perseus/src/validation.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
PerseusNumericInputAnswer,
PerseusOrdererWidgetOptions,
PerseusPlotterWidgetOptions,
PerseusRadioWidgetOptions,
PerseusRadioChoice,
PerseusTableWidgetOptions,
} from "./perseus-types";
import type {InteractiveMarkerType} from "./widgets/label-image/types";
Expand Down Expand Up @@ -160,10 +160,12 @@ export type PerseusPlotterRubric = PerseusPlotterWidgetOptions;

export type PerseusPlotterUserInput = ReadonlyArray<number>;

export type PerseusRadioRubric = PerseusRadioWidgetOptions;
export type PerseusRadioRubric = {
// The choices provided to the user.
choices: ReadonlyArray<PerseusRadioChoice>;
};

export type PerseusRadioUserInput = {
countChoices?: boolean;
choicesSelected: ReadonlyArray<boolean>;
numCorrect?: number;
noneOfTheAboveIndex?: number | null | undefined;
Expand Down
1 change: 0 additions & 1 deletion packages/perseus/src/widgets/group/group.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ describe("group widget", () => {
false,
true,
],
"countChoices": false,
"noneOfTheAboveIndex": null,
"noneOfTheAboveSelected": false,
"numCorrect": 1,
Expand Down
4 changes: 0 additions & 4 deletions packages/perseus/src/widgets/radio/radio-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class Radio extends React.Component<Props> implements Widget {

const choiceStates = props.choiceStates;
const choicesSelected = choiceStates.map(() => false);
const countChoices = props.countChoices;
const numCorrect = props.numCorrect;

for (let i = 0; i < choicesSelected.length; i++) {
Expand All @@ -101,7 +100,6 @@ class Radio extends React.Component<Props> implements Widget {
}

return {
countChoices,
choicesSelected,
numCorrect,
noneOfTheAboveIndex,
Expand All @@ -116,7 +114,6 @@ class Radio extends React.Component<Props> implements Widget {
let noneOfTheAboveSelected = false;

const choicesSelected = [...values];
const countChoices = props.countChoices;
const numCorrect = props.numCorrect;
const valuesLength = values.length;

Expand All @@ -136,7 +133,6 @@ class Radio extends React.Component<Props> implements Widget {
choicesSelected,
noneOfTheAboveIndex,
noneOfTheAboveSelected,
countChoices,
numCorrect,
};
}
Expand Down
42 changes: 23 additions & 19 deletions packages/perseus/src/widgets/radio/radio-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import {mockStrings} from "../../strings";

import radioValidator from "./radio-validator";

import type {
PerseusRadioRubric,
PerseusRadioUserInput,
} from "../../validation.types";

describe("radioValidator", () => {
it("is invalid when no options are selected", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
choicesSelected: [false, false, false, false],
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1"},
{content: "Choice 2"},
Expand All @@ -23,12 +28,12 @@ describe("radioValidator", () => {
});

it("is invalid when number selected does not match number correct", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
numCorrect: 2,
choicesSelected: [true, false, false, false],
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1", correct: true},
{content: "Choice 2", correct: true},
Expand All @@ -43,20 +48,19 @@ describe("radioValidator", () => {
});

it("is invalid when none of the above and an answer are both selected", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
noneOfTheAboveSelected: true,
choicesSelected: [true, false, false, false, true],
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1", correct: true},
{content: "Choice 2", correct: false},
{content: "Choice 3", correct: false},
{content: "Choice 4", correct: false},
{content: "None of the above", correct: false},
],
hasNoneOfTheAbove: true,
};

const result = radioValidator(userInput, rubric, mockStrings);
Expand All @@ -65,11 +69,11 @@ describe("radioValidator", () => {
});

it("can handle single correct answer", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
choicesSelected: [true, false, false, false],
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1", correct: true},
{content: "Choice 2", correct: false},
Expand All @@ -84,11 +88,11 @@ describe("radioValidator", () => {
});

it("can handle single incorrect answer", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
choicesSelected: [false, false, false, true],
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1", correct: true},
{content: "Choice 2", correct: false},
Expand All @@ -103,11 +107,11 @@ describe("radioValidator", () => {
});

it("can handle multiple correct answer", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
choicesSelected: [true, true, false, false],
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1", correct: true},
{content: "Choice 2", correct: true},
Expand All @@ -122,11 +126,11 @@ describe("radioValidator", () => {
});

it("can handle multiple incorrect answer", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
choicesSelected: [true, false, false, true],
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1", correct: true},
{content: "Choice 2", correct: true},
Expand All @@ -141,13 +145,13 @@ describe("radioValidator", () => {
});

it("can handle none of the above correct answer", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
choicesSelected: [false, false, false, false, true],
noneOfTheAboveSelected: true,
noneOfTheAboveIndex: 4,
};

const rubric = {
const rubric: PerseusRadioRubric = {
choices: [
{content: "Choice 1", correct: false},
{content: "Choice 2", correct: false},
Expand All @@ -162,13 +166,13 @@ describe("radioValidator", () => {
});

it("can handle none of the above incorrect answer", () => {
const userInput = {
const userInput: PerseusRadioUserInput = {
choicesSelected: [false, false, false, false, true],
noneOfTheAboveSelected: true,
noneOfTheAboveIndex: 4,
};

const rubric = {
const rubric: PerseusRadioRubric = {
Copy link
Member

Choose a reason for hiding this comment

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

I love all this typing. Thank you!

choices: [
{content: "Choice 1", correct: true},
{content: "Choice 2", correct: false},
Expand Down
6 changes: 3 additions & 3 deletions packages/perseus/src/widgets/radio/radio-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function radioValidator(
};
}

// TODO: should numCorrect actually be on the rubric
// TODO(LEMS-2541): should numCorrect actually be on the rubric
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for filing this ticket. I think numCorrect, noneOfTheAboveIndex, and noneOfTheAboveSelected are all problematic, but I don't think we have to address it in this PR. It should probably be a fast follow though.

// instead of the userInput?
if (
userInput.numCorrect &&
Expand All @@ -35,7 +35,7 @@ function radioValidator(
// If NOTA and some other answer are checked, ...
}

// TODO: should noneOfTheAboveSelected be replaced with a
// TODO(LEMS-2541): should noneOfTheAboveSelected be replaced with a
// combination of choicesSelected and noneOfTheAboveIndex?
if (userInput.noneOfTheAboveSelected && numSelected > 1) {
return {
Expand All @@ -46,7 +46,7 @@ function radioValidator(

const correct = userInput.choicesSelected.every((selected, i) => {
let isCorrect: boolean;
// TODO: should noneOfTheAboveIndex actually be on the rubric
// TODO(LEMS-2541): should noneOfTheAboveIndex actually be on the rubric
// instead of the userInput?
if (userInput.noneOfTheAboveIndex === i) {
isCorrect = rubric.choices.every((choice, j) => {
Expand Down