Skip to content

Commit 96d11eb

Browse files
authored
Merge pull request #167 from farhan-nurzi-deriv/farhan/tablet-portrait
Farhan/add isTabletPortrait to useDevice
2 parents 49b0758 + 617f163 commit 96d11eb

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
11
import React from "react";
22
import { render, screen } from "@testing-library/react";
33
import { PageLayout } from "..";
4-
import * as hooks from "../../../hooks"
4+
import * as hooks from "../../../hooks";
55

66
// Mocking the useDevice hook
77
jest.mock("../../../hooks", () => ({
8-
useDevice: jest.fn().mockReturnValue({isMobile:false}),
8+
useDevice: jest.fn().mockReturnValue({ isMobile: false }),
99
}));
1010

1111
describe("PageLayout Component", () => {
12-
it("renders children correctly", () => {
13-
render(
14-
<PageLayout>
15-
<div>Content</div>
16-
</PageLayout>
17-
);
18-
const content =screen.getByText("Content")
19-
expect(content).toBeInTheDocument();
20-
});
12+
it("renders children correctly", () => {
13+
render(
14+
<PageLayout>
15+
<div>Content</div>
16+
</PageLayout>,
17+
);
18+
const content = screen.getByText("Content");
19+
expect(content).toBeInTheDocument();
20+
});
2121

22-
it("renders sidebar when provided and not on mobile", () => {
23-
const sidebar = <div>Sidebar</div>;
24-
render(<PageLayout sidebar={sidebar} />);
25-
const sidebarContent =screen.getByText("Sidebar")
26-
expect(sidebarContent).toBeInTheDocument();
27-
});
22+
it("renders sidebar when provided and not on mobile", () => {
23+
const sidebar = <div>Sidebar</div>;
24+
render(<PageLayout sidebar={sidebar} />);
25+
const sidebarContent = screen.getByText("Sidebar");
26+
expect(sidebarContent).toBeInTheDocument();
27+
});
2828

29-
it("does not render sidebar on mobile", () => {
30-
jest.spyOn(hooks,'useDevice').mockImplementation(()=>({isMobile:true, isDesktop:false, isTablet:false}))
31-
const sidebar = <div>Sidebar</div>;
32-
render(<PageLayout sidebar={sidebar} />);
33-
const sidebarContent =screen.queryByText("Sidebar")
34-
expect(sidebarContent).not.toBeInTheDocument()
35-
});
29+
it("does not render sidebar on mobile", () => {
30+
jest.spyOn(hooks, "useDevice").mockImplementation(() => ({
31+
isMobile: true,
32+
isDesktop: false,
33+
isTablet: false,
34+
isTabletPortrait: false,
35+
}));
36+
const sidebar = <div>Sidebar</div>;
37+
render(<PageLayout sidebar={sidebar} />);
38+
const sidebarContent = screen.queryByText("Sidebar");
39+
expect(sidebarContent).not.toBeInTheDocument();
40+
});
3641

37-
it("does not render sidebar when not provided", () => {
38-
render(<PageLayout />);
39-
const sidebarContent=screen.queryByTestId("sidebar")
40-
expect(sidebarContent).toBeNull();
41-
});
42+
it("does not render sidebar when not provided", () => {
43+
render(<PageLayout />);
44+
const sidebarContent = screen.queryByTestId("sidebar");
45+
expect(sidebarContent).toBeNull();
46+
});
4247
});

src/hooks/useDevice.ts

+5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ export const useDevice = () => {
77
const isTablet = useMediaQuery(
88
`(min-width: 601px) and (max-width: 1279px)`,
99
);
10+
const isTabletPortrait = useMediaQuery(
11+
`(min-width: 601px) and (max-width: 1279px) and (orientation: portrait)`,
12+
);
1013
return {
1114
/** returns Larger screen tablets [min-width: 1280px] */
1215
isDesktop,
1316
/** returns Smaller screen tablets [max-width: 600px] */
1417
isMobile,
1518
/** returns Larger screen phones and smaller screen desktop [min-width: 601px and max-width: 1279px] */
1619
isTablet,
20+
/** returns tablet screen with portrait orientation [min-width: 601px and max-width: 1279px and orientation: portrait] */
21+
isTabletPortrait,
1722
};
1823
};

0 commit comments

Comments
 (0)