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

Add PhET simulation content editor #1532

Merged
merged 16 commits into from
Aug 21, 2024
2 changes: 2 additions & 0 deletions .changeset/gold-knives-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 2 additions & 0 deletions .changeset/thin-lemons-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {render, screen} from "@testing-library/react";
import {userEvent as userEventLib} from "@testing-library/user-event";
import * as React from "react";

import PhetSimEditor from "../phet-sim-editor";

import type {UserEvent} from "@testing-library/user-event";

describe("phet-sim editor", () => {
let userEvent: UserEvent;
beforeEach(() => {
userEvent = userEventLib.setup({
advanceTimers: jest.advanceTimersByTime,
});
});

it("renders", async () => {
// Act
render(<PhetSimEditor onChange={() => {}} />);

// Assert
expect(screen.getByLabelText("URL")).toBeInTheDocument();
expect(screen.getByLabelText("Description")).toBeInTheDocument();
});

it("should be possible to change URL", async () => {
// Arrange
const onChangeMock = jest.fn();

// Act
render(<PhetSimEditor onChange={onChangeMock} />);
await userEvent.type(screen.getByLabelText("URL"), "h");

// Assert
expect(onChangeMock).toBeCalledWith({url: "h"});
});

it("should be possible to change Description", async () => {
// Arrange
const onChangeMock = jest.fn();

// Act
render(<PhetSimEditor onChange={onChangeMock} />);
await userEvent.type(screen.getByLabelText("Description"), "P");

// Assert
expect(onChangeMock).toBeCalledWith({description: "P"});
});
});
56 changes: 33 additions & 23 deletions packages/perseus-editor/src/widgets/phet-sim-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
/* eslint-disable @khanacademy/ts-no-error-suppressions */
/* eslint-disable react/sort-comp */
import {Changeable, EditorJsonify} from "@khanacademy/perseus";
import {LabeledTextField} from "@khanacademy/wonder-blocks-form";
import * as React from "react";

import BlurInput from "../components/blur-input";
import type {PerseusPhetSimWidgetOptions} from "@khanacademy/perseus/src/perseus-types";

Check failure on line 6 in packages/perseus-editor/src/widgets/phet-sim-editor.tsx

View workflow job for this annotation

GitHub Actions / Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x)

Import for monorepo package '@khanacademy/perseus' is internal

type PhetSimEditorProps = any;
type Options = {
url: PerseusPhetSimWidgetOptions["url"];
description: PerseusPhetSimWidgetOptions["description"];
};
aemandine marked this conversation as resolved.
Show resolved Hide resolved

class PhetSimEditor extends React.Component<PhetSimEditorProps> {
static propTypes = {
...Changeable.propTypes,
};

static widgetName = "phet-sim" as const;
type Props = Options & {
onChange: (arg1: {
url?: Props["url"];
description?: Props["description"];
}) => void;
};

static defaultProps: PhetSimEditorProps = {
class PhetSimEditor extends React.Component<Props> {
static defaultProps: Options = {
url: "",
description: "",
};

change: (arg1: any, arg2: any) => any = (...args) => {
return Changeable.change.apply(this, args);
};
static widgetName = "phet-sim" as const;

render(): React.ReactNode {
return (
<div>
<label>
URL:
<BlurInput
value={this.props.url}
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 1.
onChange={this.change("url")}
/>
</label>
<LabeledTextField
label={"URL"}
value={this.props.url}
onChange={(url: string) => this.props.onChange({url})}
/>
<br />
<LabeledTextField
label={"Description"}
value={this.props.description}
onChange={(description: string) =>
this.props.onChange({description})
}
/>
</div>
);
}

serialize: () => any = () => {
return EditorJsonify.serialize.call(this);
serialize: () => Options = () => {
aemandine marked this conversation as resolved.
Show resolved Hide resolved
return {
url: this.props.url,
description: this.props.description,
};

Check warning on line 52 in packages/perseus-editor/src/widgets/phet-sim-editor.tsx

View check run for this annotation

Codecov / codecov/patch

packages/perseus-editor/src/widgets/phet-sim-editor.tsx#L49-L52

Added lines #L49 - L52 were not covered by tests
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/perseus/src/widgets/phet-sim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,6 @@ export default {
displayName: "PhET Simulation",
widget: PhetSim,
// Let's not expose it to all content creators yet
hidden: true,
hidden: false,
aemandine marked this conversation as resolved.
Show resolved Hide resolved
isLintable: true,
} as WidgetExports<typeof PhetSim>;
Loading