Skip to content

Commit 32cc4a4

Browse files
authored
Create helper to build public widget options for expression (#2139)
## Summary: Adds a function that takes expression's full widget options and filters out answer data. It also adds this function to the widget's widget export and adds a test confirming the function does what is expected. Issue: LEMS-2758 ## Test plan: - Confirm all checks pass - Confirm expression still works as expected Author: Myranae Reviewers: Myranae, benchristel, handeyeco, jeremywiebe Required Reviewers: Approved By: handeyeco, jeremywiebe Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x) Pull Request URL: #2139
1 parent ffaa390 commit 32cc4a4

File tree

7 files changed

+88
-1
lines changed

7 files changed

+88
-1
lines changed

.changeset/smooth-houses-burn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/perseus": minor
3+
---
4+
5+
Implement a widget export function to filter out rubric data from widget options for the Expression widget

.changeset/ten-insects-cheat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/perseus-core": patch
3+
---
4+
5+
Move util files out of widget folder in perseus package to utils folder in perseus-core

packages/perseus-core/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ export type {ExpressionDefaultWidgetOptions} from "./widgets/expression";
3838
export type * from "./widgets/logic-export.types";
3939

4040
export {default as getOrdererPublicWidgetOptions} from "./utils/orderer-util";
41+
42+
export {default as getExpressionPublicWidgetOptions} from "./utils/expression-util";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import getExpressionPublicWidgetOptions from "./expression-util";
2+
3+
import type {PerseusExpressionWidgetOptions} from "@khanacademy/perseus-core";
4+
5+
describe("getExpressionPublicWidgetOptions", () => {
6+
it("should return the correct public options without any answer data", () => {
7+
// Arrange
8+
const options: PerseusExpressionWidgetOptions = {
9+
answerForms: [
10+
{
11+
considered: "correct",
12+
form: false,
13+
simplify: false,
14+
value: "123-x",
15+
},
16+
],
17+
buttonSets: ["basic"],
18+
functions: ["f", "g", "h"],
19+
times: false,
20+
visibleLabel: "the visible label",
21+
ariaLabel: "the aria label",
22+
buttonsVisible: "always",
23+
};
24+
25+
// Act
26+
const publicWidgetOptions = getExpressionPublicWidgetOptions(options);
27+
28+
// Assert
29+
expect(publicWidgetOptions).toEqual({
30+
times: false,
31+
buttonSets: ["basic"],
32+
functions: ["f", "g", "h"],
33+
buttonsVisible: "always",
34+
visibleLabel: "the visible label",
35+
ariaLabel: "the aria label",
36+
});
37+
});
38+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type {PerseusExpressionWidgetOptions} from "@khanacademy/perseus-core";
2+
3+
/**
4+
* For details on the individual options, see the
5+
* PerseusExpressionWidgetOptions type
6+
*/
7+
type ExpressionPublicWidgetOptions = {
8+
buttonSets: PerseusExpressionWidgetOptions["buttonSets"];
9+
functions: PerseusExpressionWidgetOptions["functions"];
10+
times: PerseusExpressionWidgetOptions["times"];
11+
visibleLabel?: PerseusExpressionWidgetOptions["visibleLabel"];
12+
ariaLabel?: PerseusExpressionWidgetOptions["ariaLabel"];
13+
buttonsVisible?: PerseusExpressionWidgetOptions["buttonsVisible"];
14+
};
15+
16+
/**
17+
* Given a PerseusExpressionWidgetOptions object, return a new object with only
18+
* the public options that should be exposed to the client.
19+
*/
20+
function getExpressionPublicWidgetOptions(
21+
options: PerseusExpressionWidgetOptions,
22+
): ExpressionPublicWidgetOptions {
23+
return {
24+
buttonSets: options.buttonSets,
25+
functions: options.functions,
26+
times: options.times,
27+
visibleLabel: options.visibleLabel,
28+
ariaLabel: options.ariaLabel,
29+
buttonsVisible: options.buttonsVisible,
30+
};
31+
}
32+
33+
export default getExpressionPublicWidgetOptions;

packages/perseus/src/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
Version,
1515
WidgetOptionsUpgradeMap,
1616
getOrdererPublicWidgetOptions,
17+
getExpressionPublicWidgetOptions,
1718
} from "@khanacademy/perseus-core";
1819
import type {LinterContextProps} from "@khanacademy/perseus-linter";
1920
import type {
@@ -541,7 +542,8 @@ export type WidgetScorerFunction = (
541542
*/
542543
export type PublicWidgetOptionsFunction =
543544
| typeof getCategorizerPublicWidgetOptions
544-
| typeof getOrdererPublicWidgetOptions;
545+
| typeof getOrdererPublicWidgetOptions
546+
| typeof getExpressionPublicWidgetOptions;
545547

546548
export type WidgetExports<
547549
T extends React.ComponentType<any> & Widget = React.ComponentType<any>,

packages/perseus/src/widgets/expression/expression.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getDecimalSeparator,
55
expressionLogic,
66
type PerseusExpressionWidgetOptions,
7+
getExpressionPublicWidgetOptions,
78
} from "@khanacademy/perseus-core";
89
import {linterContextDefault} from "@khanacademy/perseus-linter";
910
import {scoreExpression, validateExpression} from "@khanacademy/perseus-score";
@@ -542,6 +543,7 @@ export default {
542543
// TODO(LEMS-2656): remove TS suppression
543544
// @ts-expect-error: Type 'UserInput' is not assignable to type 'PerseusExpressionUserInput'.
544545
validator: validateExpression,
546+
getPublicWidgetOptions: getExpressionPublicWidgetOptions,
545547

546548
// TODO(LEMS-2656): remove TS suppression
547549
// @ts-expect-error: Type 'ScoringData' is not assignable to type 'PerseusExpressionScoringData'.

0 commit comments

Comments
 (0)