From fba00dc4ed0de73ccd8f71e84cff1475b8706c99 Mon Sep 17 00:00:00 2001 From: Tamara Date: Wed, 16 Oct 2024 15:55:06 -0500 Subject: [PATCH 1/6] Add types to test And remove one item that isn't needed --- .../src/widgets/radio/radio-validator.test.ts | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/perseus/src/widgets/radio/radio-validator.test.ts b/packages/perseus/src/widgets/radio/radio-validator.test.ts index 35da9bef95..b06c790154 100644 --- a/packages/perseus/src/widgets/radio/radio-validator.test.ts +++ b/packages/perseus/src/widgets/radio/radio-validator.test.ts @@ -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"}, @@ -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}, @@ -43,12 +48,12 @@ 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}, @@ -56,7 +61,6 @@ describe("radioValidator", () => { {content: "Choice 4", correct: false}, {content: "None of the above", correct: false}, ], - hasNoneOfTheAbove: true, }; const result = radioValidator(userInput, rubric, mockStrings); @@ -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}, @@ -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}, @@ -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}, @@ -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}, @@ -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}, @@ -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 = { choices: [ {content: "Choice 1", correct: true}, {content: "Choice 2", correct: false}, From 2c2df854d849a4b8090a27eef8239b0ed1f36214 Mon Sep 17 00:00:00 2001 From: Tamara Date: Wed, 16 Oct 2024 15:55:48 -0500 Subject: [PATCH 2/6] Refine Rubric type to what is needed to score --- packages/perseus/src/validation.types.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/perseus/src/validation.types.ts b/packages/perseus/src/validation.types.ts index addeb6fed0..b68589815e 100644 --- a/packages/perseus/src/validation.types.ts +++ b/packages/perseus/src/validation.types.ts @@ -20,7 +20,7 @@ import type { PerseusPassageRefWidgetOptions, PerseusPassageWidgetOptions, PerseusPlotterWidgetOptions, - PerseusRadioWidgetOptions, + PerseusRadioChoice, PerseusSorterWidgetOptions, PerseusTableWidgetOptions, PerseusVideoWidgetOptions, @@ -177,9 +177,14 @@ export type PerseusPlotterRubric = PerseusPlotterWidgetOptions; export type PerseusPlotterUserInput = ReadonlyArray; -export type PerseusRadioRubric = PerseusRadioWidgetOptions; +export type PerseusRadioRubric = { + // The choices provided to the user. + choices: ReadonlyArray; +}; export type PerseusRadioUserInput = { + /*TODO(LEMS-2541): countChoices seems to be necessary for rendering the + question, not scoring. Maybe move to renderProps?*/ countChoices?: boolean; choicesSelected: ReadonlyArray; numCorrect?: number; From 7d37ff7975623c08b75169c3e84ff7a1050043f1 Mon Sep 17 00:00:00 2001 From: Tamara Date: Wed, 16 Oct 2024 15:58:05 -0500 Subject: [PATCH 3/6] Update TODOs with ticket reference --- packages/perseus/src/widgets/radio/radio-validator.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/perseus/src/widgets/radio/radio-validator.ts b/packages/perseus/src/widgets/radio/radio-validator.ts index 019582efc4..4d5165e3fd 100644 --- a/packages/perseus/src/widgets/radio/radio-validator.ts +++ b/packages/perseus/src/widgets/radio/radio-validator.ts @@ -21,7 +21,7 @@ function radioValidator( }; } - // TODO: should numCorrect actually be on the rubric + // TODO(LEMS-2541): should numCorrect actually be on the rubric // instead of the userInput? if ( userInput.numCorrect && @@ -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 { @@ -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) => { From 6950cc8ee6f0dcf0d0b8ed3776c125e250d30506 Mon Sep 17 00:00:00 2001 From: Tamara Date: Wed, 16 Oct 2024 15:58:48 -0500 Subject: [PATCH 4/6] Add changeset --- .changeset/odd-dancers-relax.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/odd-dancers-relax.md diff --git a/.changeset/odd-dancers-relax.md b/.changeset/odd-dancers-relax.md new file mode 100644 index 0000000000..0880106194 --- /dev/null +++ b/.changeset/odd-dancers-relax.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/perseus": patch +--- + +Refine Radio's Rubric type From 4ede90071aa9f9acfbd2365e3f26686cf6f8f535 Mon Sep 17 00:00:00 2001 From: Tamara Date: Thu, 17 Oct 2024 11:45:43 -0500 Subject: [PATCH 5/6] Remove countChoices from userInput --- .../perseus/src/multi-items/__tests__/multi-renderer.test.tsx | 2 -- packages/perseus/src/validation.types.ts | 3 --- packages/perseus/src/widgets/group/group.test.tsx | 1 - packages/perseus/src/widgets/radio/radio-component.tsx | 4 ---- 4 files changed, 10 deletions(-) diff --git a/packages/perseus/src/multi-items/__tests__/multi-renderer.test.tsx b/packages/perseus/src/multi-items/__tests__/multi-renderer.test.tsx index 9c073a6fdd..8842225333 100644 --- a/packages/perseus/src/multi-items/__tests__/multi-renderer.test.tsx +++ b/packages/perseus/src/multi-items/__tests__/multi-renderer.test.tsx @@ -629,7 +629,6 @@ describe("multi-item renderer", () => { true, false, ], - "countChoices": false, "noneOfTheAboveIndex": null, "noneOfTheAboveSelected": false, "numCorrect": 1, @@ -785,7 +784,6 @@ describe("multi-item renderer", () => { true, false, ], - "countChoices": false, "noneOfTheAboveIndex": null, "noneOfTheAboveSelected": false, "numCorrect": 1, diff --git a/packages/perseus/src/validation.types.ts b/packages/perseus/src/validation.types.ts index b68589815e..58dea6f221 100644 --- a/packages/perseus/src/validation.types.ts +++ b/packages/perseus/src/validation.types.ts @@ -183,9 +183,6 @@ export type PerseusRadioRubric = { }; export type PerseusRadioUserInput = { - /*TODO(LEMS-2541): countChoices seems to be necessary for rendering the - question, not scoring. Maybe move to renderProps?*/ - countChoices?: boolean; choicesSelected: ReadonlyArray; numCorrect?: number; noneOfTheAboveIndex?: number | null | undefined; diff --git a/packages/perseus/src/widgets/group/group.test.tsx b/packages/perseus/src/widgets/group/group.test.tsx index ae22946c61..015e5622ac 100644 --- a/packages/perseus/src/widgets/group/group.test.tsx +++ b/packages/perseus/src/widgets/group/group.test.tsx @@ -429,7 +429,6 @@ describe("group widget", () => { false, true, ], - "countChoices": false, "noneOfTheAboveIndex": null, "noneOfTheAboveSelected": false, "numCorrect": 1, diff --git a/packages/perseus/src/widgets/radio/radio-component.tsx b/packages/perseus/src/widgets/radio/radio-component.tsx index 821e9af822..c329ac0f8d 100644 --- a/packages/perseus/src/widgets/radio/radio-component.tsx +++ b/packages/perseus/src/widgets/radio/radio-component.tsx @@ -82,7 +82,6 @@ class Radio extends React.Component 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++) { @@ -101,7 +100,6 @@ class Radio extends React.Component implements Widget { } return { - countChoices, choicesSelected, numCorrect, noneOfTheAboveIndex, @@ -116,7 +114,6 @@ class Radio extends React.Component implements Widget { let noneOfTheAboveSelected = false; const choicesSelected = [...values]; - const countChoices = props.countChoices; const numCorrect = props.numCorrect; const valuesLength = values.length; @@ -136,7 +133,6 @@ class Radio extends React.Component implements Widget { choicesSelected, noneOfTheAboveIndex, noneOfTheAboveSelected, - countChoices, numCorrect, }; } From 480e6530f0c71b954d48ae5120eb2e0bfa0c3abf Mon Sep 17 00:00:00 2001 From: Tamara Date: Thu, 17 Oct 2024 11:54:02 -0500 Subject: [PATCH 6/6] Update changeset --- .changeset/odd-dancers-relax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/odd-dancers-relax.md b/.changeset/odd-dancers-relax.md index 0880106194..2df7d48176 100644 --- a/.changeset/odd-dancers-relax.md +++ b/.changeset/odd-dancers-relax.md @@ -2,4 +2,4 @@ "@khanacademy/perseus": patch --- -Refine Radio's Rubric type +Refine Radio's Rubric and UserInput types