-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...on-no-select-warning/menu-option-desktop-editor-open-selection-no-select-warning.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import MenuOptionDesktopEditorOpenSelectionNoSelectWarning from "./menu-option-desktop-editor-open-selection-no-select-warning"; | ||
|
||
describe("MenuOptionDesktopEditorOpenSelectionNoSelectWarning", () => { | ||
it("should render without crashing", () => { | ||
render(<MenuOptionDesktopEditorOpenSelectionNoSelectWarning select={[]} isReadOnly={false} />); | ||
}); | ||
|
||
it("should show error notification when trying to open editor without selecting anything", () => { | ||
render(<MenuOptionDesktopEditorOpenSelectionNoSelectWarning select={[]} isReadOnly={false} />); | ||
fireEvent.keyDown(window, { key: "e", ctrlKey: true }); | ||
expect(screen.getByText("select first")).toBeTruthy(); | ||
}); | ||
|
||
it("should not show error notification when select is not empty", () => { | ||
render( | ||
<MenuOptionDesktopEditorOpenSelectionNoSelectWarning select={["item"]} isReadOnly={false} /> | ||
); | ||
fireEvent.keyDown(window, { key: "e", ctrlKey: true }); | ||
expect(screen.queryByText("select first")).toBeNull(); | ||
}); | ||
|
||
it("should not show error notification when read-only mode is enabled", () => { | ||
render(<MenuOptionDesktopEditorOpenSelectionNoSelectWarning select={[]} isReadOnly={true} />); | ||
fireEvent.keyDown(window, { key: "e", ctrlKey: true }); | ||
expect(screen.queryByText("select first")).toBeNull(); | ||
}); | ||
|
||
it("should not show error notification when editor feature is disabled", () => { | ||
jest.spyOn(window, "fetch").mockImplementation(() => ({ | ||
json: async () => ({ openEditorEnabled: false }) | ||
})); | ||
render(<MenuOptionDesktopEditorOpenSelectionNoSelectWarning select={[]} isReadOnly={false} />); | ||
fireEvent.keyDown(window, { key: "e", ctrlKey: true }); | ||
expect(screen.queryByText("select first")).toBeNull(); | ||
}); | ||
|
||
it("should clear error notification on callback", () => { | ||
render(<MenuOptionDesktopEditorOpenSelectionNoSelectWarning select={[]} isReadOnly={false} />); | ||
fireEvent.keyDown(window, { key: "e", ctrlKey: true }); | ||
fireEvent.click(screen.getByText("Close")); // Assuming 'Close' is the text inside the notification's close button | ||
expect(screen.queryByText("select first")).not.toBeInTheDocument(); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...no-select-warning/menu-option-desktop-editor-open-selection-no-select-warning.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import MenuOptionDesktopEditorOpenSelectionNoSelectWarning from "./menu-option-desktop-editor-open-selection-no-select-warning"; | ||
|
||
export default { | ||
title: "components/molecules/menu-option-desktop-editor-open-selection-no-select-warning" | ||
}; | ||
|
||
export const Default = () => { | ||
return <MenuOptionDesktopEditorOpenSelectionNoSelectWarning select={[]} isReadOnly={false} />; | ||
}; |
23 changes: 18 additions & 5 deletions
23
...lection-no-select-warning/menu-option-desktop-editor-open-selection-no-select-warning.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,41 @@ | ||
import { memo } from "react"; | ||
import { memo, useState } from "react"; | ||
import useFetch from "../../../hooks/use-fetch"; | ||
import useHotKeys from "../../../hooks/use-keyboard/use-hotkeys"; | ||
import { IEnvFeatures } from "../../../interfaces/IEnvFeatures"; | ||
import { UrlQuery } from "../../../shared/url-query"; | ||
import Notification, { NotificationType } from "../../atoms/notification/notification"; | ||
|
||
interface IMenuOptionDesktopEditorOpenSelectionNoSelectWarningProps { | ||
select: string[]; | ||
isReadonly: boolean; | ||
isReadOnly: boolean; | ||
} | ||
|
||
const MenuOptionDesktopEditorOpenSelectionNoSelectWarning: React.FunctionComponent<IMenuOptionDesktopEditorOpenSelectionNoSelectWarningProps> = | ||
memo(({ select, isReadonly }) => { | ||
memo(({ select, isReadOnly }) => { | ||
// Check API to know if feature is needed! | ||
const featuresResult = useFetch(new UrlQuery().UrlApiFeaturesAppSettings(), "get"); | ||
const dataFeatures = featuresResult?.data as IEnvFeatures | undefined; | ||
|
||
// for showing a notification | ||
const [isError, setIsError] = useState(""); | ||
|
||
/** | ||
* Open editor with keys - command + e | ||
*/ | ||
useHotKeys({ key: "e", ctrlKeyOrMetaKey: true }, () => { | ||
if (dataFeatures?.openEditorEnabled !== true || isReadonly || select.length >= 1) return; | ||
if (dataFeatures?.openEditorEnabled !== true || isReadOnly || select.length >= 1) return; | ||
setIsError("select first"); | ||
}); | ||
|
||
return <></>; | ||
return ( | ||
<> | ||
{isError !== "" ? ( | ||
<Notification callback={() => setIsError("")} type={NotificationType.default}> | ||
{isError} | ||
</Notification> | ||
) : null} | ||
</> | ||
); | ||
}); | ||
|
||
export default MenuOptionDesktopEditorOpenSelectionNoSelectWarning; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters