Skip to content

Commit e4b1ade

Browse files
committed
fix: improve the community components testing
1 parent a54cc6b commit e4b1ade

File tree

12 files changed

+81
-74
lines changed

12 files changed

+81
-74
lines changed

__mocks__/course.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,40 +92,40 @@ export const mockLearningModule: LearningModule = {
9292
};
9393

9494
export const mockCourse: Course = {
95-
id: "course",
96-
ref: "course ref",
97-
created_at: new Date("2022-05-01T12:00:00Z"),
98-
updated_at: new Date("2022-05-01T12:00:00Z"),
99-
duration: 3,
100-
summary: "Course description",
101-
level: 3,
102-
name: "course name",
103-
description: "Course description",
104-
objectives: ["course description", "course objectives"],
105-
locale: "English",
106-
community: "community",
107-
slug: "course description slug",
108-
introduction: Introduction,
109-
active: true,
110-
certificateIcon: "certificate",
111-
certificateData: mockCertificateData,
112-
timestamp: 0,
113-
learningModules: [mockLearningModule],
114-
trailer: mockTrailer,
115-
disclaimer: "Course",
116-
items: ["item 1", "item 2"],
117-
faq: [
118-
{
119-
description: "faq description",
120-
title: "faq title",
121-
},
122-
],
123-
prerequisite: {
95+
id: "course",
96+
ref: "course ref",
97+
translations: ["these are the translations"],
98+
created_at: new Date("2022-05-01T12:00:00Z"),
99+
updated_at: new Date("2022-05-01T12:00:00Z"),
100+
duration: 3,
101+
summary: "Course description",
102+
level: 3,
103+
name: "course name",
104+
description: "Course description",
105+
objectives: ["course description", "course objectives"],
106+
locale: "English",
107+
community: "community",
108+
slug: "course description slug",
109+
introduction: Introduction,
110+
active: true,
111+
certificateIcon: "certificate",
112+
certificateData: mockCertificateData,
113+
timestamp: 0,
114+
learningModules: [mockLearningModule],
115+
trailer: mockTrailer,
116+
disclaimer: "Course",
124117
items: ["item 1", "item 2"],
125-
hint: "prerequisite hint",
126-
},
118+
faq: [
119+
{
120+
description: "faq description",
121+
title: "faq title",
122+
},
123+
],
124+
prerequisite: {
125+
items: ["item 1", "item 2"],
126+
hint: "prerequisite hint",
127+
},
127128
};
128-
129129
export const mockFormat: Format = {
130130
githubLink: true,
131131
text: true,

__tests__/components/sections/communities/_partials/Section.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ describe("section", () => {
2323
<div>community section child</div>
2424
</Section>
2525
);
26-
const communitySectionChildren = screen.getByText("community section child");
27-
expect(communitySectionChildren).toBeInTheDocument();
26+
expect(screen.getByText("community section child")).toBeInTheDocument();
2827
});
2928
});

__tests__/components/sections/communities/overview/MainHeader.test.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ import "@testing-library/jest-dom";
22
import { screen } from "@testing-library/react";
33
import CommunitySection from "@/components/sections/communities/overview/MainHeader";
44
import { renderWithRedux } from "@__mocks__/renderWithRedux";
5+
import { mockCommunity } from "@__mocks__/community";
6+
import { mockCourse } from "@__mocks__/course";
7+
import { CommunitiesState } from "@/store/feature/community.slice";
58

69
describe("MainHeader", () => {
7-
it("displays the header", () => {
8-
renderWithRedux(<CommunitySection testId="mainHeaderId" />);
9-
10-
expect(screen.getByTestId("mainHeaderId")).toBeInTheDocument();
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.getByRole('img')).toBeInTheDocument()
22+
}
1123
expect(screen.getByText("communities.submissions")).toBeInTheDocument();
1224
expect(screen.getByText("communities.feedbacks")).toBeInTheDocument();
1325
});

__tests__/components/sections/communities/overview/Sidebar.test.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@ jest.mock("next/router", () => ({
1212
}));
1313

