Skip to content

Commit

Permalink
New tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicScrewdriver committed Dec 12, 2024
1 parent 5d8707c commit 346e7ed
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
97 changes: 88 additions & 9 deletions packages/perseus/src/widgets/image/image.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import {describe, beforeEach, it} from "@jest/globals";
import {act, screen, waitFor} from "@testing-library/react";
import userEvent from "@testing-library/user-event";

Check failure on line 3 in packages/perseus/src/widgets/image/image.test.ts

View workflow job for this annotation

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

Using exported name 'userEvent' as identifier for default export

import {testDependencies} from "../../../../../testing/test-dependencies";
import * as Dependencies from "../../dependencies";
import {scorePerseusItemTesting} from "../../util/test-utils";
import {isAccessible} from "../../widgets";
import {renderQuestion} from "../__testutils__/renderQuestion";

import {question} from "./image.testdata";
import {question, questionWithZoom} from "./image.testdata";

import type {APIOptions} from "../../types";

describe.each([true, false])("image widget - isMobile %b", (isMobile) => {
const apiOptions: APIOptions = {isMobile};
const images: Array<Record<any, any>> = [];
let originalImage;

Check failure on line 18 in packages/perseus/src/widgets/image/image.test.ts

View workflow job for this annotation

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

'originalImage' is assigned a value but never used. Allowed unused vars must match /^_*$/u

beforeEach(() => {
jest.clearAllMocks();
originalImage = window.Image;
// Mock HTML Image so we can trigger onLoad callbacks and see full
// image rendering.
// @ts-expect-error - TS2322 - Type 'Mock<Record<string, any>, [], any>' is not assignable to type 'new (width?: number | undefined, height?: number | undefined) => HTMLImageElement'.
window.Image = jest.fn(() => {
const img: Record<string, any> = {};
images.push(img);
return img;
});

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

// Mocked for loading graphie in svg-image
global.fetch = jest.fn(() =>
Promise.resolve({
text: () => "",
ok: true,
}),
) as jest.Mock;
});

it("should snapshot", () => {
Expand Down Expand Up @@ -71,4 +78,76 @@ describe.each([true, false])("image widget - isMobile %b", (isMobile) => {
// Act and Assert
expect(isAccessible(inaccessibleWidgetInfo)).toBe(false);
});

it("should zoom on click for applicable images", async () => {
// Arrange
const altText = questionWithZoom.widgets["image 1"].options.alt!;
renderQuestion(questionWithZoom, apiOptions);

// Tells the image loader 1, or all, of our images loaded
const markImagesAsLoaded = (imageIndex?: number) => {
if (imageIndex != null) {
const img = images[imageIndex];
if (img?.onload) {
act(() => img.onload());
}
} else {
images.forEach((i) => {
if (i?.onload) {
act(() => i.onload());
}
});
}
};

// Act
// Act
markImagesAsLoaded(); // Tell the ImageLoader that our images are loaded
await waitFor(async () => {
await screen.getByAltText(altText).focus();
screen.getByAltText(altText).click();
});

// Assert
// The image should have the zoomed class
await waitFor(() => {
expect(screen.getByTestId("zoomed-image")).toBeVisible();
});
});

it("should zoom on keyboard interaction for applicable images", async () => {
// Arrange
const altText = questionWithZoom.widgets["image 1"].options.alt!;
renderQuestion(questionWithZoom, apiOptions);

// Tells the image loader 1, or all, of our images loaded
const markImagesAsLoaded = (imageIndex?: number) => {
if (imageIndex != null) {
const img = images[imageIndex];
if (img?.onload) {
act(() => img.onload());
}
} else {
images.forEach((i) => {
if (i?.onload) {
act(() => i.onload());
}
});
}
};

// Act
// Act
markImagesAsLoaded(); // Tell the ImageLoader that our images are loaded
await waitFor(async () => {
await screen.getByAltText(altText).focus();
await userEvent.keyboard("{enter}");
});

// Assert
// The image should have the zoomed class
await waitFor(() => {
expect(screen.getByTestId("zoomed-image")).toBeVisible();
});
});
});
2 changes: 2 additions & 0 deletions packages/perseus/src/zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ Zoom.prototype.zoomImage = function () {

Zoom.prototype._zoomOriginal = function () {
this.$zoomedImage.addClass("zoom-img").attr("data-action", "zoom-out");

this.$zoomedImage.attr("data-test-id", "zoomed-image");
$(this._targetImage).css("visibility", "hidden");

this._backdrop = document.createElement("div");
Expand Down

0 comments on commit 346e7ed

Please sign in to comment.