Skip to content

Commit

Permalink
add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Nov 7, 2023
1 parent ed1c3c0 commit 113e32d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from "@testing-library/react";
import { fireEvent, render, screen } from "@testing-library/react";
import { act } from "react-dom/test-utils";
import * as useLocation from "../../../hooks/use-location/use-location";
import { newIArchive } from "../../../interfaces/IArchive";
Expand Down Expand Up @@ -32,7 +32,7 @@ describe("MenuOptionMoveToTrash", () => {
});

describe("context", () => {
it("check if dispatch", async () => {
it("check if dispatch when click", async () => {
jest.spyOn(FetchPost, "default").mockReset();
const test = {
...newIArchive(),
Expand Down Expand Up @@ -82,6 +82,107 @@ describe("MenuOptionMoveToTrash", () => {
component.unmount();
});

it("check if dispatch when keyDown enter", async () => {
jest.spyOn(FetchPost, "default").mockReset();
const test = {
...newIArchive(),
fileIndexItems: [
{
...newIFileIndexItem(),
parentDirectory: "/",
fileName: "test.jpg"
} as IFileIndexItem
]
} as IArchiveProps;

const mockIConnectionDefault: Promise<IConnectionDefault> =
Promise.resolve({
...newIConnectionDefault(),
data: null,
statusCode: 200
});
const fetchPostSpy = jest
.spyOn(FetchPost, "default")
.mockImplementationOnce(() => mockIConnectionDefault);

const dispatch = jest.fn();
const component = await render(
<MenuOptionMoveToTrash
setSelect={jest.fn()}
select={["test.jpg"]}
isReadOnly={false}
state={test}
dispatch={dispatch}
/>
);

const trashButton = screen.queryByTestId("trash") as HTMLButtonElement;
expect(trashButton).toBeTruthy();

await act(async () => {
await fireEvent.keyDown(trashButton, {
key: "Enter"
});
});

expect(fetchPostSpy).toBeCalled();
expect(dispatch).toBeCalled();
expect(dispatch).toBeCalledWith({
toRemoveFileList: ["/test.jpg"],
type: "remove"
});
component.unmount();
});

it("check if dispatch when keyDown tab so skip", async () => {
jest.spyOn(FetchPost, "default").mockReset();
const test = {
...newIArchive(),
fileIndexItems: [
{
...newIFileIndexItem(),
parentDirectory: "/",
fileName: "test.jpg"
} as IFileIndexItem
]
} as IArchiveProps;

const mockIConnectionDefault: Promise<IConnectionDefault> =
Promise.resolve({
...newIConnectionDefault(),
data: null,
statusCode: 200
});
const fetchPostSpy = jest
.spyOn(FetchPost, "default")
.mockImplementationOnce(() => mockIConnectionDefault);

const dispatch = jest.fn();
const component = await render(
<MenuOptionMoveToTrash
setSelect={jest.fn()}
select={["test.jpg"]}
isReadOnly={false}
state={test}
dispatch={dispatch}
/>
);

const trashButton = screen.queryByTestId("trash") as HTMLButtonElement;
expect(trashButton).toBeTruthy();

await act(async () => {
await fireEvent.keyDown(trashButton, {
key: "Tab"
});
});

expect(fetchPostSpy).toBeCalledTimes(0);
expect(dispatch).toBeCalledTimes(0);

component.unmount();
});

it("check if not dispatch when error", () => {
console.log("-- check if not dispatch when error");

Expand Down Expand Up @@ -178,6 +279,7 @@ describe("MenuOptionMoveToTrash", () => {
component.unmount();
});
});

describe("context 2", () => {
it("check if when pressing Delete key", () => {
console.log("-- check if when pressing Delete key");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,9 @@ describe("MenuArchive", () => {
return <></>;
});

jest.spyOn(React, "useContext").mockImplementationOnce(() => {
return contextValues;
});
jest
.spyOn(React, "useContext")
.mockImplementationOnce(() => contextValues);

const component = render(<MenuArchive />);

Expand Down

0 comments on commit 113e32d

Please sign in to comment.