Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 21, 2024
1 parent b482cde commit a005be8
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 61 deletions.
2 changes: 1 addition & 1 deletion starsky/starsky/clientapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"projectRoot": "../../"
}
],
"json",
"html",
"cobertura"
],
"coverageThreshold": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import * as useFetch from "../../../hooks/use-fetch";
import * as useLocation from "../../../hooks/use-location/use-location";
import { IArchiveProps } from "../../../interfaces/IArchiveProps";
import { IConnectionDefault } from "../../../interfaces/IConnectionDefault";
import { PageType, newIRelativeObjects } from "../../../interfaces/IDetailView";
import { IEnvFeatures } from "../../../interfaces/IEnvFeatures";
import { IExifStatus } from "../../../interfaces/IExifStatus";
import * as FetchPost from "../../../shared/fetch/fetch-post";
import { StartMenuOptionDesktopEditorOpenSelection } from "./menu-option-desktop-editor-open-selection";
import { UrlQuery } from "../../../shared/url-query";
import * as Notification from "../../atoms/notification/notification";
import * as ModalDesktopEditorOpenSelectionConfirmation from "../../organisms/modal-desktop-editor-open-selection-confirmation/modal-desktop-editor-open-selection-confirmation";
import MenuOptionDesktopEditorOpenSelection, {
StartMenuOptionDesktopEditorOpenSelection
} from "./menu-option-desktop-editor-open-selection";