1414
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 scoreboardLink = `/communities/${mockCommunity.slug}/scoreboard`;
20+
1521
it("displays the sidebar", () => {
16-
renderWithRedux(<Sidebar testId="sidebarId" />, { community: { current: mockCommunity, list: [mockCommunity], courses: [mockCourse], status: "succeeded", error: "" } });
17-
expect(screen.getByTestId("sidebarId")).toBeInTheDocument();
1822
expect(screen.getByText("communities.overview.challenges.title")).toBeInTheDocument();
1923
expect(screen.getByText("communities.overview.challenges.description")).toBeInTheDocument();
24+
expect(screen.getByText("communities.overview.scoreboard.title")).toBeInTheDocument();
25+
expect(screen.getByText("communities.overview.scoreboard.description")).toBeInTheDocument();
26+
});
27+
28+
it("renders the links correctly", () => {
29+
expect(screen.getByRole("link", { name: "main link" })).toHaveAttribute("href", mainLink);
30+
expect(screen.getByRole("link", { name: "scoreboard link" })).toHaveAttribute("href", scoreboardLink);
2031
});
2132
});

__tests__/components/sections/communities/overview/Wrapper.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ jest.mock("next/router", () => ({
99
}),
1010
}));
1111

12+
1213
describe("Wrapper", () => {
1314
it("displays the wrapper", () => {
1415
renderWithRedux(
15-
<Wrapper testId="wrapperId" filter={<div>Test Filter</div>}>
16+
<Wrapper filter={<div>Test Filter</div>}>
1617
<div>Wrappper test</div>
1718
</Wrapper>
1819
);
20+
1921
expect(screen.getByTestId("wrapperId")).toBeInTheDocument();
22+
expect(screen.getByText("communities.overview.challenges.title")).toBeInTheDocument();
2023
expect(screen.getByText("Wrappper test")).toBeInTheDocument();
2124
expect(screen.getByText("Test Filter")).toBeInTheDocument();
2225
});

__tests__/components/sections/communities/overview/_partials/SectionWrapper.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ describe("SectionWrapper", () => {
1111
<div>Test Children</div>
1212
</SectionWrapper>
1313
);
14-
1514
expect(screen.getByText(title)).toBeInTheDocument();
1615
expect(screen.getByText(description)).toBeInTheDocument();
1716
expect(screen.getByText("Test Children")).toBeInTheDocument();
@@ -23,9 +22,8 @@ describe("SectionWrapper", () => {
2322
<div>Test Children</div>
2423
</SectionWrapper>
2524
);
26-
27-
expect(screen.queryByText("Test Title")).not.toBeInTheDocument();
28-
expect(screen.queryByText("Test Description")).not.toBeInTheDocument();
25+
expect(screen.queryByText("Test Title")).toBe(null);
26+
expect(screen.queryByText("Test Description")).toBe(null);
2927
expect(screen.getByText("Test Children")).toBeInTheDocument();
3028
});
3129
});

