From 5674fdf2528761dc6bd31c55a9afe5320949e50d Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 22 Feb 2024 18:32:23 +0100 Subject: [PATCH] add tests --- ...ion-desktop-editor-open-selection.spec.tsx | 91 ++++++++++++++++++- ...u-option-desktop-editor-open-selection.tsx | 4 +- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.spec.tsx b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.spec.tsx index b702215490..df873a1702 100644 --- a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.spec.tsx +++ b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.spec.tsx @@ -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 = Promise.resolve({ + data: true, + statusCode: 200 + } as IConnectionDefault); + + const mockIConnectionDefaultErrorStatus: Promise = 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( + + ); + + 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, @@ -280,7 +361,11 @@ describe("ModalDesktopEditorOpenConfirmation", () => { .mockImplementationOnce(() => mockIConnectionDefaultResolve); const component = render( - + ); expect(useFetchSpy).toHaveBeenCalled(); @@ -289,6 +374,10 @@ describe("ModalDesktopEditorOpenConfirmation", () => { expect(fetchPostSpy).toHaveBeenCalled(); expect(fetchPostSpy).toHaveBeenCalledTimes(1); + expect(fetchPostSpy).toHaveBeenLastCalledWith( + new UrlQuery().UrlApiDesktopEditorOpenAmountConfirmationChecker(), + "f=%2Ffile1.jpg" + ); component.unmount(); }); diff --git a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.tsx b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.tsx index 779c05f694..4d07163ab6 100644 --- a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.tsx +++ b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-selection/menu-option-desktop-editor-open-selection.tsx @@ -53,7 +53,7 @@ export async function StartMenuOptionDesktopEditorOpenSelection( setModalConfirmationOpenFiles: (value: React.SetStateAction) => 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(); @@ -110,7 +110,7 @@ const MenuOptionDesktopEditorOpenSelection: React.FunctionComponent - {/* Modal move folder to trash */} + {/* Modal confirmation for open many files at one */} {modalConfirmationOpenFiles ? ( {