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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-hairs-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Refactor cs-program to use a getUserInputFunction
16 changes: 12 additions & 4 deletions packages/perseus/src/widgets/cs-program/cs-program.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class CSProgram extends React.Component<Props> {
return csProgramValidator(state);
}

static getUserInputFromProps(props: Props): PerseusCSProgramUserInput {
return {
status: props.status,
message: props.message,
};
}

componentDidMount() {
$(window).on("message", this.handleMessageEvent);
}
Expand Down Expand Up @@ -108,11 +115,12 @@ class CSProgram extends React.Component<Props> {
return Changeable.change.apply(this, args);
};

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.


simpleValidate(): PerseusScore {
return csProgramValidator({
status: this.props.status,
message: this.props.message,
});
return csProgramValidator(this.getUserInput());
}

render(): React.ReactNode {
Expand Down