Skip to content

Commit f59ad6e

Browse files
authored
Merge pull request #1246 from dacadeorg/test/section-homepage-testing
feat: add test for the section homepage
2 parents 7874f9b + 7a33435 commit f59ad6e

29 files changed

+227
-36
lines changed

__mocks__/community.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Community } from "@/types/community";
2+
import { reward } from "./reward";
3+
import { colors } from "./colors";
4+
5+
6+
export const metadata = {
7+
invite_id: "abc123",
8+
submissions: 5,
9+
bestSubmissions: ["submission1", "submission2"],
10+
feedbacks: 10,
11+
name: "Project XYZ",
12+
issuedOn: "2024-01-29T12:00:00Z",
13+
image: "image_url",
14+
title: "Title of the project",
15+
description: "Description of the project",
16+
narrative: "Narrative of the project",
17+
recipientName: "John Doe",
18+
issuerName: "Jane Smith",
19+
comment: "This is a comment",
20+
linkToWork: "link_to_work",
21+
submission: "submission_details",
22+
};
23+
24+
export const community: Community = {
25+
id: "ew-43",
26+
ref: "community/ref",
27+
created_at: new Date("2022-05-01T12:00:00Z"),
28+
updated_at: new Date("2022-05-01T12:00:00Z"),
29+
summary: "this is the summary",
30+
icon: "public/img/communities/aeternity.svg",
31+
name: "aeternity",
32+
image: "public/img/communities/aeternity.svg",
33+
colors: colors,
34+
slug: "ae",
35+
active: true,
36+
description: "this is a aeternity community",
37+
metadata,
38+
timestamp: 182044800000,
39+
rewards: [reward],
40+
reward,
41+
courses: 3,
42+
duration: 4,
43+
can_mint_certificates: true,
44+
challenges: 3
45+
};

__tests__/components/sections/bounties/Navigation.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "@testing-library/jest-dom";
22
import { screen } from "@testing-library/react";
33
import BountiesNavigation from "@/components/sections/bounties/Navigation";
4-
import { renderWithRedux } from "../../../../__mocks__/renderWithRedux";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
55
import { List } from "@/utilities/CommunityNavigation";
66

