diff --git a/.changeset/mean-pens-grow.md b/.changeset/mean-pens-grow.md new file mode 100644 index 0000000000..771d67c9d4 --- /dev/null +++ b/.changeset/mean-pens-grow.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/perseus": patch +--- + +Internal: Add regression tests for PerseusItem parser diff --git a/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts b/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts index b1ad09b5ed..4678fdaf8b 100644 --- a/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts +++ b/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts @@ -43,6 +43,6 @@ export const parseRadioWidget: Parser = parseWidget( displayCount: optional(any), // v0 props // `noneOfTheAbove` is still in use (but only set to `false`). - noneOfTheAbove: constant(false), + noneOfTheAbove: optional(constant(false)), }), ); diff --git a/packages/perseus/src/util/parse-perseus-json/regression-tests/data/radio-missing-noneOfTheAbove.json b/packages/perseus/src/util/parse-perseus-json/regression-tests/data/radio-missing-noneOfTheAbove.json new file mode 100644 index 0000000000..ed463b51a8 --- /dev/null +++ b/packages/perseus/src/util/parse-perseus-json/regression-tests/data/radio-missing-noneOfTheAbove.json @@ -0,0 +1,48 @@ +{ + "question": { + "content": "$\\begin{align} \nA &= \\{x: x \\text{ is a rectangle in the plane P} \\} \\\\\\\\\nB &= \\{x: x \\text{ is a square in the plane P} \\}\n\\end{align}$\n\n**Is $A \\subset B$ ?**\n\n[[☃ radio 1]]\n", + "images": {}, + "widgets": { + "radio 1": { + "type": "radio", + "alignment": "default", + "static": false, + "graded": true, + "options": { + "choices": [ + { + "content": "Yes", + "correct": false + }, + { + "content": "No", + "correct": true + } + ], + "randomize": false, + "multipleSelect": false, + "countChoices": false, + "displayCount": null, + "hasNoneOfTheAbove": false, + "deselectEnabled": false + }, + "version": { + "major": 1, + "minor": 0 + } + } + } + }, + "answerArea": { + "calculator": false, + "chi2Table": false, + "periodicTable": false, + "tTable": false, + "zTable": false + }, + "itemDataVersion": { + "major": 0, + "minor": 1 + }, + "hints": [] +} diff --git a/packages/perseus/src/util/parse-perseus-json/regression-tests/parse-perseus-json-regression.test.ts b/packages/perseus/src/util/parse-perseus-json/regression-tests/parse-perseus-json-regression.test.ts new file mode 100644 index 0000000000..96a2b1b32d --- /dev/null +++ b/packages/perseus/src/util/parse-perseus-json/regression-tests/parse-perseus-json-regression.test.ts @@ -0,0 +1,20 @@ +import * as fs from "fs"; +import {join} from "path"; + +import {parseAndTypecheckPerseusItem} from "../index"; + +const dataFiles = fs.readdirSync(join(__dirname, "data")); + +describe("parseAndTypecheckPerseusItem", () => { + it.each(dataFiles)("accepts data/%s", (filename) => { + const json = fs.readFileSync( + join(__dirname, "data", filename), + "utf-8", + ); + const result = parseAndTypecheckPerseusItem(json); + + // This strange-looking assertion style results in the failure message + // being printed if parsing fails, so the test is easier to debug. + expect(result).toEqual(expect.objectContaining({type: "success"})); + }); +});