Skip to content

Commit

Permalink
Add PhET widget code coverage (#1620)
Browse files Browse the repository at this point in the history
## Summary:
Add new tests to ensure maximal code coverage of the PhET widget. This is based on the new code from the [a11y update, PR #1619](#1619) and should be merged in after the a11y update is merged in.
* Check that fullscreen button renders when the simulation loads successfully
* Check that fullscreen button does not render when the simulation fails to load
* Check that the locale warning banner renders when the `kaLocale` is not available for the requested PhET simulation

Issue: LEMS-2277

## Test plan:
* `yarn jest packages/perseus/src/widgets/phet-simulation/phet-simulation.test.ts`

Author: aemandine

Reviewers: SonicScrewdriver

Required Reviewers:

Approved By: SonicScrewdriver

Checks: ❌ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1620
  • Loading branch information
aemandine authored Sep 13, 2024
1 parent ab7b0fd commit dbac5d4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-eels-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Add code coverage
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ describe("phet-simulation widget", () => {
"https://phet.colorado.edu/sims/html/projectile-data-lab/latest/projectile-data-lab_all.html?locale=en",
);
});
// Fullscreen button should be available when the simulation renders successfully
await waitFor(() => {
expect(
screen.getByRole("button", {name: "Fullscreen"}),
).toBeInTheDocument();
});
});

it("should display an error for a non-PhET URL", async () => {
it("should display an error and hide fullscreen button for a non-PhET URL", async () => {
// Arrange
const apiOptions: APIOptions = {
isMobile: false,
Expand All @@ -57,7 +63,48 @@ describe("phet-simulation widget", () => {
await waitFor(() => {
expect(
screen.getByText("Sorry, this simulation cannot load."),
).toBeDefined();
).toBeInTheDocument();
});
// Fullscreen button should not be in the document when the simulation cannot load
await waitFor(() => {
expect(
screen.queryByRole("button", {name: "Fullscreen"}),
).not.toBeInTheDocument();
});
});

it("should display a locale warning for unavailable locale for PhET", async () => {
// Arrange
const apiOptions: APIOptions = {
isMobile: false,
};
jest.spyOn(Dependencies, "getDependencies").mockReturnValue({
...testDependencies,
kaLocale: "zz",
});

// Act
renderQuestion(question1, apiOptions);

// Assert
await waitFor(() => {
expect(
screen.getByText(
"Sorry, this simulation isn't available in your language.",
),
).toBeInTheDocument();
});
await waitFor(() => {
expect(screen.queryByTitle("Projectile Data Lab")).toHaveAttribute(
"src",
"https://phet.colorado.edu/sims/html/projectile-data-lab/latest/projectile-data-lab_all.html?locale=zz",
);
});
// Fullscreen button should be available when the simulation renders successfully
await waitFor(() => {
expect(
screen.getByRole("button", {name: "Fullscreen"}),
).toBeInTheDocument();
});
});

Expand Down

0 comments on commit dbac5d4

Please sign in to comment.