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
6 changes: 6 additions & 0 deletions .changeset/three-ligers-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/perseus": minor
"@khanacademy/perseus-editor": minor
---

Add a content editor for the PhET widget
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {Dependencies} from "@khanacademy/perseus";
import {render, screen} from "@testing-library/react";
import {userEvent as userEventLib} from "@testing-library/user-event";
import * as React from "react";

import {testDependencies} from "../../../../../testing/test-dependencies";
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,
});

jest.spyOn(Dependencies, "getDependencies").mockReturnValue(
testDependencies,
);
});

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"});
});
});
58 changes: 34 additions & 24 deletions packages/perseus-editor/src/widgets/phet-sim-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
/* 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";

type PhetSimEditorProps = any;
type DefaultProps = {
url: PerseusPhetSimWidgetOptions["url"];
description: PerseusPhetSimWidgetOptions["description"];
};

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

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

static defaultProps: PhetSimEditorProps = {
class PhetSimEditor extends React.Component<Props> {
static defaultProps: DefaultProps = {
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(): PerseusPhetSimWidgetOptions {
return {
url: this.props.url,
description: this.props.description,
};
}

Check warning on line 53 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-L53

Added lines #L49 - L53 were not covered by tests
}

export default PhetSimEditor;
1 change: 1 addition & 0 deletions packages/perseus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export type {
PerseusInputNumberWidgetOptions,
PerseusInteractiveGraphWidgetOptions,
PerseusItem,
PerseusPhetSimWidgetOptions,
PerseusPlotterWidgetOptions,
PerseusPythonProgramWidgetOptions,
PerseusRadioWidgetOptions,
Expand Down
2 changes: 0 additions & 2 deletions packages/perseus/src/perseus-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1561,8 +1561,6 @@ export type PerseusPhetSimWidgetOptions = {
url: string;
// Translatable Text; Description of the sim for Khanmigo and alt text
description: string;
// Always false
static: boolean;
};

export type PerseusVideoWidgetOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const question1: PerseusRenderer = {
options: {
url: "https://phet.colorado.edu/sims/html/projectile-data-lab/latest/projectile-data-lab_all.html",
description: "Projectile Data Lab",
static: false,
},
alignment: "default",
},
Expand All @@ -32,7 +31,6 @@ export const nonPhetUrl: PerseusRenderer = {
options: {
url: "https://google.com/",
description: "Google",
static: false,
},
alignment: "default",
},
Expand Down