Skip to content

Commit

Permalink
#1866 add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Dec 17, 2024
1 parent 1febfa3 commit 2f43ab7
Showing 1 changed file with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createEvent, fireEvent, render, screen } from "@testing-library/react";
import { act } from "react";
import React, { act } from "react";
import { Orientation } from "../../../interfaces/IFileIndexItem";
import { OnMoveMouseTouchAction } from "./on-move-mouse-touch-action";
import { OnWheelMouseAction } from "./on-wheel-mouse-action";
Expand Down Expand Up @@ -298,5 +298,99 @@ describe("PanAndZoomImage", () => {

component.unmount();
});

it("should call setIsLoading with false when image is not loaded", () => {
const setIsLoadingMock = jest.fn();

// Mock the querySelector to return an image element that is not loaded
const containerRefMock = {
current: {
querySelector: jest.fn().mockReturnValue({
complete: true,
naturalHeight: 0
})
}
};

jest.spyOn(React, "useRef").mockReturnValueOnce(containerRefMock);

render(
<PanAndZoomImage
src="test.jpg"
translateRotation={Orientation.Horizontal}
onWheelCallback={jest.fn()}
onResetCallback={jest.fn()}
setIsLoading={setIsLoadingMock}
id="test-id"
/>
);

// Verify that setIsLoading was called with true
expect(setIsLoadingMock).toHaveBeenCalledWith(true);
});

it("should call setIsLoading with true when image is not loaded", () => {
const setIsLoadingMock = jest.fn();

// Mock the querySelector to return an image element that is not loaded
const containerRefMock = {
current: {
querySelector: jest.fn().mockReturnValue({
complete: false,
naturalHeight: 0
})
}
};

jest.spyOn(React, "useRef").mockReturnValueOnce(containerRefMock);

render(
<PanAndZoomImage
src="test.jpg"
translateRotation={Orientation.Horizontal}
onWheelCallback={jest.fn()}
onResetCallback={jest.fn()}
setIsLoading={setIsLoadingMock}
id="test-id"
/>
);

// Verify that setIsLoading was called with true
expect(setIsLoadingMock).toHaveBeenCalledWith(true);
});

it("should call touchMove on touch move event", async () => {
const spyOn = jest
.spyOn(OnMoveMouseTouchAction.prototype, "touchMove")
.mockImplementationOnce(() => {});

const component = render(
<PanAndZoomImage
src="test.jpg"
translateRotation={Orientation.Horizontal}
onWheelCallback={jest.fn()}
onResetCallback={jest.fn()}
setIsLoading={jest.fn()}
id="test-id"
/>
);

const touchEvent = new TouchEvent("touchmove", {
touches: [
{
identifier: 1,
target: component.container,
clientX: 500,
clientY: 500
} as unknown as Touch
]
});

window?.dispatchEvent(touchEvent);

expect(spyOn).toHaveBeenCalled();

component.unmount();
});
});
});

0 comments on commit 2f43ab7

Please sign in to comment.