Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the cs-program widget to use a getUserInput function #1697

Merged
merged 9 commits into from
Oct 1, 2024

Conversation

Myranae
Copy link
Contributor

@Myranae Myranae commented Sep 30, 2024

Summary:

The cs-program widget previously did not have a getUserInput function. This function is necessary as we split the scoring logic out of the renderer system, so this adds and integrates the function.

Issue: LEMS-2459

Test plan:

  • Confirm all tests pass
  • Confirm stories still work with an npm snapshot (cs-program doesn't seem to work in localhost)

@Myranae Myranae self-assigned this Sep 30, 2024
Copy link
Contributor

github-actions bot commented Sep 30, 2024

Size Change: +4 B (0%)

Total Size: 864 kB

Filename Size Change
packages/perseus/dist/es/index.js 418 kB +4 B (0%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38.3 kB
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/index.js 78 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 1.48 kB
packages/perseus-editor/dist/es/index.js 280 kB
packages/perseus-linter/dist/es/index.js 22.2 kB
packages/perseus/dist/es/strings.js 3.4 kB
packages/pure-markdown/dist/es/index.js 3.66 kB
packages/simple-markdown/dist/es/index.js 12.4 kB

compressed-size-action

Copy link
Contributor

github-actions bot commented Sep 30, 2024

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (6dea81a) and published it to npm. You
can install it using the tag PR1697.

Example:

yarn add @khanacademy/perseus@PR1697

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR1697

@Myranae Myranae marked this pull request as ready for review September 30, 2024 15:17
@khan-actions-bot khan-actions-bot requested a review from a team September 30, 2024 15:17
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Sep 30, 2024

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/big-hairs-speak.md, packages/perseus/src/widgets/cs-program/cs-program.test.ts, packages/perseus/src/widgets/cs-program/cs-program.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

Comment on lines 118 to 120
getUserInput(): PerseusCSProgramUserInput {
return CSProgram.getUserInputFromProps(this.props);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need the getUserInputFromProps method?

Suggested change
getUserInput(): PerseusCSProgramUserInput {
return CSProgram.getUserInputFromProps(this.props);
}
getUserInput(): PerseusCSProgramUserInput {
return {
status: this.props.status,
message: this.props.message,
};
}

Copy link
Contributor Author

@Myranae Myranae Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo, for sure. I think I was just following the framework for other widgets that use getUserInput; it seems like most of them use this method. Maybe leave this for now and have a ticket to go through and refactor out getUserInputFromProps from as many widgets as possible? I'm not sure I understand why they were there in the first place. Looks like the method might be used in tests 🤔

Copy link
Contributor Author

@Myranae Myranae Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so first I kept the method and set up a test with it. I was following the example from categorizer's test. But then I tried to use the example from group's test and realized that cs-program uses an iframe. Checking out iframe, it doesn't have a getUserInputFromProps method, and it does it the way you suggest here. So I just did it this way in the end XD Not sure how to add a test though since iframe doesn't have any tests for getUserInput either. Do you have any suggestions @handeyeco ? (Also, I just picked up the ticket to split out validation logic from iframe, so we'll see if doing that first gives ideas here)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't really know. Maybe something like:

// Arrange
const widgetRef: CSProgram | null = null;
render(<CSProgram ref={(ref) => widgetRef = ref} status="test status" message="test message />);

// Act
const result = widgetRef.getUserInput();

// Assert
expect(result.status).toBe("test status");
expect(result.message).toBe("test message");

I wouldn't lose sleep on it. We'll probably need to add tests to all the widgets for this and this could be a part of that process.

@handeyeco
Copy link
Contributor

Also I wonder if it'd be worth adding a test for this.

Comment on lines 42 to 71
it("should show default user input before user interaction", () => {
const apiOptions = {
isMobile: false,
} as const;

const {renderer} = renderQuestion(question1, apiOptions);
const userInput =
renderer.getUserInput()[0] as PerseusCSProgramUserInput;

expect(userInput.status).toBe("incomplete");
expect(userInput.message).toBe(null);
});

it("should snapshot default user input before user interaction", () => {
// Arrange
const {renderer} = renderQuestion(question1);

// Act
const userInput = renderer.getUserInput();

// Assert
expect(userInput).toMatchInlineSnapshot(`
[
{
"message": null,
"status": "incomplete",
},
]
`);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@handeyeco So I added two different versions of what seem to be possibly the same test. Do either of these seem valuable? I wasn't able to figure out how to get an updated message from the renderer, but we can test the default at least.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first one is useful. Thanks!

@Myranae Myranae merged commit b9d84cc into main Oct 1, 2024
9 checks passed
@Myranae Myranae deleted the tb/LEMS-2459/getUserInputFunc-for-cs-program branch October 1, 2024 20:25
jeremywiebe pushed a commit that referenced this pull request Oct 2, 2024
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.


# Releases
## @khanacademy/[email protected]

### Major Changes

-   [#1696](#1696) [`3e1697229`](3e16972) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Remove deprecated `Util.widgetShouldHighlight` function

### Minor Changes

-   [#1696](#1696) [`3e1697229`](3e16972) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Migrate `Widget` type to be an TypeScript `interface` so each widget can declare it implements it explicitly.

### Patch Changes

-   [#1697](#1697) [`b9d84ccba`](b9d84cc) Thanks [@Myranae](https://github.com/Myranae)! - Refactor cs-program to use a getUserInputFunction


-   [#1713](#1713) [`bcd32425c`](bcd3242) Thanks [@handeyeco](https://github.com/handeyeco)! - Some minor cleanup related to validators


-   [#1708](#1708) [`8e95e00c4`](8e95e00) Thanks [@Myranae](https://github.com/Myranae)! - Move validation logic out of the orderer widget and add tests

## @khanacademy/[email protected]

### Patch Changes

-   Updated dependencies \[[`b9d84ccba`](b9d84cc), [`bcd32425c`](bcd3242), [`8e95e00c4`](8e95e00), [`3e1697229`](3e16972), [`3e1697229`](3e16972)]:
    -   @khanacademy/[email protected]

Author: khan-actions-bot

Reviewers: jeremywiebe

Required Reviewers:

Approved By: jeremywiebe

Checks: ⏭️  Publish npm snapshot, ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1714
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants