Skip to content

Commit

Permalink
Create helper to build public widget options for orderer (#2137)
Browse files Browse the repository at this point in the history
## Summary:
Adds a function that takes orderer'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 as expected. 

Issue: LEMS-2763

## Test plan:
- Confirm all checks pass
- Confirm orderer still works as expected

Author: Myranae

Reviewers: Myranae, benchristel, jeremywiebe, handeyeco

Required Reviewers:

Approved By: benchristel, jeremywiebe

Checks: ✅ Publish npm snapshot (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), ✅ 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: #2137
  • Loading branch information
Myranae authored Jan 23, 2025
1 parent 36198bf commit b4b3a3d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-points-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus-core": patch
---

Move util files out of widget folder in perseus package to utils folder in perseus-core
5 changes: 5 additions & 0 deletions .changeset/seven-kiwis-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": minor
---

Implement a widget export function to filter out rubric data from widget options for the orderer widget
2 changes: 2 additions & 0 deletions packages/perseus-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export {default as expressionLogic} from "./widgets/expression";
export type {ExpressionDefaultWidgetOptions} from "./widgets/expression";

export type * from "./widgets/logic-export.types";

export {default as getOrdererPublicWidgetOptions} from "./utils/orderer-util";
38 changes: 38 additions & 0 deletions packages/perseus-core/src/utils/orderer-util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import getOrdererPublicWidgetOptions from "./orderer-util";

import type {PerseusOrdererWidgetOptions} from "@khanacademy/perseus-core";

describe("getOrdererPublicWidgetOptions", () => {
it("should return the correct public options without any answer data", () => {
// Arrange
const options: PerseusOrdererWidgetOptions = {
otherOptions: [],
layout: "horizontal",
options: [
{content: "$10.9$", images: {}, widgets: {}},
{content: "$11$", images: {}, widgets: {}},
{content: "$\\sqrt{120}$", images: {}, widgets: {}},
],
correctOptions: [
{content: "$10.9$", images: {}, widgets: {}},
{content: "$\\sqrt{120}$", images: {}, widgets: {}},
{content: "$11$", images: {}, widgets: {}},
],
height: "normal",
};

// Act
const publicWidgetOptions = getOrdererPublicWidgetOptions(options);

// Assert
expect(publicWidgetOptions).toEqual({
layout: "horizontal",
options: [
{content: "$10.9$", images: {}, widgets: {}},
{content: "$11$", images: {}, widgets: {}},
{content: "$\\sqrt{120}$", images: {}, widgets: {}},
],
height: "normal",
});
});
});
27 changes: 27 additions & 0 deletions packages/perseus-core/src/utils/orderer-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {PerseusOrdererWidgetOptions} from "@khanacademy/perseus-core";

/**
* For details on the individual options, see the
* PerseusOrdererWidgetOptions type
*/
type OrdererPublicWidgetOptions = {
options: PerseusOrdererWidgetOptions["options"];
height: PerseusOrdererWidgetOptions["height"];
layout: PerseusOrdererWidgetOptions["layout"];
};

/**
* Given a PerseusOrdererWidgetOptions object, return a new object with only
* the public options that should be exposed to the client.
*/
function getOrdererPublicWidgetOptions(
options: PerseusOrdererWidgetOptions,
): OrdererPublicWidgetOptions {
return {
options: options.options,
height: options.height,
layout: options.layout,
};
}

export default getOrdererPublicWidgetOptions;
4 changes: 3 additions & 1 deletion packages/perseus/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
AnalyticsEventHandlerFn,
Version,
WidgetOptionsUpgradeMap,
getOrdererPublicWidgetOptions,
} from "@khanacademy/perseus-core";
import type {LinterContextProps} from "@khanacademy/perseus-linter";
import type {
Expand Down Expand Up @@ -539,7 +540,8 @@ export type WidgetScorerFunction = (
* A union type of all the functions that provide public widget options.
*/
export type PublicWidgetOptionsFunction =
typeof getCategorizerPublicWidgetOptions;
| typeof getCategorizerPublicWidgetOptions
| typeof getOrdererPublicWidgetOptions;

export type WidgetExports<
T extends React.ComponentType<any> & Widget = React.ComponentType<any>,
Expand Down
3 changes: 2 additions & 1 deletion packages/perseus/src/widgets/orderer/orderer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @khanacademy/ts-no-error-suppressions */
/* eslint-disable @babel/no-invalid-this, react/no-unsafe */
import {Errors} from "@khanacademy/perseus-core";
import {Errors, getOrdererPublicWidgetOptions} from "@khanacademy/perseus-core";
import {linterContextDefault} from "@khanacademy/perseus-linter";
import {scoreOrderer, validateOrderer} from "@khanacademy/perseus-score";
import $ from "jquery";
Expand Down Expand Up @@ -787,4 +787,5 @@ export default {
// TODO(LEMS-2656): remove TS suppression
// @ts-expect-error: Type UserInput is not assignable to type PerseusOrdererUserInput
validator: validateOrderer,
getPublicWidgetOptions: getOrdererPublicWidgetOptions,
} satisfies WidgetExports<typeof Orderer>;

0 comments on commit b4b3a3d

Please sign in to comment.