Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 22, 2024
1 parent f534292 commit 5674fdf
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,87 @@ describe("ModalDesktopEditorOpenConfirmation", () => {
});
});

it("ModalDesktopEditorOpenSelectionConfirmation and FetchPost error status Notification", 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 mockIConnectionDefaultErrorStatus: Promise<IConnectionDefault> = Promise.resolve({
data: null, // FAIL:!
statusCode: 500
} as IConnectionDefault);

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

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

const notificationSpy = 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(() => mockIConnectionDefaultErrorStatus);

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(fetchPostSpy).toHaveBeenCalledTimes(2);
expect(fetchPostSpy).toHaveBeenNthCalledWith(
2,
new UrlQuery().UrlApiDesktopEditorOpen(),
"f=%2Ffile1.jpg&collections=true"
);
// Failure
expect(notificationSpy).toHaveBeenCalled();

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

it("ModalDesktopEditorOpenSelectionConfirmation and close due FetchPost true", async () => {
const mockGetIConnectionDefaultFeatureToggle = {
statusCode: 200,
Expand Down Expand Up @@ -280,7 +361,11 @@ describe("ModalDesktopEditorOpenConfirmation", () => {
.mockImplementationOnce(() => mockIConnectionDefaultResolve);

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

expect(useFetchSpy).toHaveBeenCalled();
Expand All @@ -289,6 +374,10 @@ describe("ModalDesktopEditorOpenConfirmation", () => {

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

component.unmount();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function StartMenuOptionDesktopEditorOpenSelection(
setModalConfirmationOpenFiles: (value: React.SetStateAction<boolean>) => void
) {
const toDesktopOpenList = new URLPath().MergeSelectFileIndexItem(select, state.fileIndexItems);
if (!toDesktopOpenList) return;
if (!toDesktopOpenList || toDesktopOpenList.length === 0) return;
const selectParams = new URLPath().ArrayToCommaSeparatedStringOneParent(toDesktopOpenList, "");
const urlCheck = new UrlQuery().UrlApiDesktopEditorOpenAmountConfirmationChecker();

Expand Down Expand Up @@ -110,7 +110,7 @@ const MenuOptionDesktopEditorOpenSelection: React.FunctionComponent<IMenuOptionD

return (
<>
{/* Modal move folder to trash */}
{/* Modal confirmation for open many files at one */}
{modalConfirmationOpenFiles ? (
<ModalDesktopEditorOpenSelectionConfirmation
handleExit={() => {
Expand Down

0 comments on commit 5674fdf

Please sign in to comment.