From d1c806e3b0489abbeff6c1d73a0da6e97c17ab70 Mon Sep 17 00:00:00 2001 From: yaswanth-deriv Date: Fri, 29 Mar 2024 12:43:51 +0400 Subject: [PATCH 1/2] test: added index file for importing hooks and made changes accordingly --- .../PageLayout/__test__/PageLayout.spec.tsx | 42 +++++++++++++++++++ src/components/PageLayout/index.tsx | 2 +- src/hooks/index.ts | 2 + src/main.ts | 4 +- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/components/PageLayout/__test__/PageLayout.spec.tsx create mode 100644 src/hooks/index.ts diff --git a/src/components/PageLayout/__test__/PageLayout.spec.tsx b/src/components/PageLayout/__test__/PageLayout.spec.tsx new file mode 100644 index 00000000..569b2573 --- /dev/null +++ b/src/components/PageLayout/__test__/PageLayout.spec.tsx @@ -0,0 +1,42 @@ +import React from "react"; +import { render, screen } from "@testing-library/react"; +import { PageLayout } from ".."; +import * as hooks from "../../../hooks" + +// Mocking the useDevice hook +jest.mock("../../../hooks", () => ({ + useDevice: jest.fn().mockReturnValue({isMobile:false}), +})); + +describe("PageLayout Component", () => { + it("renders children correctly", () => { + render( + +
Content
+
+ ); + const content =screen.getByText("Content") + expect(content).toBeInTheDocument(); + }); + + it("renders sidebar when provided and not on mobile", () => { + const sidebar =
Sidebar
; + render(); + const sidebarContent =screen.getByText("Sidebar") + expect(sidebarContent).toBeInTheDocument(); + }); + + it("does not render sidebar on mobile", () => { + jest.spyOn(hooks,'useDevice').mockImplementation(()=>({isMobile:true, isDesktop:false, isTablet:false})) + const sidebar =
Sidebar
; + render(); + const sidebarContent =screen.queryByText("Sidebar") + expect(sidebarContent).not.toBeInTheDocument() + }); + + it("does not render sidebar when not provided", () => { + render(); + const sidebarContent=screen.queryByTestId("sidebar") + expect(sidebarContent).toBeNull(); + }); +}); diff --git a/src/components/PageLayout/index.tsx b/src/components/PageLayout/index.tsx index e7ab14c7..9d24bded 100644 --- a/src/components/PageLayout/index.tsx +++ b/src/components/PageLayout/index.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { useDevice } from "../../hooks/useDevice"; +import { useDevice } from "../../hooks"; import "./PageLayout.scss"; type PageLayoutProps = { diff --git a/src/hooks/index.ts b/src/hooks/index.ts new file mode 100644 index 00000000..68559283 --- /dev/null +++ b/src/hooks/index.ts @@ -0,0 +1,2 @@ +export {useDevice} from './useDevice' +export {useOnClickOutside} from './useOnClickOutside' diff --git a/src/main.ts b/src/main.ts index 575e1fde..ff4660e4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,6 +19,6 @@ export { Text } from "./components/Text"; export { TextArea } from "./components/TextArea"; export { ToggleSwitch } from "./components/ToggleSwitch"; export { Tooltip } from "./components/Tooltip"; -export { useDevice } from "./hooks/useDevice"; -export { useOnClickOutside } from "./hooks/useOnClickOutside"; +export { useDevice,useOnClickOutside } from "./hooks"; +// export { useOnClickOutside } from "./hooks/useOnClickOutside"; export { VerticalTab, VerticalTabItems } from "./components/VerticalTab"; From c27d513e8be42d405b3215aaab3d4f6b3f482aa8 Mon Sep 17 00:00:00 2001 From: yaswanth-deriv Date: Fri, 29 Mar 2024 14:01:10 +0400 Subject: [PATCH 2/2] test: remove commented lines --- src/main.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index ff4660e4..9e1d02e8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,5 +20,4 @@ export { TextArea } from "./components/TextArea"; export { ToggleSwitch } from "./components/ToggleSwitch"; export { Tooltip } from "./components/Tooltip"; export { useDevice,useOnClickOutside } from "./hooks"; -// export { useOnClickOutside } from "./hooks/useOnClickOutside"; export { VerticalTab, VerticalTabItems } from "./components/VerticalTab";