From 250f67e4afd8742a50d2dc3b30889d700b3d914b Mon Sep 17 00:00:00 2001 From: Matthew Curtis Date: Tue, 26 Nov 2024 10:19:29 -0600 Subject: [PATCH] radio --- .../src/widgets/radio/__tests__/radio.test.ts | 27 +++++++++++++++++++ packages/perseus/src/widgets/radio/radio.ts | 13 +++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/perseus/src/widgets/radio/__tests__/radio.test.ts b/packages/perseus/src/widgets/radio/__tests__/radio.test.ts index 4eeb1de264..0eb7393801 100644 --- a/packages/perseus/src/widgets/radio/__tests__/radio.test.ts +++ b/packages/perseus/src/widgets/radio/__tests__/radio.test.ts @@ -8,6 +8,7 @@ import * as Dependencies from "../../../dependencies"; import {mockStrings} from "../../../strings"; import {renderQuestion} from "../../__testutils__/renderQuestion"; import PassageWidget from "../../passage"; +import RadioWidgetExport from "../radio"; import scoreRadio from "../score-radio"; import { @@ -984,3 +985,29 @@ describe("scoring", () => { expect(renderer).toHaveBeenAnsweredIncorrectly(); }); }); + +describe("propsUpgrade", () => { + it("can upgrade from v0 to v1", () => { + const v0props = { + choices: [{content: "Choice 1"}, {content: "Choice 2"}], + }; + + const result = RadioWidgetExport.propUpgrades["1"](v0props); + + expect(result).toEqual({ + choices: [{content: "Choice 1"}, {content: "Choice 2"}], + hasNoneOfTheAbove: false, + }); + }); + + it("throws from noneOfTheAbove", () => { + const v0props = { + choices: [{content: "Choice 1"}, {content: "Choice 2"}], + noneOfTheAbove: true, + }; + + expect(() => RadioWidgetExport.propUpgrades["1"](v0props)).toThrow( + "radio widget v0 no longer supports auto noneOfTheAbove", + ); + }); +}); diff --git a/packages/perseus/src/widgets/radio/radio.ts b/packages/perseus/src/widgets/radio/radio.ts index 815320c5e5..d41e084f9f 100644 --- a/packages/perseus/src/widgets/radio/radio.ts +++ b/packages/perseus/src/widgets/radio/radio.ts @@ -127,10 +127,12 @@ const transform = ( const propUpgrades = { "1": (v0props: any): any => { + const {noneOfTheAbove, ...rest} = v0props; + let choices; let hasNoneOfTheAbove; - if (!v0props.noneOfTheAbove) { + if (!noneOfTheAbove) { choices = v0props.choices; hasNoneOfTheAbove = false; } else { @@ -139,10 +141,11 @@ const propUpgrades = { ); } - return _.extend(_.omit(v0props, "noneOfTheAbove"), { - choices: choices, - hasNoneOfTheAbove: hasNoneOfTheAbove, - }); + return { + ...rest, + choices, + hasNoneOfTheAbove, + }; }, } as const;