describe("ModalDesktopEditorOpenConfirmation", () => {
const state = {
fileIndexItems: [
{
filePath: "/file2.jpg",
fileName: "file1",
fileCollectionName: "1",
fileName: "file1.jpg",
fileCollectionName: "file1",
fileHash: "1",
parentDirectory: "/",
status: IExifStatus.Ok
},
{
filePath: "/file2.jpg",
fileName: "file2",
fileCollectionName: "1",
fileName: "file2.jpg",
fileCollectionName: "file1",
fileHash: "1",
parentDirectory: "/",
status: IExifStatus.Ok
Expand All @@ -36,61 +45,277 @@ describe("ModalDesktopEditorOpenConfirmation", () => {
dateCache: 1
} as IArchiveProps;

it("sets modal confirmation open files if openWithoutConformationResult is false", async () => {
const select = ["file1", "file2"];
const collections = false;

const setIsError = jest.fn();
const messageDesktopEditorUnableToOpen = "Unable to open desktop editor";
const setModalConfirmationOpenFiles = jest.fn();

const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: false,
statusCode: 200
} as IConnectionDefault);

jest.spyOn(FetchPost, "default").mockImplementationOnce(() => mockIConnectionDefaultResolve);

await StartMenuOptionDesktopEditorOpenSelection(
select,
collections,
state,
setIsError,
messageDesktopEditorUnableToOpen,
setModalConfirmationOpenFiles
);
expect(setModalConfirmationOpenFiles).toHaveBeenCalledWith(true);
expect(setIsError).not.toHaveBeenCalled(); // Error should not be set in this case
describe("default function", () => {
it("renders without crashing", () => {
render(
<MenuOptionDesktopEditorOpenSelection
isReadOnly={true}
select={[]}
state={state}
setEnableMoreMenu={() => {}}
/>
);
});

it("calls StartMenuOptionDesktopEditorOpenSelection on hotkey trigger", async () => {
const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: true,
statusCode: 200
} as IConnectionDefault);

const fetchPostSpy = jest
.spyOn(FetchPost, "default")
.mockReset()
.mockImplementationOnce(() => mockIConnectionDefaultResolve)
.mockImplementationOnce(() => mockIConnectionDefaultResolve);

const component = render(
<MenuOptionDesktopEditorOpenSelection
isReadOnly={true}
select={["file1.jpg", "file2.jpg"]}
state={state}
setEnableMoreMenu={() => {}}
/>
);
fireEvent.keyDown(document.body, { key: "e", ctrlKey: true });

expect(fetchPostSpy).toHaveBeenCalled();
expect(fetchPostSpy).toHaveBeenCalledTimes(1);
expect(fetchPostSpy).toHaveBeenNthCalledWith(
1,
new UrlQuery().UrlApiDesktopEditorOpenAmountConfirmationChecker(),
"f=%2Ffile1.jpg%3B%2Ffile2.jpg"
);

await waitFor(() => {
expect(fetchPostSpy).toHaveBeenCalledTimes(2);
expect(fetchPostSpy).toHaveBeenNthCalledWith(
2,
new UrlQuery().UrlApiDesktopEditorOpen(),
"f=%2Ffile1.jpg%3B%2Ffile2.jpg&collections=true"
);
component.unmount();
});
});

it("renders ModalDesktopEditorOpenSelectionConfirmation and open modal due FetchPost false", async () => {
const mockGetIConnectionDefaultFeatureToggle = {
statusCode: 200,
data: {
openEditorEnabled: true
} as IEnvFeatures
} as IConnectionDefault;

const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: false,
statusCode: 200
} as IConnectionDefault);

const modalSpy = jest
.spyOn(ModalDesktopEditorOpenSelectionConfirmation, "default")
.mockImplementationOnce(() => <></>);

const useLocationFunction = () => {
return {
location: {
search: "?f=test1.jpg"
} as unknown as Location,
navigate: jest.fn()
};
};

jest
.spyOn(useLocation, "default")
.mockImplementationOnce(useLocationFunction)
.mockImplementationOnce(useLocationFunction);

jest.spyOn(Notification, "default").mockImplementationOnce(() => <></>);

const useFetchSpy = jest
.spyOn(useFetch, "default")
.mockImplementationOnce(() => mockGetIConnectionDefaultFeatureToggle)
.mockImplementationOnce(() => mockGetIConnectionDefaultFeatureToggle);

const fetchPostSpy = jest
.spyOn(FetchPost, "default")
.mockReset()
.mockImplementationOnce(() => mockIConnectionDefaultResolve)
.mockImplementationOnce(() => mockIConnectionDefaultResolve);

const component = render(
<MenuOptionDesktopEditorOpenSelection
state={state}
select={["file1.jpg"]}
isReadOnly={false}
/>
);

expect(useFetchSpy).toHaveBeenCalled();

fireEvent.click(screen.getByTestId("menu-option-desktop-editor-open"));

expect(fetchPostSpy).toHaveBeenCalled();
expect(fetchPostSpy).toHaveBeenCalledTimes(1);
expect(fetchPostSpy).toHaveBeenNthCalledWith(
1,
new UrlQuery().UrlApiDesktopEditorOpenAmountConfirmationChecker(),
"f=%2Ffile1.jpg"
);

await waitFor(() => {
expect(modalSpy).toHaveBeenCalled();

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

it("renders ModalDesktopEditorOpenSelectionConfirmation and close due FetchPost true", async () => {
const mockGetIConnectionDefaultFeatureToggle = {
statusCode: 200,
data: {
openEditorEnabled: true
} as IEnvFeatures
} as IConnectionDefault;

const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: true,
statusCode: 200
} as IConnectionDefault);

const useLocationFunction = () => {
return {
location: window.location,
navigate: jest.fn()
};
};

jest
.spyOn(useLocation, "default")
.mockImplementationOnce(useLocationFunction)
.mockImplementationOnce(useLocationFunction);

jest.spyOn(Notification, "default").mockImplementationOnce(() => <></>);

const useFetchSpy = jest
.spyOn(useFetch, "default")
.mockImplementationOnce(() => mockGetIConnectionDefaultFeatureToggle)
.mockImplementationOnce(() => mockGetIConnectionDefaultFeatureToggle);

const fetchPostSpy = jest
.spyOn(FetchPost, "default")
.mockReset()
.mockImplementationOnce(() => mockIConnectionDefaultResolve)
.mockImplementationOnce(() => mockIConnectionDefaultResolve);

const component = render(
<MenuOptionDesktopEditorOpenSelection state={state} select={["1"]} isReadOnly={false} />
);

expect(useFetchSpy).toHaveBeenCalled();

fireEvent.click(screen.getByTestId("menu-option-desktop-editor-open"));

expect(fetchPostSpy).toHaveBeenCalled();
expect(fetchPostSpy).toHaveBeenCalledTimes(1);

component.unmount();
});
});
xit("calls openDesktop if openWithoutConformationResult is true", async () => {
const select = ["file1", "file2"];
const collections = false;
const setIsError = jest.fn();
const messageDesktopEditorUnableToOpen = "Unable to open desktop editor";
const setModalConfirmationOpenFiles = jest.fn();
const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: false,
statusCode: 200
} as IConnectionDefault);

jest.spyOn(FetchPost, "default").mockImplementationOnce(() => mockIConnectionDefaultResolve);

await StartMenuOptionDesktopEditorOpenSelection(
select,
collections,
state,
setIsError,
messageDesktopEditorUnableToOpen,
setModalConfirmationOpenFiles
);
expect(setModalConfirmationOpenFiles).not.toHaveBeenCalled(); // Modal confirmation should not be set in this case
expect(setIsError).not.toHaveBeenCalled(); // Error should not be set in this case
// expect(openDesktop).toHaveBeenCalledWith(
// select,
// collections,
// state,
// setIsError,
// messageDesktopEditorUnableToOpen
// );

describe("StartMenuOptionDesktopEditorOpenSelection", () => {
it("sets modal confirmation open files if openWithoutConformationResult is false", async () => {
const select = ["file1", "file2"];
const collections = false;

const setIsError = jest.fn();
const messageDesktopEditorUnableToOpen = "[for example] Unable to open desktop editor";
const setModalConfirmationOpenFiles = jest.fn();

const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: false,
statusCode: 200
} as IConnectionDefault);

jest.spyOn(FetchPost, "default").mockImplementationOnce(() => mockIConnectionDefaultResolve);

await StartMenuOptionDesktopEditorOpenSelection(
select,
collections,
state,
setIsError,
messageDesktopEditorUnableToOpen,
setModalConfirmationOpenFiles
);
expect(setModalConfirmationOpenFiles).toHaveBeenCalledWith(true);
expect(setIsError).not.toHaveBeenCalled(); // Error should not be set in this case
});

it("calls openDesktop if openWithoutConformationResult is true and open succceed", async () => {
const select = ["file1.jpg", "file2.jpg"];
const collections = false;
const setIsError = jest.fn();
const messageDesktopEditorUnableToOpen = "[for example] Unable to open desktop editor";
const setModalConfirmationOpenFiles = jest.fn();
const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: true,
statusCode: 200
} as IConnectionDefault);

const fetchPostSpy = jest
.spyOn(FetchPost, "default")
.mockReset()
.mockImplementationOnce(() => mockIConnectionDefaultResolve)
.mockImplementationOnce(() => mockIConnectionDefaultResolve);

await StartMenuOptionDesktopEditorOpenSelection(
select,
collections,
state,
setIsError,
messageDesktopEditorUnableToOpen,
setModalConfirmationOpenFiles
);
expect(setModalConfirmationOpenFiles).not.toHaveBeenCalled(); // Modal confirmation should not be set in this case
expect(setIsError).not.toHaveBeenCalled(); // Error should not be set in this case
expect(fetchPostSpy).toHaveBeenCalledTimes(2);
expect(fetchPostSpy).toHaveBeenLastCalledWith(
new UrlQuery().UrlApiDesktopEditorOpen(),
"f=%2Ffile1.jpg%3B%2Ffile2.jpg&collections=false"
);
});

it("calls openDesktop if openWithoutConformationResult is true but open failed", async () => {
const select = ["file1", "file2"];
const collections = false;
const setIsError = jest.fn();
const messageDesktopEditorUnableToOpen = "[for example] Unable to open desktop editor";
const setModalConfirmationOpenFiles = jest.fn();
const mockIConnectionDefaultResolve: Promise<IConnectionDefault> = Promise.resolve({
data: true,
statusCode: 200
} as IConnectionDefault);

const mockIConnectionDefaultFailed: Promise<IConnectionDefault> = Promise.resolve({
data: true,
statusCode: 300
} as IConnectionDefault);

jest
.spyOn(FetchPost, "default")
.mockReset()
.mockImplementationOnce(() => mockIConnectionDefaultResolve)
.mockImplementationOnce(() => mockIConnectionDefaultFailed);

await StartMenuOptionDesktopEditorOpenSelection(
select,
collections,
state,
setIsError,
messageDesktopEditorUnableToOpen,
setModalConfirmationOpenFiles
);
expect(setModalConfirmationOpenFiles).not.toHaveBeenCalled(); // Modal confirmation should not be set in this case
expect(setIsError).toHaveBeenCalled();
});
});
});

0 comments on commit a005be8

Please sign in to comment.