From 3bc8c9f2e523794fd2f3094bf92cbb7ab50d6ef9 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 22 Feb 2024 20:39:34 +0100 Subject: [PATCH] add tests --- ...option-desktop-editor-open-single.spec.tsx | 44 ++++++++++++++++++- ...menu-option-desktop-editor-open-single.tsx | 5 ++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.spec.tsx b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.spec.tsx index 84c2466963..c1479096e9 100644 --- a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.spec.tsx +++ b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.spec.tsx @@ -5,7 +5,9 @@ import { IEnvFeatures } from "../../../interfaces/IEnvFeatures"; import * as FetchPost from "../../../shared/fetch/fetch-post"; import { UrlQuery } from "../../../shared/url-query"; import * as Notification from "../../atoms/notification/notification"; -import MenuOptionDesktopEditorOpenSingle from "./menu-option-desktop-editor-open-single"; +import MenuOptionDesktopEditorOpenSingle, { + OpenDesktopSingle +} from "./menu-option-desktop-editor-open-single"; describe("MenuOptionDesktopEditorOpenSingle", () => { it("should render without errors", () => { @@ -62,6 +64,41 @@ describe("MenuOptionDesktopEditorOpenSingle", () => { container.unmount(); }); + it("calls StartMenuOptionDesktopEditorOpenSelection on hotkey trigger", async () => { + const mockIConnectionDefaultResolve: Promise = Promise.resolve({ + data: true, + statusCode: 200 + } as IConnectionDefault); + + const fetchPostSpy = jest + .spyOn(FetchPost, "default") + .mockReset() + .mockImplementationOnce(() => mockIConnectionDefaultResolve) + .mockImplementationOnce(() => mockIConnectionDefaultResolve); + + const container = render( + + ); + + fireEvent.keyDown(document.body, { key: "e", ctrlKey: true }); + + await waitFor(() => { + expect(fetchPostSpy).toHaveBeenCalled(); + expect(fetchPostSpy).toHaveBeenCalledTimes(1); + + expect(fetchPostSpy).toHaveBeenNthCalledWith( + 1, + new UrlQuery().UrlApiDesktopEditorOpen(), + "f=%2Ftest.jpg&collections=false" + ); + container.unmount(); + }); + }); + it("feature toggle disabled", () => { const mockGetIConnectionDefaultFeatureToggle = { statusCode: 200, @@ -148,4 +185,9 @@ describe("MenuOptionDesktopEditorOpenSingle", () => { }); container.unmount(); }); + + it("OpenDesktopSingle readonly should skip", async () => { + const result = await OpenDesktopSingle("/", false, jest.fn(), "error", true); + expect(result).toBeFalsy(); + }); }); diff --git a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.tsx b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.tsx index 320ee8f63d..4ea7288709 100644 --- a/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.tsx +++ b/starsky/starsky/clientapp/src/components/molecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.tsx @@ -23,9 +23,9 @@ export async function OpenDesktopSingle( setIsError: React.Dispatch>, messageDesktopEditorUnableToOpen: string, isReadOnly: boolean -) { +): Promise { if (isReadOnly) { - return; + return false; } const urlOpen = new UrlQuery().UrlApiDesktopEditorOpen(); @@ -37,6 +37,7 @@ export async function OpenDesktopSingle( if (openDesktopResult.statusCode >= 300) { setIsError(messageDesktopEditorUnableToOpen); } + return true; } const MenuOptionDesktopEditorOpenSingle: React.FunctionComponent =