77
jest.mock("next/router", () => ({
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import "@testing-library/jest-dom";
2+
import { screen } from "@testing-library/react";
3+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
4+
import CommunitiesSection from "@/components/sections/homepage/Communities";
5+
import { mockCommunity } from "@__mocks__/fixtures/community";
6+
jest.mock("next/router", () => ({
7+
useRouter: jest.fn(),
8+
}));
9+
10+
describe("Communities Section", () => {
11+
it("should render community section", () => {
12+
renderWithRedux(<CommunitiesSection communities={[mockCommunity]} />);
13+
expect(screen.getByTestId("communitiesSectionId")).toBeInTheDocument();
14+
[mockCommunity].forEach((community) => {
15+
expect(screen.getByText(community.name)).toBeInTheDocument();
16+
expect(screen.getByText(community.name).textContent).toBe("aeternity");
17+
});
18+
expect(screen.getByText("page.index.communities.partnering.title")).toBeInTheDocument();
19+
});
20+
it("should handle empty communities list", () => {
21+
renderWithRedux(<CommunitiesSection communities={[]} />);
22+
expect(screen.queryByText(mockCommunity.name)).not.toBeInTheDocument();
23+
});
24+
25+
it("should render the PartneringCard", () => {
26+
renderWithRedux(<CommunitiesSection communities={[mockCommunity]} />);
27+
expect(screen.getByText("page.index.communities.partnering.title")).toBeInTheDocument();
28+
});
29+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import "@testing-library/jest-dom";
2+
import { screen } from "@testing-library/react";
3+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
4+
import MainSection, { cards } from "@/components/sections/homepage/Main";
5+
6+
describe("Main Section", () => {
7+
it("should render the main section", () => {
8+
renderWithRedux(<MainSection testId="mainSectionId" />);
9+
expect(screen.getByTestId("mainSectionId")).toBeInTheDocument();
10+
const linkElements = screen.getAllByText("page.index.main.button");
11+
const linkElement = linkElements.find((el) => el.closest("a") !== null);
12+
expect(linkElement?.closest("a")).toHaveAttribute("href", "/communities");
13+
const lastLink = linkElements[linkElements.length - 1];
14+
expect(lastLink.closest("a")).toHaveAttribute("href", "/communities");
15+
});
16+
17+
it("should display cards", () => {
18+
renderWithRedux(<MainSection />);
19+
cards.forEach((card) => {
20+
const cardTitleElement = screen.getByText(card.title);
21+
const cardBodyElement = screen.getByText(card.body);
22+
expect(cardTitleElement).toBeInTheDocument();
23+
expect(cardTitleElement).toHaveTextContent(card.title);
24+
expect(cardBodyElement).toBeInTheDocument();
25+
expect(cardBodyElement).toHaveTextContent(card.body);
26+
});
27+
});
28+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "@testing-library/jest-dom";
2+
import { screen } from "@testing-library/react";
3+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
4+
import OpenSource from "@/components/sections/homepage/OpenSource";
5+
6+
describe("OpenSource", () => {
7+
it("should render Open Source", () => {
8+
renderWithRedux(<OpenSource />);
9+
expect(screen.getByTestId("openSourceId")).toBeInTheDocument();
10+
const link = screen.getByRole("link");
11+
expect(link).toBeInTheDocument();
12+
expect(link).toHaveAttribute("href", "https://github.com/dacadeorg/dacade-frontend-app");
13+
});
14+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import "@testing-library/jest-dom";
2+
import { screen } from "@testing-library/react";
3+
import TestimonialsSection from "@/components/sections/homepage/Testimonials";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
5+
6+
describe("TestimonialsSection", () => {
7+
it("should render TestimonialsSection with child components", () => {
8+
renderWithRedux(<TestimonialsSection />);
9+
expect(screen.getByTestId("testimonialsSectionId")).toBeInTheDocument();
10+
expect(screen.getByTestId("communityStatsId")).toBeInTheDocument();
11+
expect(screen.getByTestId("testimonialsId")).toBeInTheDocument();
12+
expect(screen.getByTestId("openSourceId")).toBeInTheDocument();
13+
expect(screen.getByTestId("section")).toBeInTheDocument();
14+
});
15+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import CommunityStats, { stats } from "@/components/sections/homepage/_partials/testimonials/CommunityStats";
2+
import "@testing-library/jest-dom";
3+
import { screen } from "@testing-library/react";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
5+
6+
describe("CommunityStats", () => {
7+
it("should render Community stats", () => {
8+
renderWithRedux(<CommunityStats />);
9+
const communityStat = screen.getByTestId("communityStatsId");
10+
expect(communityStat).toBeInTheDocument();
11+
});
12+
13+
it("should display the stats", () => {
14+
renderWithRedux(<CommunityStats />);
15+
const digitFormatter = new Intl.NumberFormat();
16+
const formattedStats = stats.map(stat => ({
17+
...stat,
18+
count: digitFormatter.format(stat.count),
19+
}));
20+
21+
formattedStats.forEach((stat) => {
22+
expect(screen.getByText(stat.count)).toBeInTheDocument();
23+
expect(screen.getByText(stat.count).textContent).toBe(stat.count.toString());
24+
expect(screen.getByText(stat.title)).toBeInTheDocument();
25+
expect(screen.getByText(stat.title).textContent).toBe(stat.title.toString());
26+
expect(screen.getByText(stat.description)).toBeInTheDocument();
27+
expect(screen.getByText(stat.description).textContent).toBe(`${stat.title} ${stat.description}`);
28+
});
29+
});
30+
31+
it("should display the anchor tag with button", () => {
32+
renderWithRedux(<CommunityStats />);
33+
expect(screen.getByRole("link")).toHaveAttribute("href");
34+
expect(screen.getByRole("link").getAttribute("href")).toBe("https://discord.gg/U38KQHDtHe");
35+
expect(screen.getByRole("button")).toBeInTheDocument();
36+
expect(screen.getByText("testimonials.community.join")).toBeInTheDocument();
37+
});
38+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import "@testing-library/jest-dom";
2+
import TestimonialsSection from "@/components/sections/homepage/_partials/testimonials/Stories";
3+
import { render, screen } from "@testing-library/react";
4+
5+
const fixtureTestimonials = {
6+
content: "testimonials content",
7+
icon: "/testimonials-icon",
8+
};
9+
10+
describe("Stories", () => {
11+
it("should display each story correctly", () => {
12+
render(<TestimonialsSection list={Array(10).fill(fixtureTestimonials)} />);
13+
const stories = screen.getAllByRole("img");
14+
stories.forEach((storyImage) => {
15+
expect(storyImage).toBeInTheDocument();
16+
expect(storyImage).toHaveAttribute("src", expect.stringContaining(fixtureTestimonials.icon.replace("/testimonials-icon", "/_next/image?url=%2Ftestimonials-icon&w=96&q=75")));
17+
expect(storyImage).toHaveAttribute("alt");
18+
});
19+
});
20+
});

__tests__/components/sections/submissions/BestSubmission.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "@testing-library/jest-dom";
22
import { screen } from "@testing-library/react";
33
import { challenge, submission } from "@__mocks__/fixtures/challenge";
4-
import { renderWithRedux } from "../../../../__mocks__/renderWithRedux";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
55
import BestSubmissions from "@/components/sections/submissions/BestSubmissions";
66

77
jest.mock("next/router", () => ({

__tests__/components/ui/Avatar.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Avatar from "@/components/ui/Avatar";
22
import "@testing-library/jest-dom";
33
import { render, screen } from "@testing-library/react";
4-
import { userIcon, mockProfile } from "../../../__mocks__/fixtures/profile";
4+
import { userIcon, mockProfile } from "@__mocks__/fixtures/profile";
55

66
describe("Avatar", () => {
77
it("Should render the avatar", () => {

0 commit comments

Comments
 (0)