Skip to content

Commit

Permalink
radio
Browse files Browse the repository at this point in the history
  • Loading branch information
handeyeco committed Nov 26, 2024
1 parent 65c06c1 commit 250f67e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
27 changes: 27 additions & 0 deletions packages/perseus/src/widgets/radio/__tests__/radio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
);
});
});
13 changes: 8 additions & 5 deletions packages/perseus/src/widgets/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -139,10 +141,11 @@ const propUpgrades = {
);
}

return _.extend(_.omit(v0props, "noneOfTheAbove"), {
choices: choices,
hasNoneOfTheAbove: hasNoneOfTheAbove,
});
return {
...rest,
choices,
hasNoneOfTheAbove,
};
},
} as const;

Expand Down

0 comments on commit 250f67e

Please sign in to comment.