Skip to content

Commit 07871b4

Browse files
EPMGCIP-171: Implement UTs for "Home.tsx" component
1 parent 9845327 commit 07871b4

File tree

5 files changed

+96
-16
lines changed

5 files changed

+96
-16
lines changed

src/components/organisms/ImageGallery/ImageGallery.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ function ImageGallery({ images, isZoomEnabled, displayArrows, onClickImage }: Pr
136136
infinite={false}
137137
centerPadding="28px"
138138
slidesToShow={3}
139+
className="image-gallery"
139140
arrows={displayArrows}
140141
prevArrow={<ImageGalleryArrow direction={ArrowDirection.Previous} />}
141142
nextArrow={<ImageGalleryArrow direction={ArrowDirection.Next} />}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import "@testing-library/jest-dom";
2+
import { render } from "@testing-library/react";
3+
4+
import { NextIntlClientProvider } from "next-intl";
5+
6+
import messages from "../../../../messages/en.json";
7+
8+
import Home from "./Home";
9+
10+
import { topLatestExhibits } from "@/mocks/exhibit";
11+
import { ITopLatestExhibit } from "@/interfaces/ITopLatestExhibit";
12+
13+
jest.mock("@/navigation", () => ({
14+
useRouter: jest.fn().mockReturnValue({
15+
replace: jest.fn(),
16+
}),
17+
}));
18+
19+
jest.mock("yet-another-react-lightbox", () => jest.fn());
20+
jest.mock("yet-another-react-lightbox/plugins/zoom", () => ({}));
21+
22+
describe("Home component", () => {
23+
const renderComponent = (exhibits: ITopLatestExhibit[]) =>
24+
render(
25+
<NextIntlClientProvider locale="en" messages={messages}>
26+
<Home exhibits={exhibits} />
27+
</NextIntlClientProvider>,
28+
);
29+
30+
it("should render component", () => {
31+
const { getByText } = renderComponent(topLatestExhibits);
32+
33+
expect(getByText("Welcome to our official webpage"));
34+
});
35+
36+
it("should render component with image gallery", () => {
37+
const { container } = renderComponent(topLatestExhibits);
38+
39+
const imageGalleryElement = container.querySelector(".image-gallery");
40+
41+
expect(imageGalleryElement).toBeInTheDocument();
42+
});
43+
44+
it("should not render component with image gallery if no images are discovered", () => {
45+
const exhibits = topLatestExhibits.map((exhibit) => ({
46+
...exhibit,
47+
imagesCollection: { items: [] },
48+
}));
49+
50+
const { container } = renderComponent(exhibits);
51+
52+
const imageGalleryElement = container.querySelector(".image-gallery");
53+
54+
expect(imageGalleryElement).toBeNull();
55+
});
56+
});

src/components/pages/Home/Home.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ export default function Home(props: Props): React.ReactElement {
2323

2424
const images: IImageGalleryImage[] = useMemo(
2525
() =>
26-
props.exhibits.map((exhibit) => {
27-
const firstImageItem = exhibit.imagesCollection.items[0];
26+
props.exhibits
27+
.filter((exhibit) => !!exhibit.imagesCollection.items.length)
28+
.map((exhibit) => {
29+
const firstImageItem = exhibit.imagesCollection.items[0];
2830

29-
return {
30-
url: firstImageItem.url,
31-
id: firstImageItem.sys.id,
32-
};
33-
}),
31+
return {
32+
url: firstImageItem.url,
33+
id: firstImageItem.sys.id,
34+
};
35+
}),
3436
[props.exhibits],
3537
);
3638

src/interfaces/ITopLatestExhibit.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ export interface ITopLatestExhibit {
88
nameUz?: string | null;
99
nameKa?: string | null;
1010
imagesCollection: {
11-
items: [
12-
{
13-
url: string;
14-
sys: {
15-
id: string;
16-
};
17-
},
18-
];
11+
items: {
12+
url: string;
13+
sys: {
14+
id: string;
15+
};
16+
}[];
1917
};
2018
}

src/mocks/exhibit.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GetExhibitDocument } from "@/__generated__/graphql";
1+
import { ITopLatestExhibit } from "@/interfaces/ITopLatestExhibit";
22

33
export const exhibitItem = {
44
sys: {
@@ -87,3 +87,26 @@ export const exhibitMockAnotherLanguage = {
8787
nameUz: "Test Exhibit",
8888
nameKa: "Test Exhibit",
8989
};
90+
91+
export const topLatestExhibits: ITopLatestExhibit[] = [
92+
{
93+
sys: {
94+
id: "12345",
95+
},
96+
slug: "test-exhibit",
97+
nameRu: "Test Exhibit",
98+
nameUz: "Test Exhibit",
99+
nameKa: "Test Exhibit",
100+
nameEn: "Test Exhibit",
101+
imagesCollection: {
102+
items: [
103+
{
104+
url: "http://example.com",
105+
sys: {
106+
id: "1",
107+
},
108+
},
109+
],
110+
},
111+
},
112+
];

0 commit comments

Comments
 (0)