-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add test for the profile component #1256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
WalkthroughWalkthroughThe recent updates involved adding and enhancing mock data structures for testing various components and functionalities, including bounties, challenges, communities, courses, and feedback. Additionally, test cases were introduced or improved for components within the profile's achievements, and communities sections. Some adjustments were made to TypeScript interfaces, making certain properties optional. These changes aid in robust testing, improved test coverage, and enhanced flexibility in data structures. Changes
Warning Review ran into problemsProblems (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
✅ Deploy Preview for staging-dacade ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Outside diff range and nitpick comments (2)
src/components/sections/profile/achievements/LinkField.tsx (2)
Line range hint
2-3
: Consider importing types explicitly to avoid runtime imports.- import { useTranslation } from "next-i18next"; + import type { useTranslation } from "next-i18next";
Line range hint
47-47
: Specify the type attribute for the button to ensure correct behavior across browsers.- <button className="p-1 py-0 bg-white border border-blue-600 text-blue-600"> + <button type="button" className="p-1 py-0 bg-white border border-blue-600 text-blue-600">
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- tests/components/sections/profile/achievements/LinkField.test.tsx (1 hunks)
- src/components/sections/profile/achievements/LinkField.tsx (3 hunks)
Additional context used
Biome
src/components/sections/profile/achievements/LinkField.tsx
[error] 42-42: Enforce to have the onClick mouse event with the onKeyUp, the onKeyDown, or the onKeyPress keyboard event.
[error] 2-3: Some named imports are only used as types.
[error] 47-47: Provide an explicit type prop for the button element.
Additional comments not posted (3)
src/components/sections/profile/achievements/LinkField.tsx (1)
14-14
: The addition of the optionaltestId
prop with a default value enhances testability without breaking existing usages.__tests__/components/sections/profile/achievements/LinkField.test.tsx (2)
5-13
: The test for rendering the achievement link is comprehensive and correctly verifies the presence of the component and the link text.
23-32
: The test correctly checks for the presence of the link and button elements. Consider simplifying the window check.
@@ -37,7 +38,7 @@ export default function AchievementLinkField({ link }: AchievementLinkFieldProps | |||
const copy = () => navigator.clipboard.writeText(link as string); | |||
|
|||
return ( | |||
<div className="border relative p-2 rounded"> | |||
<div data-testid={testId} className="border relative p-2 rounded"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add keyboard accessibility to the clickable text element.
- <p className="text-gray-500 line-clamp-1 break-all flex-1 text-sm md:text-base overflow-hidden" onClick={copy}>
+ <p className="text-gray-500 line-clamp-1 break-all flex-1 text-sm md:text-base overflow-hidden" onClick={copy} onKeyUp={copy} tabIndex="0">
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
<div data-testid={testId} className="border relative p-2 rounded"> | |
<div data-testid={testId} className="border relative p-2 rounded"> | |
<p className="text-gray-500 line-clamp-1 break-all flex-1 text-sm md:text-base overflow-hidden" onClick={copy} onKeyUp={copy} tabIndex="0"> |
it("should copy the link to the clipboard when clicked", () => { | ||
render(<AchievementLinkField link={testLink} />); | ||
if(navigator.clipboard){ | ||
const mockCopy = async () => await navigator.clipboard.writeText(testLink) | ||
const linkElement = screen.getByText(testLink); | ||
fireEvent.click(linkElement); | ||
expect(mockCopy()).toHaveBeenCalled(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correctly mock the clipboard API to ensure the test is reliable.
- if(navigator.clipboard){
- const mockCopy = async () => await navigator.clipboard.writeText(testLink)
- const linkElement = screen.getByText(testLink);
- fireEvent.click(linkElement);
- expect(mockCopy()).toHaveBeenCalled();
- }
+ jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(() => Promise.resolve());
+ const linkElement = screen.getByText(testLink);
+ fireEvent.click(linkElement);
+ expect(navigator.clipboard.writeText).toHaveBeenCalledWith(testLink);
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
it("should copy the link to the clipboard when clicked", () => { | |
render(<AchievementLinkField link={testLink} />); | |
if(navigator.clipboard){ | |
const mockCopy = async () => await navigator.clipboard.writeText(testLink) | |
const linkElement = screen.getByText(testLink); | |
fireEvent.click(linkElement); | |
expect(mockCopy()).toHaveBeenCalled(); | |
} | |
it("should copy the link to the clipboard when clicked", () => { | |
render(<AchievementLinkField link={testLink} />); | |
jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(() => Promise.resolve()); | |
const linkElement = screen.getByText(testLink); | |
fireEvent.click(linkElement); | |
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(testLink); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
Outside diff range and nitpick comments (2)
src/components/sections/profile/communities/List.tsx (1)
Line range hint
4-10
: Optimize import statements.Since these imports are only used for type information, consider using TypeScript's
import type
to make this explicit and potentially optimize bundling.- import FeedbackCard from "@/components/cards/Feedback"; - import { useMultiSelector } from "@/hooks/useTypedSelector"; - import useNavigation from "@/hooks/useNavigation"; - import { useTranslation } from "next-i18next"; - import { ReactElement } from "react"; - import SubmissionCard from "@/components/cards/Submission"; - import { Community } from "@/types/community"; - import { Feedback } from "@/types/feedback"; - import { Submission } from "@/types/bounty"; - import { IRootState } from "@/store"; + import type FeedbackCard from "@/components/cards/Feedback"; + import type { useMultiSelector } from "@/hooks/useTypedSelector"; +import type useNavigation from "@/hooks/useNavigation"; + import type { useTranslation } from "next-i18next"; + import type { ReactElement } from "react"; + import type SubmissionCard from "@/components/cards/Submission"; + import type { Community } from "@/types/community"; + import type { Feedback } from "@/types/feedback"; + import type { Submission } from "@/types/bounty"; + import type { IRootState } from "@/store";src/types/bounty.d.ts (1)
Line range hint
113-113
: Refine the type forevaluation
inSubmissionMetadata
to avoid usingany
.- evaluation: any; + evaluation: Evaluation | null;
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (12)
- mocks/bounty.ts (1 hunks)
- mocks/challenge.ts (1 hunks)
- mocks/community.ts (1 hunks)
- mocks/course.ts (1 hunks)
- mocks/feedback.ts (1 hunks)
- tests/components/sections/profile/achievements/LinkField.test.tsx (1 hunks)
- tests/components/sections/profile/achievements/ListItem.test.tsx (1 hunks)
- tests/components/sections/profile/communities/List.test.tsx (1 hunks)
- src/components/sections/profile/achievements/ListItem.tsx (2 hunks)
- src/components/sections/profile/communities/List.tsx (2 hunks)
- src/types/bounty.d.ts (1 hunks)
- src/types/community.d.ts (1 hunks)
Files skipped from review due to trivial changes (2)
- mocks/bounty.ts
- tests/components/sections/profile/achievements/ListItem.test.tsx
Files skipped from review as they are similar to previous changes (1)
- tests/components/sections/profile/achievements/LinkField.test.tsx
Additional context used
Biome
__mocks__/feedback.ts
[error] 1-1: All these imports are only used as types.
src/components/sections/profile/achievements/ListItem.tsx
[error] 34-34: Do not use template literals if interpolation and special-character handling are not needed.
[error] 34-34: Do not use template literals if interpolation and special-character handling are not needed.
[error] 34-34: Do not use template literals if interpolation and special-character handling are not needed.
[error] 1-1: All these imports are only used as types.
src/types/community.d.ts
[error] 24-24: Unexpected any. Specify a different type.
[error] 1-1: All these imports are only used as types.
[error] 1-2: All these imports are only used as types.
__tests__/components/sections/profile/communities/List.test.tsx
[error] 24-27: Prefer for...of instead of forEach.
__mocks__/community.ts
[error] 1-1: All these imports are only used as types.
src/components/sections/profile/communities/List.tsx
[error] 4-5: All these imports are only used as types.
[error] 6-7: All these imports are only used as types.
[error] 7-8: All these imports are only used as types.
[error] 8-9: All these imports are only used as types.
[error] 9-10: All these imports are only used as types.
src/types/bounty.d.ts
[error] 113-113: Unexpected any. Specify a different type.
[error] 1-1: All these imports are only used as types.
[error] 1-2: All these imports are only used as types.
[error] 2-3: All these imports are only used as types.
[error] 3-4: All these imports are only used as types.
[error] 4-5: All these imports are only used as types.
[error] 5-6: All these imports are only used as types.
__mocks__/course.ts
[error] 1-1: All these imports are only used as types.
__mocks__/challenge.ts
[error] 103-105: This function expression can be turned into an arrow function.
[error] 4-5: All these imports are only used as types.
[error] 6-7: All these imports are only used as types.
Additional comments not posted (18)
src/components/sections/profile/achievements/ListItem.tsx (1)
16-16
: Addition oftestId
prop enhances testability.The addition of the
testId
prop is a good practice for making components more testable. This change is well-implemented.Also applies to: 33-33
src/types/community.d.ts (1)
25-26
: Optional properties enhance flexibility.Making
challenge
andsubmission
properties optional in theCommunity
interface allows for more flexible data handling, which is a positive change.__tests__/components/sections/profile/communities/List.test.tsx (1)
14-29
: Test implementation forSubmissionList
looks comprehensive.The test covers various scenarios and properly checks for the presence of elements, which is crucial for ensuring the UI behaves as expected.
src/components/sections/profile/communities/List.tsx (1)
29-29
: Addition oftestId
prop enhances testability.The addition of the
testId
prop is a good practice for making components more testable. This change is well-implemented.src/types/bounty.d.ts (1)
157-157
: Making thereferrals
property optional enhances flexibility in handling user data where referral information might not be available.__mocks__/course.ts (8)
5-7
: TheIntroduction
mock provides a clear and concise description for course introductions.
9-12
: ThemockCertificateData
is well-structured and provides necessary details for testing certificate-related features.
14-26
: TheRubric
mock is comprehensive and covers all necessary fields for testing assessment criteria.
28-33
: ThemockRatingCriteria
is effectively structured to facilitate testing of rating systems in courses.
35-50
: TheMaterialType
enum andmockMaterial
provide a detailed and versatile set of mock data for testing various material types in courses.
52-75
: TheInteractiveModule
mock is well-designed to test interactive elements within course modules.
77-92
: ThemockLearningModule
is thoroughly detailed, providing a robust framework for testing learning modules in courses.
94-128
: ThemockCourse
is extensively detailed, covering all aspects of a course, from basic information to interactive modules and FAQs, providing a comprehensive set for testing.__mocks__/challenge.ts (5)
9-24
: ThefixtureUser
function provides a comprehensive set of user properties for testing, enhancing the realism of user-related test scenarios.
26-66
: Thechallenge
function is well-structured, offering a detailed and realistic setup for testing challenge-related features in the application.
68-106
: ThefixtureSubmission
function is meticulously detailed, providing a robust framework for testing submissions, including user interactions and metadata handling.
107-117
: ThemockInvite
object is effectively structured to facilitate testing of invitation features within team challenges.
119-144
: ThemockTeamMember
andmockTeam
mocks are well-designed, providing detailed and realistic data for testing team dynamics and functionalities in challenges.
@@ -0,0 +1,28 @@ | |||
import { Feedback } from "@/types/feedback"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize import statements.
Since these imports are only used for type information, consider using TypeScript's import type
to make this explicit and potentially optimize bundling.
- import { Feedback } from "@/types/feedback";
- import { fixtureSubmission, fixtureUser } from "./challenge";
- import { reward } from "./reward";
+ import type { Feedback } from "@/types/feedback";
+ import type { fixtureSubmission, fixtureUser } from "./challenge";
+ import type { reward } from "./reward";
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
import { Feedback } from "@/types/feedback"; | |
import type { Feedback } from "@/types/feedback"; | |
import type { fixtureSubmission, fixtureUser } from "./challenge"; | |
import type { reward } from "./reward"; |
@@ -29,13 +30,13 @@ interface AchievementViewItemProps { | |||
} | |||
* @returns {*} | |||
*/ | |||
export default function AchievementViewItem({ name, columns = 3, mobileBlock = false, itemsStart = false, children }: AchievementViewItemProps): ReactElement { | |||
export default function AchievementViewItem({ name, columns = 3, mobileBlock = false, itemsStart = false, children, testId = "achievementViewItemId" }: AchievementViewItemProps): ReactElement { | |||
const gridClasses = !mobileBlock ? [`grid`, `grid-cols-${columns}`] : [`grid`, `grid-cols-1 md:grid-cols-${columns}`, `gap-y-3 md:gap-y-0`]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize string handling in class names.
Avoid using template literals where simple strings suffice. This can improve performance slightly and makes the code cleaner.
- const gridClasses = !mobileBlock ? [`grid`, `grid-cols-${columns}`] : [`grid`, `grid-cols-1 md:grid-cols-${columns}`, `gap-y-3 md:gap-y-0`];
+ const gridClasses = !mobileBlock ? ['grid', `grid-cols-${columns}`] : ['grid', 'grid-cols-1 md:grid-cols-${columns}', 'gap-y-3 md:gap-y-0'];
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
const gridClasses = !mobileBlock ? [`grid`, `grid-cols-${columns}`] : [`grid`, `grid-cols-1 md:grid-cols-${columns}`, `gap-y-3 md:gap-y-0`]; | |
const gridClasses = !mobileBlock ? ['grid', `grid-cols-${columns}`] : ['grid', 'grid-cols-1 md:grid-cols-${columns}', 'gap-y-3 md:gap-y-0']; |
map: function () { | ||
throw new Error("Function not implemented."); | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert the function expression to an arrow function for consistency and modern JavaScript practices.
- map: function () {
+ map: () => {
throw new Error("Function not implemented.");
},
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
map: function () { | |
throw new Error("Function not implemented."); | |
}, | |
map: () => { | |
throw new Error("Function not implemented."); | |
}, |
submission.forEach((submissions) => { | ||
console.log("Checking Submission: ", screen.getByText(submissions.text)); | ||
expect(screen.getByText(submissions.text)).toBeInTheDocument() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use for...of
for better handling of asynchronous operations.
Using for...of
instead of forEach
can handle asynchronous operations more predictably and is generally recommended in tests.
- submission.forEach((submissions) => {
- console.log("Checking Submission: ", screen.getByText(submissions.text));
- expect(screen.getByText(submissions.text)).toBeInTheDocument()
- })
+ for (const submission of submissions) {
+ console.log("Checking Submission: ", screen.getByText(submission.text));
+ expect(screen.getByText(submission.text)).toBeInTheDocument();
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
submission.forEach((submissions) => { | |
console.log("Checking Submission: ", screen.getByText(submissions.text)); | |
expect(screen.getByText(submissions.text)).toBeInTheDocument() | |
}) | |
for (const submission of submissions) { | |
console.log("Checking Submission: ", screen.getByText(submission.text)); | |
expect(screen.getByText(submission.text)).toBeInTheDocument(); | |
} |
@@ -0,0 +1,57 @@ | |||
import { Community } from "@/types/community"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize import statements.
Since these imports are only used for type information, consider using TypeScript's import type
to make this explicit and potentially optimize bundling.
- import { Community } from "@/types/community";
+ import type { Community } from "@/types/community";
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
import { Community } from "@/types/community"; | |
import type { Community } from "@/types/community"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (1)
- tests/components/sections/profile/communities/List.test.tsx (1 hunks)
Additional comments not posted (1)
__tests__/components/sections/profile/communities/List.test.tsx (1)
14-30
: Refactor the test to usefor...of
for iteration and improve readability.The test correctly sets up the environment and performs assertions. However, the use of
forEach
for asynchronous operations is not ideal. Refactoring it to use afor...of
loop can improve handling of potential asynchronous operations and enhance readability.Additionally, consider adding more descriptive test names and assertions to better document the behavior being tested.
- submission.forEach((submissions) => { - expect(screen.getByText(submissions.reward.amount + submissions.reward.token)).toBeInTheDocument() - }) + for (const submission of submissions) { + expect(screen.getByText(submission.reward.amount + submission.reward.token)).toBeInTheDocument(); + }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (2)
- tests/components/sections/profile/communities/List.test.tsx (1 hunks)
- src/components/sections/profile/communities/List.tsx (2 hunks)
Files skipped from review as they are similar to previous changes (1)
- tests/components/sections/profile/communities/List.test.tsx
Additional comments not posted (2)
src/components/sections/profile/communities/List.tsx (2)
29-29
: Good addition oftestId
for testing purposes.The introduction of the
testId
prop with a default value enhances the testability of theSubmissionList
component, which aligns well with the objective of adding tests for the profile component. This change should not affect existing functionality but improves maintainability and the ease of testing.
40-40
: Ensure usage of thetestId
prop does not interfere with existing styles or behaviors.While the
testId
prop is a valuable addition for testing, it's important to verify that its presence in the DOM does not inadvertently affect any CSS styles or JavaScript behaviors that might be targeting thediv
element more generally.#!/bin/bash # Description: Check for any CSS or JS that might be targeting the div elements specifically without considering the `testId`. # Test: Search for CSS or JS that targets div elements in the project. rg --type css,js 'div' | grep -v 'testId'
Submit a pull request
Replace any ":question:" below with information about your pull request.
Pull Request Details
Provide details about your pull request and what it adds, fixes, or changes.
❓
Breaking Changes
Describe what features are broken by this pull request and why, if any.
❓
Issues Fixed
Enter the issue numbers resolved by this pull request below, if any.
Other Relevant Information
Provide any other important details below.