__tests__/components/sections/communities/overview/scoreboard/index.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ describe("ScoreboardOverview", () => {
1717
};
1818

1919
it("displays the scoreboard overview", () => {
20-
renderWithRedux(<ScoreboardOverview testId="scoreboardId" />, {
20+
renderWithRedux(<ScoreboardOverview />, {
2121
scoreboard: { list: [mockScoreboard], loading: true, filterBy: "" },
2222
});
23-
expect(screen.getByTestId("scoreboardId")).toBeInTheDocument();
23+
expect(screen.getByTestId("loader")).toBeInTheDocument();
2424
});
2525

2626
it("renders scoreboard cards when not loading and list is not empty", () => {

src/components/sections/communities/overview/MainHeader.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,22 @@ import { useSelector } from "@/hooks/useTypedSelector";
33
import { useTranslation } from "next-i18next";
44
import Image from "next/image";
55
import { ReactElement } from "react";
6-
7-
interface CommunitySectionProps {
8-
testId?: string;
9-
}
10-
116
/**
127
* Represents the Community Section
138
* @date 4/13/2023 - 5:56:41 AM
149
*
1510
* @export
1611
* @returns {ReactElement}
1712
*/
18-
export default function CommunitySection({ testId }: CommunitySectionProps): ReactElement {
13+
export default function CommunitySection(): ReactElement {
1914
const { t } = useTranslation();
2015
const community = useSelector((state) => state.communities.current);
21-
2216
const submissions = community?.metadata?.submissions || 0;
2317
const feedbacks = community?.metadata?.feedbacks || 0;
2418

2519
return (
2620
<Section className="bg-theme-primary text-theme-text">
27-
<div data-testid={testId} className="py-2 md:py-8 mx-auto">
21+
<div className="py-2 md:py-8 mx-auto">
2822
<div className="flex flex-col justify-between lg:flex-row">
2923
<div className="max-w-md mb-4 md:max-w-md lg:max-w-lg xl:max-w-xl 2xl:max-w-2xl lg:mr-6">
3024
<h1 className="text-5xl tracking-tight sm:text-6.5xl xl:text-7.75xl max-w-text-xs">{community?.name}</h1>

src/components/sections/communities/overview/Sidebar.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { useRouter } from "next/router";
44
import { useTranslation } from "next-i18next";
55
import classNames from "classnames";
66

7-
interface SidebarProps {
8-
testId?: string;
9-
}
10-
117
/**
128
* @interface SidebarProps
139
* @description The props for the Sidebar component.
@@ -18,7 +14,7 @@ interface SidebarProps {
1814
* @param {} SidebarProps - The props for the Sidebar component.
1915
* @returns JSX.Element - The rendered Sidebar component.
2016
*/
21-
export default function Sidebar({ testId }: SidebarProps): JSX.Element {
17+
export default function Sidebar(): JSX.Element {
2218
const currentCommunity = useSelector((state) => state.communities.current);
2319
const hasCurrentCommunity = !!currentCommunity; // Check if currentCommunity exists
2420
const router = useRouter();
@@ -32,20 +28,19 @@ export default function Sidebar({ testId }: SidebarProps): JSX.Element {
3228
const isActive = (link: string) => {
3329
return router.asPath === link;
3430
};
35-
3631
const scoreboardLink = hasCurrentCommunity ? `/communities/${currentCommunity.slug}/scoreboard` : "";
3732
const mainLink = hasCurrentCommunity ? `/communities/${currentCommunity.slug}` : "";
3833

3934
return (
40-
<div data-testid={testId} className="flex flex-col md:divide-y divide-solid divide-gray-100 w-full text-gray-700 space-y-6">
41-
<Link href={mainLink}>
35+
<div className="flex flex-col md:divide-y divide-solid divide-gray-100 w-full text-gray-700 space-y-6">
36+
<Link href={mainLink} aria-label="main link">
4237
<div className={isActive(mainLink) ? "" : "opacity-80"}>
4338
<div className="font-medium text-.5xl leading-snug">{t("communities.overview.challenges.title")}</div>
4439
<div className="text-sm font-light lg:w-full lg:pr-7 pt-2 mb-6 md:mb-0">{t("communities.overview.challenges.description")} </div>
4540
</div>
4641
</Link>
4742
{hasCurrentCommunity && (
48-
<Link href={scoreboardLink}>
43+
<Link href={scoreboardLink} aria-label="scoreboard link">
4944
<div className={classNames("pt-6", { "opacity-80 md:block hidden scroll-smooth": isActive(scoreboardLink) })}>
5045
<div className="font-medium text-.5xl leading-snug">{t("communities.overview.scoreboard.title")}</div>
5146
<div className="text-sm font-light lg:w-full lg:pr-7 pt-2 mb-6 md:mb-0">{t("communities.overview.scoreboard.description")}</div>

src/components/sections/communities/overview/Wrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface WrapperProps {
2020
* @returns {JSX.Element} A styled Wrapper component containing a MainHeaderSection, CommunitySidebar,
2121
* optional filter component, and children content.
2222
*/
23-
export default function Wrapper({ children, filter, testId }: WrapperProps): JSX.Element {
23+
export default function Wrapper({ children, filter, testId = "wrapperId" }: WrapperProps): JSX.Element {
2424
return (
2525
<div data-testid={testId} className="relative">
2626
<MainHeaderSection />

0 commit comments

Comments
 (0)