Skip to content

Commit 276a140

Browse files
Merge pull request #1244 from dacadeorg/test/section-community
Test: add community tests
2 parents 5947862 + 2b104a7 commit 276a140

File tree

18 files changed

+418
-12
lines changed

18 files changed

+418
-12
lines changed

__mocks__/referrals.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Referral, UserReferral } from "@/types/community";
2+
import { mockCommunity } from "./community";
3+
import { challenge, mockUser, submission as getMockSubmission } from "./challenge";
4+
5+
6+
const referralSubmission = () => Object.assign(getMockSubmission(), { challengeData: challenge(), link: "referral-link" });
7+
const userReferral: UserReferral = Object.assign(mockUser(), { submissions: [referralSubmission()] });
8+
9+
export const mockReferral = (): Referral => ({
10+
id: "",
11+
name: "",
12+
ref: "",
13+
created_at: new Date("2022-05-01T12:00:00Z"),
14+
updated_at: new Date("2022-05-01T12:00:00Z"),
15+
title: "",
16+
community: mockCommunity,
17+
timestamp: 0,
18+
reward: {
19+
id: "",
20+
ref: "",
21+
created_at: new Date("2022-05-01T12:00:00Z"),
22+
updated_at: new Date("2022-05-01T12:00:00Z"),
23+
challenge: "",
24+
type: "",
25+
community: "",
26+
token: "",
27+
stable: false,
28+
fiatCurrency: undefined,
29+
amount: 0,
30+
timestamp: 0,
31+
distribution: undefined,
32+
},
33+
challenge: challenge(),
34+
submissions: [getMockSubmission()],
35+
rewarded: false,
36+
user: userReferral,
37+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Header from "@/components/sections/communities/_partials/Header";
2+
import { render, screen } from "@testing-library/react";
3+
import "@testing-library/jest-dom";
4+
5+
const headerProps = {
6+
description: "Test Description",
7+
title: "Test Title",
8+
subtitle: "Test Subtitle",
9+
isTeamChallenge: true,
10+
isHackathon: true,
11+
};
12+
13+
describe("Header", () => {
14+
it("renders the Header with Props", () => {
15+
render(
16+
<Header
17+
description={headerProps.description}
18+
title={headerProps.title}
19+
subtitle={headerProps.subtitle}
20+
isTeamChallenge={headerProps.isTeamChallenge}
21+
isHackathon={headerProps.isHackathon}
22+
/>
23+
);
24+
expect(screen.getByRole("heading", { name: headerProps.title })).toBeInTheDocument();
25+
expect(screen.getByRole("heading", { name: new RegExp(headerProps.subtitle) })).toBeInTheDocument();
26+
expect(screen.getByTestId("tag")).toBeInTheDocument();
27+
expect(screen.getByText(headerProps.description)).toBeInTheDocument();
28+
});
29+
30+
it("does not render subtitle when not provided", () => {
31+
render(<Header />);
32+
expect(screen.queryByRole("heading", { name: new RegExp(headerProps.subtitle) })).toBe(null);
33+
expect(screen.queryByRole("tag")).toBe(null);
34+
});
35+
36+
it("renders 'TEAM' if isHackathon is false", () => {
37+
render(<Header subtitle={headerProps.subtitle} isTeamChallenge={headerProps.isTeamChallenge} />);
38+
expect(screen.getByText("TEAM")).toBeInTheDocument();
39+
});
40+
41+
it("renders 'Hackathon' if isHackathon is true", () => {
42+
render(<Header subtitle={headerProps.subtitle} isTeamChallenge={headerProps.isTeamChallenge} isHackathon={headerProps.isHackathon} />);
43+
expect(screen.getByText("Hackathon")).toBeInTheDocument();
44+
});
45+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Section from "@/components/sections/communities/_partials/Section";
2+
import "@testing-library/jest-dom";
3+
import { render, screen } from "@testing-library/react";
4+
5+
describe("section", () => {
6+
it("should render section", () => {
7+
render(<Section />);
8+
const communitySection = screen.getByTestId("sectionId");
9+
expect(communitySection).toBeInTheDocument();
10+
});
11+
12+
it("should render section with title and subtitle", () => {
13+
render(<Section title={"section title"} subtitle={"section subtitle"} hideSubtitleOnMobile={false} />);
14+
const communitySectionTitle = screen.getByText("section title");
15+
expect(communitySectionTitle).toBeInTheDocument();
16+
const communitySectionSubTitle = screen.getByText("section subtitle");
17+
expect(communitySectionSubTitle).toBeInTheDocument();
18+
});
19+
20+
it("should render with children", () => {
21+
render(
22+
<Section>
23+
<div>community section child</div>
24+
</Section>
25+
);
26+
expect(screen.getByText("community section child")).toBeInTheDocument();
27+
});
28+
});
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 { CoursesOverview } from "@/components/sections/communities/overview/Courses";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
5+
import { mockCourse } from "@__mocks__/fixtures/course";
6+
import { mockCommunity } from "@__mocks__/fixtures/community";
7+
8+
9+
jest.mock("next/router", () => ({
10+
useRouter: () => ({
11+
asPath: "next",
12+
}),
13+
}));
14+
15+
describe("CoursesOverview", () => {
16+
it("renders the courses overview section with course cards", () => {
17+
renderWithRedux(<CoursesOverview />, {
18+
courses: { current: mockCourse, list: [mockCourse], content: mockCourse.community, count: 1, menus: [] },
19+
community: { current: mockCommunity, list: [mockCommunity], courses: [mockCourse], status: "succeeded", error: "" },
20+
});
21+
expect(screen.getByText("communities.overview.courses.title")).toBeInTheDocument();
22+
[mockCourse].forEach((course) => {
23+
const courseElement = screen.getByText(course.name);
24+
expect(courseElement).toBeInTheDocument();
25+
expect(courseElement.textContent).toBe(course.name);
26+
});
27+
});
28+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import "@testing-library/jest-dom";
2+
import { screen } from "@testing-library/react";
3+
import CommunitySection from "@/components/sections/communities/overview/MainHeader";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
5+
import { CommunitiesState } from "@/store/feature/community.slice";
6+
import { mockCommunity } from "@__mocks__/fixtures/community";
7+
import { mockCourse } from "@__mocks__/fixtures/course";
8+
9+
describe("MainHeader", () => {
10+
const communityMockState = {
11+
communities: { current: mockCommunity, courses: [mockCourse], list: [mockCommunity], status: "succeeded" as CommunitiesState["status"], error: null },
12+
};
13+
it("renders community section component", () => {
14+
renderWithRedux(<CommunitySection />, communityMockState);
15+
expect(screen.getByRole("heading", {name:mockCommunity.name})).toBeInTheDocument();
16+
const summaryElements = screen.queryAllByText(mockCommunity.summary);
17+
summaryElements.forEach(element=>{
18+
expect(element).toBeInTheDocument()
19+
})
20+
if(mockCommunity.icon){
21+
expect(screen.getByAltText(mockCommunity.name)).toBeInTheDocument()
22+
}
23+
expect(screen.getByText("communities.submissions")).toBeInTheDocument();
24+
expect(screen.getByText("communities.feedbacks")).toBeInTheDocument();
25+
});
26+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "@testing-library/jest-dom";
2+
import { screen } from "@testing-library/react";
3+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
4+
import Sidebar from "@/components/sections/communities/overview/Sidebar";
5+
import { mockCourse } from "@__mocks__/fixtures/course";
6+
import { mockCommunity } from "@__mocks__/fixtures/community";
7+
8+
jest.mock("next/router", () => ({
9+
useRouter: () => ({
10+
asPath: "next",
11+
}),
12+
}));
13+
14+
describe("Sidebar", () => {
15+
beforeEach(() => {
16+
renderWithRedux(<Sidebar />, { communities: { current: mockCommunity, list: [mockCommunity], courses: [mockCourse], status: "succeeded", error: "" } });
17+
});
18+
const mainLink = `/communities/${mockCommunity.slug}`;
19+
const learningMaterialsLink = `/communities/${mockCommunity.slug}/learning-materials`;
20+
const scoreboardLink = `/communities/${mockCommunity.slug}/scoreboard`;
21+
22+
it("displays the sidebar", () => {
23+
expect(screen.getByText("communities.overview.challenges.title")).toBeInTheDocument();
24+
expect(screen.getByText("communities.overview.challenges.description")).toBeInTheDocument();
25+
expect(screen.getByText("communities.overview.scoreboard.title")).toBeInTheDocument();
26+
expect(screen.getByText("communities.overview.scoreboard.description")).toBeInTheDocument();
27+
});
28+
29+
it("renders the links correctly", () => {
30+
expect(screen.getByText("communities.overview.challenges.title").closest("a")).toHaveAttribute("href", mainLink);
31+
expect(screen.getByText("communities.overview.learning-materials.title").closest("a")).toHaveAttribute("href", learningMaterialsLink);
32+
expect(screen.getByText("communities.overview.scoreboard.title").closest("a")).toHaveAttribute("href", scoreboardLink);
33+
});
34+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import "@testing-library/jest-dom";
2+
import { screen } from "@testing-library/react";
3+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
4+
import Wrapper from "@/components/sections/communities/overview/Wrapper";
5+
6+
jest.mock("next/router", () => ({
7+
useRouter: () => ({
8+
asPath: "next",
9+
}),
10+
}));
11+
12+
13+
describe("Wrapper", () => {
14+
it("displays the wrapper with all the children", () => {
15+
renderWithRedux(
16+
<Wrapper filter={<div>Test Filter</div>}>
17+
<div>Wrappper test</div>
18+
</Wrapper>
19+
);
20+
21+
expect(screen.getByTestId("wrapperId")).toBeInTheDocument();
22+
expect(screen.getByText("communities.overview.challenges.title")).toBeInTheDocument();
23+
expect(screen.getByText("Wrappper test")).toBeInTheDocument();
24+
expect(screen.getByText("Test Filter")).toBeInTheDocument();
25+
});
26+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { render, screen } from "@testing-library/react";
2+
import "@testing-library/jest-dom";
3+
import { SectionWrapper } from "@/components/sections/communities/overview/_partials/SectionWrapper";
4+
5+
describe("SectionWrapper", () => {
6+
it("renders with title and description", () => {
7+
const title = "Test Title";
8+
const description = "Test Description";
9+
render(
10+
<SectionWrapper title={title} description={description}>
11+
<div>Test Children</div>
12+
</SectionWrapper>
13+
);
14+
expect(screen.getByText(title)).toBeInTheDocument();
15+
expect(screen.getByText(description)).toBeInTheDocument();
16+
expect(screen.getByText("Test Children")).toBeInTheDocument();
17+
});
18+
19+
it("renders without title and description when they are not available", () => {
20+
render(
21+
<SectionWrapper>
22+
<div>Test Children</div>
23+
</SectionWrapper>
24+
);
25+
expect(screen.queryByText("Test Title")).toBe(null);
26+
expect(screen.queryByText("Test Description")).toBe(null);
27+
expect(screen.getByText("Test Children")).toBeInTheDocument();
28+
});
29+
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import Filters, { filterOptions, sortingOptions } from "@/components/sections/communities/overview/scoreboard/Filter";
2+
import "@testing-library/jest-dom";
3+
import { fireEvent, screen } from "@testing-library/react";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
5+
6+
jest.mock("next/router", () => {
7+
const mockRouter = {
8+
query: { slug: "test-slug" },
9+
locale: "en",
10+
push: jest.fn(),
11+
};
12+
return {
13+
useRouter: jest.fn().mockReturnValue(mockRouter),
14+
};
15+
});
16+
17+
describe("Filter", () => {
18+
19+
it("renders filter options", () => {
20+
renderWithRedux(<Filters />);
21+
expect(screen.getByText("Filter by")).toBeInTheDocument();
22+
filterOptions.forEach((option) => {
23+
const labelElement = screen.getByText(option.label)
24+
const valueElement = screen.getByDisplayValue(option.value)
25+
expect(labelElement).toBeInTheDocument();
26+
expect(labelElement.textContent).toBe(option.label)
27+
expect(valueElement).toBeInTheDocument();
28+
expect(valueElement.getAttribute("value")).toBe(option.value)
29+
});
30+
});
31+
32+
it("renders default filter and sort values", () => {
33+
renderWithRedux(<Filters />);
34+
const defaultFilter = screen.getByDisplayValue("all");
35+
const defaultSort = screen.getByDisplayValue("score");
36+
expect(defaultFilter).toBeChecked();
37+
expect(defaultSort).toBeChecked();
38+
});
39+
40+
it("disables filter options when value matches data", () => {
41+
renderWithRedux(<Filters />);
42+
filterOptions.forEach((option) => {
43+
const valueElement = screen.getByDisplayValue(option.value);
44+
if (option.value === "all") {
45+
expect(valueElement).toBeDisabled();
46+
}
47+
});
48+
});
49+
50+
it("updates filter options on change on filter", () => {
51+
renderWithRedux(<Filters />);
52+
filterOptions.forEach((option) => {
53+
const value = screen.getByDisplayValue(option.value);
54+
fireEvent.change(value, { target: { value: "new filter option" } });
55+
expect(value.getAttribute("value")).toContain("new filter option");
56+
});
57+
});
58+
59+
it("renders sort options", () => {
60+
renderWithRedux(<Filters />);
61+
expect(screen.getByText("Sort")).toBeInTheDocument();
62+
sortingOptions.forEach((option) => {
63+
const labelElement = screen.getByText(option.label)
64+
const optionElement = screen.getByDisplayValue(option.value)
65+
expect(labelElement).toBeInTheDocument();
66+
expect(labelElement.textContent).toBe(option.label);
67+
expect(optionElement).toBeInTheDocument();
68+
expect(optionElement.getAttribute("value")).toBe(option.value);
69+
});
70+
});
71+
72+
it("updates filter options on change on sort", () => {
73+
renderWithRedux(<Filters />);
74+
sortingOptions.forEach((option) => {
75+
const value = screen.getByDisplayValue(option.value);
76+
fireEvent.change(value, { target: { value: "new filter option" } });
77+
expect(value.getAttribute("value")).toContain("new filter option");
78+
});
79+
});
80+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { render, screen } from "@testing-library/react";
2+
import "@testing-library/jest-dom";
3+
import FilterOption from "@/components/sections/communities/overview/scoreboard/_partials/FilterOption";
4+
5+
describe("FilterOption", () => {
6+
it("renders with label and is not checked by default", () => {
7+
const label = "Test Label";
8+
render(<FilterOption label={label} value="1" data="2" />);
9+
expect(screen.getByLabelText(label)).toBeInTheDocument();
10+
expect(screen.getByRole("radio")).not.toBeChecked();
11+
});
12+
13+
it("should be disabled when value matches data", () => {
14+
const label = "Test Label";
15+
render(<FilterOption label={label} value="1" data="1" />);
16+
const radio = screen.getByRole("radio");
17+
expect(radio).toBeDisabled();
18+
});
19+
});

0 commit comments

Comments
 (0)