Skip to content

Commit e1925af

Browse files
authored
Merge pull request #1268 from dacadeorg/test/list-view-for-submissions
feat: add tests for submission card
2 parents 807998e + 37b7c3e commit e1925af

File tree

13 files changed

+189
-18
lines changed

13 files changed

+189
-18
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { renderWithRedux } from "../../../../__mocks__/renderWithRedux";
2+
import "@testing-library/jest-dom";
3+
import { screen } from "@testing-library/react";
4+
import { useRouter } from "next/router";
5+
import List from "@/components/sections/submissions/List";
6+
import { submission } from "../../.../../../../__mocks__/fixtures/challenge";
7+
import React from "react";
8+
9+
jest.mock("next/router", () => ({
10+
useRouter: jest.fn(),
11+
}));
12+
13+
const mockState = {
14+
submissions: {
15+
current: {
16+
...submission,
17+
},
18+
list: [submission, submission, submission],
19+
hasMore: true,
20+
text: "",
21+
},
22+
};
23+
24+
const mockEmptyState = {
25+
submissions: {
26+
current: {
27+
...submission,
28+
},
29+
list: [],
30+
hasMore: false,
31+
text: "",
32+
},
33+
};
34+
35+
describe("List Component", () => {
36+
beforeEach(() => {
37+
(useRouter as jest.Mock).mockReturnValue({
38+
query: { challenge_id: "1" },
39+
});
40+
});
41+
42+
it("Should render the List component with submissions", () => {
43+
renderWithRedux(<List />, mockState);
44+
const listElement = screen.getByTestId("listId");
45+
expect(listElement).toBeInTheDocument();
46+
const submissionCardElements = screen.getAllByTestId("submissionId");
47+
expect(submissionCardElements.length).toBe(mockState.submissions.list.length);
48+
});
49+
50+
it("Should render the empty state when there are no submissions", () => {
51+
renderWithRedux(<List />, mockEmptyState);
52+
const emptyStateTitle = screen.getByText(/submissions.empty-state.title/i);
53+
expect(emptyStateTitle).toBeInTheDocument();
54+
});
55+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import View from "@/components/sections/submissions/View";
2+
import { submission } from "../../.../../../../__mocks__/fixtures/challenge";
3+
import { renderWithRedux } from "../.../../../../../__mocks__/renderWithRedux";
4+
import "@testing-library/jest-dom";
5+
import { screen } from "@testing-library/react";
6+
7+
jest.mock("next/router", () => ({
8+
useRouter: () => ({
9+
asPath: "next",
10+
}),
11+
}));
12+
13+
const mockStateWithSubmission = {
14+
submissions: {
15+
current: {
16+
...submission,
17+
},
18+
list: [submission],
19+
text: "",
20+
},
21+
};
22+
23+
const mockStateWithNoSubmission = {
24+
submissions: {
25+
current: null,
26+
list: [],
27+
text: "",
28+
},
29+
};
30+
describe("View component", () => {
31+
it("Should render the submission view card and evaluation", () => {
32+
renderWithRedux(<View />, mockStateWithSubmission);
33+
expect(screen.getByTestId("viewId")).toBeInTheDocument();
34+
// TODO: add this test once the pr for testing evaluations is merged
35+
// expect(screen.getByTestId("evaluationsId")).toBeInTheDocument();
36+
expect(screen.getByTestId("feedbackId")).toBeInTheDocument();
37+
});
38+
it("Should display a message when submission is not available", () => {
39+
renderWithRedux(<View />, mockStateWithNoSubmission);
40+
expect(screen.getByText("communities.challenge.submission.no.feedbacks")).toBeInTheDocument();
41+
});
42+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import "@testing-library/jest-dom";
2+
import { renderWithRedux } from "../../../../../__mocks__/renderWithRedux";
3+
import { screen } from "@testing-library/react";
4+
import SubmissionCard from "@/components/sections/submissions/_partials/SubmissionCard";
5+
import { submission } from "../../../../../__mocks__/fixtures/challenge";
6+
7+
jest.mock("next/router", () => ({
8+
useRouter: () => ({
9+
asPath: "next",
10+
query: { challenge_id: "test-challenge-id", slug: "test-community-slug" },
11+
pathname: "/communities/test-community-slug/challenges/test-challenge-id",
12+
}),
13+
}));
14+
15+
describe("Submission Card", () => {
16+
beforeEach(()=>{
17+
renderWithRedux(<SubmissionCard submission={submission} />);
18+
})
19+
it("should render submission card", () => {
20+
const submissionCard = screen.getByTestId("submissionCardId");
21+
expect(submissionCard).toBeInTheDocument();
22+
});
23+
it("should display the user's display name", () => {
24+
const displayName = screen.getByText(submission.user.displayName);
25+
expect(displayName).toBeInTheDocument();
26+
});
27+
28+
it("should display the submission text", () => {
29+
const submissionText = screen.getByText(submission.text);
30+
expect(submissionText).toBeInTheDocument();
31+
});
32+
33+
it("should display the submission status", () => {
34+
const submissionDate = screen.getByText(/submissions.submitted/);
35+
expect(submissionDate).toBeInTheDocument();
36+
});
37+
38+
it("should display evaluation points", () => {
39+
const evaluationPoints = screen.getByText(submission.metadata.evaluation.points.toString());
40+
expect(evaluationPoints).toBeInTheDocument();
41+
});
42+
43+
it("should have a link to the submission details", () => {
44+
const mockSubmission = submission;
45+
const communitySlug = "test-community-slug";
46+
const submissionId = mockSubmission.id;
47+
const submissionLink = screen.getByRole("link");
48+
expect(submissionLink).toBeInTheDocument();
49+
expect(submissionLink).toHaveAttribute("href", `/communities/${communitySlug}/challenges/test-challenge-id/submissions/${submissionId}`);
50+
});
51+
});

public/locales/bg/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
"communities.challenge.evaluation.points": "Points",
169169
"communities.challenge.evaluation.message": "Congratulations you gained at least 70% of the available learning points.",
170170
"communities.challenge.evaluation.message.nominated": "Поздравления, печеленето на {{threshold}}% ви номинира за наградния фонд от:",
171+
"communities.challenge.submission.no.feedbacks": "Обратната връзка за това подаване вече не е налична",
171172
"communities.submission.title": "Submissions",
172173
"communities.feedbacks": "Feedbacks",
173174
"communities.navigation.overview": "Преглед",

public/locales/en/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
"communities.challenge.evaluation.points": "Points",
170170
"communities.challenge.evaluation.message": "Congratulations you gained at least 70% of the available learning points.",
171171
"communities.challenge.evaluation.message.nominated": "Congratulations, earning {{threshold}}% nominates you for the prize pool of:",
172+
"communities.challenge.submission.no.feedbacks": "The feedback for this submission is no longer available",
172173
"communities.submission.title": "Submissions",
173174
"communities.feedbacks": "Feedbacks",
174175
"communities.navigation.overview": "Overview",

public/locales/es/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
"communities.challenge.evaluation.points": "Puntos",
173173
"communities.challenge.evaluation.message": "¡Felicidades, ha ganado al menos el 70% de los puntos disponibles!",
174174
"communities.challenge.evaluation.message.nominated": "Felicitaciones, ganar el {{threshold}}% te nominó para el fondo de premios de:",
175+
"communities.challenge.submission.no.feedbacks": "La retroalimentación para esta presentación ya no está disponible",
175176
"communities.submission.title": "Propuestas",
176177
"communities.feedbacks": "Feedbacks",
177178
"communities.navigation.overview": "Vista general",

public/locales/hr/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
"communities.challenge.evaluation.points": "Points",
142142
"communities.challenge.evaluation.message": "Congratulations you gained at least 70% of the available learning points.",
143143
"communities.challenge.evaluation.message.nominated": "Čestitamo, osvajanje >{{threshold}}% nominira vas za nagradni fond od:",
144+
"communities.challenge.submission.no.feedbacks": "Povratne informacije za ovu prijavu više nisu dostupne",
144145
"communities.submission.title": "Submissions",
145146
"communities.feedbacks": "Feedbacks",
146147
"communities.navigation.overview": "Pregled",

src/components/cards/Submission.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ interface SubmissionCardProps {
2323
date: string;
2424
};
2525
children?: ReactNode;
26+
testId?:string;
2627
}
2728

2829
/**
2930
* Submission card component
3031
* @return {ReactElement}
3132
*/
32-
export default function SubmissionCard({ submission, link = "", children }: SubmissionCardProps): ReactElement {
33+
export default function SubmissionCard({ submission, link = "", children, testId = "submissionId" }: SubmissionCardProps): ReactElement {
3334
const { t } = useTranslation();
3435
const router = useRouter();
3536
const colors = useSelector((state) => state.ui.colors);
@@ -79,7 +80,7 @@ export default function SubmissionCard({ submission, link = "", children }: Subm
7980
boxLayout
8081
onClick={displaySubmission}
8182
>
82-
<div className="divide-y divide-gray-200 flex flex-col">
83+
<div className="divide-y divide-gray-200 flex flex-col" data-testid={testId}>
8384
<div className="pb-5">
8485
<p className="text-base sm:text-lg line-clamp-3 leading-normal text-gray-700 break-words">{submission.text}</p>
8586
</div>

src/components/sections/feedbacks/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { Challenge } from "@/types/course";
1414
import { Feedback as FeedbackType } from "@/types/feedback";
1515
import { IRootState } from "@/store";
1616

17+
interface FeedbackProps {
18+
testId?: string;
19+
}
20+
1721
/**
1822
* interface for Feedback multiSelector
1923
* @date 9/13/2023 - 9:12:52 AM
@@ -35,7 +39,7 @@ interface FeedbackMultiSelector {
3539
* @export
3640
* @returns {ReactElement}
3741
*/
38-
export default function Feedback(): ReactElement {
42+
export default function Feedback({testId = "feedbackId" }: FeedbackProps): ReactElement {
3943
const dispatch = useDispatch();
4044
const route = useRouter();
4145

@@ -59,7 +63,7 @@ export default function Feedback(): ReactElement {
5963
}, [fetchList]);
6064

6165
return (
62-
<div className="relative">
66+
<div className="relative" data-testid={testId}>
6367
{isFetching ? <Loader loading={isFetching} /> : feedbacks.map((feedback, index) => <FeedbackCard key={feedback.id} value={feedback} last={index === feedbacks.length - 1} />)}
6468
{isAuthenticated && challenge?.feedbackInfo && (
6569
<Section>

src/components/sections/submissions/List.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ interface SubmissionListMultiSelector {
1515
submissions: Submission[];
1616
hasMore: boolean;
1717
}
18+
interface ListProps {
19+
testId?: string;
20+
}
1821
/**
1922
* Submissions list component
2023
* @date 4/25/2023 - 2:21:17 PM
2124
*
2225
* @export
2326
* @returns {ReactElement}
2427
*/
25-
export default function List(): ReactElement {
28+
export default function List({ testId = "listId" }: ListProps): ReactElement {
2629
const [page, setPage] = useState(1);
2730
const [loading, setLoading] = useState(false);
2831
const { t } = useTranslation();
@@ -58,7 +61,7 @@ export default function List(): ReactElement {
5861
return (
5962
<>
6063
{submissions && submissions.length ? (
61-
<div className="text-xl md:text-.5xl px-0 py-5 md:py-10 md:pb-5 relative">
64+
<div data-testid={testId} className="text-xl md:text-.5xl px-0 py-5 md:py-10 md:pb-5 relative">
6265
<InfiniteScroll
6366
dataLength={submissions.length}
6467
next={nextPage}

0 commit comments

Comments
 (0)