-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate expression widget to latest version in PerseusItem parser (#1908
) This demonstrates how we can upgrade/migrate widget options in the parsing code, obviating the need for `propUpgrades`. The fact that `version` is an object makes this really annoying because TypeScript can't discriminate unions based on nested properties like `version.major`. Given that constraint, I think the approach I came up with is reasonable, but I'm certainly open to feedback on how to improve it. Issue: LEMS-2582 ## Test plan: `yarn test` Author: benchristel Reviewers: jeremywiebe, anakaren-rojas, catandthemachines, nishasy Required Reviewers: Approved By: jeremywiebe Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1908
- Loading branch information
1 parent
3dbca96
commit 7f2866c
Showing
5 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": patch | ||
--- | ||
|
||
Internal: Migrate expression widget options to the latest version in parseAndTypecheckPerseusItem (not yet used in production). |
76 changes: 76 additions & 0 deletions
76
packages/perseus/src/util/parse-perseus-json/perseus-parsers/expression-widget.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import {parse} from "../parse"; | ||
import {failure, success} from "../result"; | ||
|
||
import {parseExpressionWidget} from "./expression-widget"; | ||
|
||
describe("parseExpressionWidget", () => { | ||
it("migrates v0 options to v1", () => { | ||
const widget = { | ||
type: "expression", | ||
graded: true, | ||
options: { | ||
value: "the value", | ||
form: false, | ||
simplify: false, | ||
times: false, | ||
buttonsVisible: "never", | ||
buttonSets: ["basic"], | ||
functions: ["f", "g", "h"], | ||
}, | ||
version: { | ||
major: 0, | ||
minor: 1, | ||
}, | ||
}; | ||
|
||
expect(parse(widget, parseExpressionWidget)).toEqual( | ||
success({ | ||
type: "expression", | ||
graded: true, | ||
static: undefined, | ||
key: undefined, | ||
alignment: undefined, | ||
options: { | ||
times: false, | ||
ariaLabel: undefined, | ||
visibleLabel: undefined, | ||
buttonsVisible: "never", | ||
buttonSets: ["basic"], | ||
functions: ["f", "g", "h"], | ||
answerForms: [ | ||
{ | ||
considered: "correct", | ||
form: false, | ||
simplify: false, | ||
value: "the value", | ||
}, | ||
], | ||
}, | ||
version: { | ||
major: 1, | ||
minor: 0, | ||
}, | ||
}), | ||
); | ||
}); | ||
|
||
it("rejects a widget with unrecognized version", () => { | ||
const widget = { | ||
type: "expression", | ||
version: { | ||
major: -1, | ||
minor: 0, | ||
}, | ||
graded: true, | ||
options: {}, | ||
}; | ||
|
||
expect(parse(widget, parseExpressionWidget)).toEqual( | ||
failure( | ||
expect.stringContaining( | ||
"At (root).version.major -- expected 0, but got -1", | ||
), | ||
), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters