Skip to content
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

Fix/test courses new #1271

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
Open

Fix/test courses new #1271

wants to merge 11 commits into from

Conversation

gabrielaacha
Copy link
Contributor

@gabrielaacha gabrielaacha commented Jun 17, 2024

Submit a pull request

  • [ x] This is not a duplicate of an existing pull request.
  • [ x] No existing features have been broken without good reason.
  • [ x] Your commit messages are detailed
  • [ x] The code style guideline have been followed.
  • [ x] Documentation has been updated to reflect your changes.
  • [ x] Tests have been added or updated to reflect your changes.
  • [ x] All tests pass.

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.

Summary by CodeRabbit

  • Tests

    • Introduced new test cases for various components within the course sections, including CommunityNavigation, MobileNav, Navigation, Challenge, Description, Disclaimer, LearningModules, Objectives, Prerequisite, Rewards, Trailer, Wrapper, and PageNavigation.
  • Enhancements

    • Added data-testid attributes to multiple components to improve testability, including CommunityNavigation, PageNavigation, Wrapper, Challenge, Description, Disclaimer, LearningModules, Objectives, Prerequisite, Rewards, and more.
  • New Features

    • Introduced mock data and a ReduxProvider component for improved state management in a React application.

Copy link

netlify bot commented Jun 17, 2024

Deploy Preview for staging-dacade ready!

Name Link
🔨 Latest commit 3ccb28e
🔍 Latest deploy log https://app.netlify.com/sites/staging-dacade/deploys/66701737e9bda00008afbc58
😎 Deploy Preview https://deploy-preview-1271--staging-dacade.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

coderabbitai bot commented Jun 17, 2024

Walkthrough

The changes encompass adding new mock data files and extensive testing for various course-related components. The modifications include enhancements to existing components by adding data-testid attributes to facilitate improved testability. Additionally, a new ReduxProvider component is introduced for managing state within a React application using Redux.

Changes

Files/Paths Change Summary
__mocks__/bounty.ts, course.ts, provider/... Introduced new mock data for course and bounty details, added ReduxProvider component for state management
__tests__/components/sections/courses/... Added comprehensive test cases for components like CommunityNavigation, MobileNav, Overview subcomponents
src/components/sections/courses/... Added data-testid attributes to various components to enhance testability, and minor updates like styling

Sequence Diagrams

sequenceDiagram
    participant User
    participant ReduxProvider
    participant Component
    participant TestingLibrary

    User ->> ReduxProvider: Wrap components
    ReduxProvider ->> Component: Provide store
    User ->> TestingLibrary: Render Component
    TestingLibrary ->> Component: Pass `data-testid`
    Component -->> User: Display Components with Test IDs
Loading

Poem

Amidst the code where logic weaves,
🌟 New mocks were born, like bright new leaves.
Tests now dance with IDs neat,
Ensuring our components don't miss a beat.
With Redux magic, state aligns,
Together our code so brightly shines. 🐇🖥️


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 18

Outside diff range and nitpick comments (4)
src/components/sections/courses/overview/Prerequisite.tsx (1)

Line range hint 28-28: Avoid using dangerouslySetInnerHTML due to potential XSS vulnerabilities. Consider sanitizing the HTML or using a safer alternative to inject dynamic content.

- <span dangerouslySetInnerHTML={{ __html: course.prerequisite.hint }} />
+ <span>{sanitizeHtml(course.prerequisite.hint)}</span>
__tests__/components/sections/courses/Overview/Prerequisite.test.tsx (1)

32-33: The test for ObjectiveList rendering is good. Consider adding assertions to check specific attributes or behaviors of the list to ensure more robust testing.

__tests__/components/sections/courses/PageNavigation.test.tsx (1)

66-70: The test descriptions should clarify the difference between previous and next links.

Consider renaming the tests to specify which navigation link is being tested, e.g., 'should render page nav link for previous page'.

Also applies to: 74-78

src/components/sections/courses/PageNavigation.tsx (1)

Line range hint 43-43: Simplify the mapping and flattening of arrays.

- const list = useMemo(() => menus?.map((menu) => menu?.items).flat(), [menus]);
+ const list = useMemo(() => menus?.flatMap((menu) => menu?.items), [menus]);

Replacing .map().flat() with .flatMap() simplifies the code and potentially improves performance by reducing the complexity of array operations.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e6098a8 and 3ccb28e.

Files selected for processing (29)
  • mocks/bounty.ts (1 hunks)
  • mocks/course.ts (1 hunks)
  • mocks/provider/ReduxProvider.tsx (1 hunks)
  • tests/components/sections/courses/CommunityNavigation.test.tsx (1 hunks)
  • tests/components/sections/courses/MobileNav.test.tsx (1 hunks)
  • tests/components/sections/courses/Navigation.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/Challenge.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/Description.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/Disclaimer.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/LearningModules.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/Objectives.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/Prerequisite.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/Rewards.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/Trailer.test.tsx (1 hunks)
  • tests/components/sections/courses/Overview/index.test.tsx (1 hunks)
  • tests/components/sections/courses/PageNavigation.test.tsx (1 hunks)
  • tests/components/sections/courses/Wrapper.test.tsx (1 hunks)
  • src/components/sections/courses/CommunityNavigation.tsx (2 hunks)
  • src/components/sections/courses/PageNavigation.tsx (1 hunks)
  • src/components/sections/courses/Wrapper.tsx (1 hunks)
  • src/components/sections/courses/overview/Challenge.tsx (1 hunks)
  • src/components/sections/courses/overview/Description.tsx (1 hunks)
  • src/components/sections/courses/overview/Disclaimer.tsx (1 hunks)
  • src/components/sections/courses/overview/LearningModules.tsx (1 hunks)
  • src/components/sections/courses/overview/Objectives.tsx (1 hunks)
  • src/components/sections/courses/overview/Prerequisite.tsx (1 hunks)
  • src/components/sections/courses/overview/Rewards.tsx (1 hunks)
  • src/components/sections/courses/overview/Trailer.tsx (1 hunks)
  • src/components/sections/courses/overview/index.tsx (1 hunks)
Files skipped from review due to trivial changes (4)
  • mocks/bounty.ts
  • tests/components/sections/courses/Navigation.test.tsx
  • tests/components/sections/courses/Overview/Challenge.test.tsx
  • tests/components/sections/courses/Overview/Description.test.tsx
Additional context used
Biome
src/components/sections/courses/overview/Challenge.tsx

[error] 17-17: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

src/components/sections/courses/overview/Disclaimer.tsx

[error] 24-24: Avoid passing content using the dangerouslySetInnerHTML prop. (lint/security/noDangerouslySetInnerHtml)

Setting content using code can expose users to cross-site scripting (XSS) attacks

src/components/sections/courses/overview/Rewards.tsx

[error] 21-21: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

src/components/sections/courses/overview/Prerequisite.tsx

[error] 28-28: Avoid passing content using the dangerouslySetInnerHTML prop. (lint/security/noDangerouslySetInnerHtml)

Setting content using code can expose users to cross-site scripting (XSS) attacks

src/components/sections/courses/overview/LearningModules.tsx

[error] 27-30: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

__tests__/components/sections/courses/Wrapper.test.tsx

[error] 13-13: Avoid passing children using a prop (lint/correctness/noChildrenProp)

The canonical way to pass children in React is to use JSX elements

src/components/sections/courses/overview/Trailer.tsx

[error] 22-22: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.


[error] 30-30: Avoid passing content using the dangerouslySetInnerHTML prop. (lint/security/noDangerouslySetInnerHtml)

Setting content using code can expose users to cross-site scripting (XSS) attacks

src/components/sections/courses/PageNavigation.tsx

[error] 43-43: The call chain .map().flat() can be replaced with a single .flatMap() call. (lint/complexity/useFlatMap)

Safe fix: Replace the chain with .flatMap().

Additional comments not posted (26)
__mocks__/provider/ReduxProvider.tsx (1)

5-11: The implementation of ReduxProvider looks clean and follows best practices for context provisioning in React with Redux.

__tests__/components/sections/courses/Overview/Disclaimer.test.tsx (1)

6-10: The test case for rendering the Disclaimer component is correctly implemented and follows best practices for component testing with React Testing Library.

src/components/sections/courses/overview/Description.tsx (1)

19-19: The addition of data-testid attributes enhances the testability of the Description component, aligning with best practices for React component development.

src/components/sections/courses/overview/Objectives.tsx (1)

20-21: Added data-testid attributes enhance testability and maintainability of the component.

__tests__/components/sections/courses/Overview/Objectives.test.tsx (1)

12-29: Test cases correctly utilize data-testid attributes to ensure the Objectives component and its sub-components render as expected.

src/components/sections/courses/overview/Prerequisite.tsx (1)

21-23: The addition of data-testid attributes enhances testability. Good use of React's context and hooks for fetching and displaying the prerequisite information dynamically.

__tests__/components/sections/courses/Overview/Prerequisite.test.tsx (1)

26-28: The test case correctly checks for the presence of the Prerequisite component using the data-testid. Ensure all scenarios are covered in your tests, including negative cases and edge cases.

src/components/sections/courses/Wrapper.tsx (1)

29-37: Good use of responsive design and data-testid for testability. The structure is clean and modular, making it easy to maintain and understand.

__tests__/components/sections/courses/Overview/LearningModules.test.tsx (4)

1-6: Imports are correctly utilized and cover all dependencies needed for the tests.


8-11: The LearningModuleProps interface is appropriately defined with types suitable for the testing context.


13-16: The RenderLearningModule function is correctly implemented, ensuring that the LearningModulesCard is rendered with the necessary props.


18-30: Test cases are well-structured and effectively validate both the presence and the content of the Learning Modules components.

src/components/sections/courses/CommunityNavigation.tsx (2)

Line range hint 18-37: The CommunityNavigation component is logically structured and efficiently uses Redux and useMemo for deriving navigation paths based on community data.


18-26: The use of data-testid attributes in the CommunityNavigation component enhances testability by allowing specific elements to be targeted in tests.

__tests__/components/sections/courses/MobileNav.test.tsx (3)

9-14: The mocking of next/router is appropriately set up to simulate routing behavior, which is crucial for testing the MobileNav component that interacts with routing.


16-23: The RenderMobileNav function is well-implemented, correctly using ReduxProvider to ensure that Redux state is available for the MobileNav component during testing.


25-42: The test cases are comprehensive and effectively check the rendering and presence of the MobileNav component and related icons, ensuring that UI elements are correctly integrated.

src/components/sections/courses/overview/LearningModules.tsx (1)

21-29: The LearningModules component is well-implemented, efficiently using conditional rendering based on course data and effectively integrating translations and Redux state.

__tests__/components/sections/courses/CommunityNavigation.test.tsx (2)

17-31: Ensure the ReduxProvider is correctly implemented to wrap the CommunityNavigation component for state management in tests.


33-37: Verify that the data-testid for ChevronRightIcon is correctly set in the component to match communityNavId.

Verification successful

The data-testid for ChevronRightIcon is correctly set to communityNavId in the component definition.

  • src/components/sections/courses/CommunityNavigation.tsx: <ChevronRightIcon viewBox="0 0 20 20" className="w-3 h-3" data-testid="communityNavId"/>
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Confirm the data-testid 'communityNavId' is set for ChevronRightIcon in the component definition.

# Test: Search for ChevronRightIcon definition and check for data-testid.
rg --type python $'ChevronRightIcon.*data-testid="communityNavId"'

Length of output: 98



Script:

#!/bin/bash
# Description: Confirm the data-testid 'communityNavId' is set for ChevronRightIcon in the component definition.

# Test: Search for ChevronRightIcon definition and check for data-testid.
rg --type tsx --type jsx 'ChevronRightIcon.*data-testid="communityNavId"'

Length of output: 103



Script:

#!/bin/bash
# Description: Confirm the data-testid 'communityNavId' is set for ChevronRightIcon in the component definition.

# Test: Search for ChevronRightIcon definition and check for data-testid.
rg --type typescript --type javascript 'ChevronRightIcon.*data-testid="communityNavId"'

Length of output: 124



Script:

#!/bin/bash
# Description: Confirm the data-testid 'communityNavId' is set for ChevronRightIcon in the component definition.

# Test: Search for ChevronRightIcon definition and check for data-testid.
rg --glob '*.tsx' --glob '*.jsx' 'ChevronRightIcon.*data-testid="communityNavId"'

Length of output: 242

src/components/sections/courses/overview/index.tsx (1)

26-34: Ensure all data-testid attributes are set correctly and consistently for each section to facilitate targeted testing.

__tests__/components/sections/courses/Wrapper.test.tsx (1)

20-24: Ensure that each component is tested for its specific functionality and interactions.

Also applies to: 27-42

__tests__/components/sections/courses/PageNavigation.test.tsx (1)

34-47: Ensure that ArrowButton is rendered with appropriate props and the Redux store.

__mocks__/course.ts (1)

5-128: Ensure all mock data aligns with their respective type definitions.

__tests__/components/sections/courses/Overview/index.test.tsx (1)

23-25: Refactor to use destructuring for props for clarity and maintainability.

- render(<Header {...props} title={[mockCourse.name]} description={[mockCourse.description]}/>);
+ const { title, description } = props;
+ render(<Header title={[title]} description={[description]}/>);

Likely invalid or redundant comment.

src/components/sections/courses/PageNavigation.tsx (1)

109-109: Add data-testid attribute to enhance testability.

This change facilitates easier and more reliable testing by providing a specific identifier for the navigation div, allowing tests to accurately target and verify its presence and attributes.

{prevUrl && (
<Link href={prevUrl}>
<Link href={prevUrl} data-testId="pageNavLinkId">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure consistency in attribute naming.

- <Link href={prevUrl} data-testId="pageNavLinkId">
+ <Link href={prevUrl} data-testid="pageNavLinkId">
- <Link href={nextUrl} data-testId="pageNavLinkId">
+ <Link href={nextUrl} data-testid="pageNavLinkId">

The attribute for selecting elements in tests should consistently be data-testid, not data-testId. This helps maintain consistency and avoid potential issues in test scripts that might be case-sensitive.

Also applies to: 118-118

Comment on lines +12 to +15
it("should show disclaimer HTML", () => {
const courseDisclaimer = screen.getByTestId("disclaimerSpanId");
expect(courseDisclaimer).toBeInTheDocument();
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure components are rendered before attempting to access them in tests.

+ render(<Disclaimer />);
  const courseDisclaimer = screen.getByTestId("disclaimerSpanId");
  expect(courseDisclaimer).toBeInTheDocument();

Committable suggestion was skipped due to low confidence.

Comment on lines +18 to +19
<Section data-testid="challengeDescriptionId" title={t("communities.overview.challenge")}>
<span data-testid="challengeDescription" className="block mt-3 text-base font-normal md:text-lg">{course.challenge.description}</span>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using an optional chain when accessing course.challenge.description to prevent runtime errors if challenge is undefined.

- <span data-testid="challengeDescription" className="block mt-3 text-base font-normal md:text-lg">{course.challenge.description}</span>
+ <span data-testid="challengeDescription" className="block mt-3 text-base font-normal md:text-lg">{course.challenge?.description}</span>
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<Section data-testid="challengeDescriptionId" title={t("communities.overview.challenge")}>
<span data-testid="challengeDescription" className="block mt-3 text-base font-normal md:text-lg">{course.challenge.description}</span>
<Section data-testid="challengeDescriptionId" title={t("communities.overview.challenge")}>
<span data-testid="challengeDescription" className="block mt-3 text-base font-normal md:text-lg">{course.challenge?.description}</span>

Comment on lines +21 to +24
<Section data-testid="disclaimerId" className="!pb-12 !pt-0">
<Hint>
<strong>{t("communities.overview.info.disclaimer.title")}: </strong>
<span dangerouslySetInnerHTML={{ __html: course?.disclaimer as string }} />
<span data-testid="disclaimerSpanId" dangerouslySetInnerHTML={{ __html: course?.disclaimer as string }} />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that content passed to dangerouslySetInnerHTML is sanitized to prevent XSS attacks.

- <span data-testid="disclaimerSpanId" dangerouslySetInnerHTML={{ __html: course?.disclaimer as string }} />
+ <span data-testid="disclaimerSpanId">{sanitizeHtml(course?.disclaimer)}</span>

Consider implementing or using a library function sanitizeHtml to safely render HTML content.

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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<Section data-testid="disclaimerId" className="!pb-12 !pt-0">
<Hint>
<strong>{t("communities.overview.info.disclaimer.title")}: </strong>
<span dangerouslySetInnerHTML={{ __html: course?.disclaimer as string }} />
<span data-testid="disclaimerSpanId" dangerouslySetInnerHTML={{ __html: course?.disclaimer as string }} />
<Section data-testid="disclaimerId" className="!pb-12 !pt-0">
<Hint>
<strong>{t("communities.overview.info.disclaimer.title")}: </strong>
<span data-testid="disclaimerSpanId">{sanitizeHtml(course?.disclaimer)}</span>
Tools
Biome

[error] 24-24: Avoid passing content using the dangerouslySetInnerHTML prop. (lint/security/noDangerouslySetInnerHtml)

Setting content using code can expose users to cross-site scripting (XSS) attacks

Comment on lines +47 to +49
const trailerVideoSummary = screen.getByTestId("trailerVideoSummary");
expect(trailerVideoSummary).toBeInTheDocument();
expect(trailerVideoSummary).toBe(mockCourse.trailer.description);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion should use toEqual for checking equality of content.

- expect(trailerVideoSummary).toBe(mockCourse.trailer.description);
+ expect(trailerVideoSummary.textContent).toEqual(mockCourse.trailer.description);
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const trailerVideoSummary = screen.getByTestId("trailerVideoSummary");
expect(trailerVideoSummary).toBeInTheDocument();
expect(trailerVideoSummary).toBe(mockCourse.trailer.description);
const trailerVideoSummary = screen.getByTestId("trailerVideoSummary");
expect(trailerVideoSummary).toBeInTheDocument();
expect(trailerVideoSummary.textContent).toEqual(mockCourse.trailer.description);

Comment on lines +21 to +29
<Section data-testid="learningModulesId">
<div className="grid flex-wrap grid-cols-1 gap-5 sm:grid-cols-2">
<div className="flex flex-col w-full mt-0 mr-4 md:pt-4">
<h4 className="font-medium inline-block text-.5xl">{t("communities.overview.learning-modules")}</h4>
<span className="max-w-xs mt-4 mb-2 text-sm rounded-3xl">{t("communities.overview.learning-modules-description")}</span>
</div>
{course.learningModules &&
course.learningModules.map((learningModule, index) => {
return <LearningModuleCard key={`module-${index}`} learningModule={learningModule} />;
return <LearningModuleCard data-testid="learningModulesCardId" key={`module-${index}`} learningModule={learningModule} />;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using an optional chain for course.learningModules.map to ensure safety in cases where course.learningModules might be undefined.

- course.learningModules.map((learningModule, index) => {
+ course.learningModules?.map((learningModule, index) => {

This change prevents potential runtime errors if course.learningModules is undefined.

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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<Section data-testid="learningModulesId">
<div className="grid flex-wrap grid-cols-1 gap-5 sm:grid-cols-2">
<div className="flex flex-col w-full mt-0 mr-4 md:pt-4">
<h4 className="font-medium inline-block text-.5xl">{t("communities.overview.learning-modules")}</h4>
<span className="max-w-xs mt-4 mb-2 text-sm rounded-3xl">{t("communities.overview.learning-modules-description")}</span>
</div>
{course.learningModules &&
course.learningModules.map((learningModule, index) => {
return <LearningModuleCard key={`module-${index}`} learningModule={learningModule} />;
return <LearningModuleCard data-testid="learningModulesCardId" key={`module-${index}`} learningModule={learningModule} />;
<Section data-testid="learningModulesId">
<div className="grid flex-wrap grid-cols-1 gap-5 sm:grid-cols-2">
<div className="flex flex-col w-full mt-0 mr-4 md:pt-4">
<h4 className="font-medium inline-block text-.5xl">{t("communities.overview.learning-modules")}</h4>
<span className="max-w-xs mt-4 mb-2 text-sm rounded-3xl">{t("communities.overview.learning-modules-description")}</span>
</div>
{course.learningModules &&
course.learningModules?.map((learningModule, index) => {
return <LearningModuleCard data-testid="learningModulesCardId" key={`module-${index}`} learningModule={learningModule} />;

Comment on lines +11 to +17
it("should render the Wrapper", () => {
// eslint-disable-next-line react/no-children-prop
render( <Wrapper children={undefined} />);
const wrapper = screen.getByTestId("wrapperId");
expect(wrapper).toBeInTheDocument();
expect(screen.getByPlaceholderText("wrapperSectionId")).toBeInTheDocument();
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the usage of children prop, use JSX children instead.

- render( <Wrapper children={undefined} />);
+ render( <Wrapper>{undefined}</Wrapper>);
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("should render the Wrapper", () => {
// eslint-disable-next-line react/no-children-prop
render( <Wrapper children={undefined} />);
const wrapper = screen.getByTestId("wrapperId");
expect(wrapper).toBeInTheDocument();
expect(screen.getByPlaceholderText("wrapperSectionId")).toBeInTheDocument();
});
it("should render the Wrapper", () => {
// eslint-disable-next-line react/no-children-prop
render( <Wrapper>{undefined}</Wrapper>);
const wrapper = screen.getByTestId("wrapperId");
expect(wrapper).toBeInTheDocument();
expect(screen.getByPlaceholderText("wrapperSectionId")).toBeInTheDocument();
});
Tools
Biome

[error] 13-13: Avoid passing children using a prop (lint/correctness/noChildrenProp)

The canonical way to pass children in React is to use JSX elements

Comment on lines +27 to +42
it("Should render Navigation component", () => {
render(<MobileNav />);
const inputElement = screen.getByPlaceholderText("showTopBorder");
expect(inputElement).toBeInTheDocument();
const mobileNav = screen.getByText("MobileNavId");
expect(mobileNav).toBeInTheDocument();
});


it("Should render Navigation component", () => {
render(<CommunityNavigation />);
const inputElement = screen.getByPlaceholderText("paths");
expect(inputElement).toBeInTheDocument();
const communityNav = screen.getByTestId("communityNavId");
expect(communityNav).toBeInTheDocument();
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The search results confirm that the data-testid="communityNavId" is present in the CommunityNavigation component. However, there was no match for placeholder="showTopBorder" in the MobileNav component.

  • The CommunityNavigation component correctly implements the test ID communityNavId.
  • The MobileNav component does not have a placeholder showTopBorder.

Locations in the codebase:

  • src/components/sections/courses/CommunityNavigation.tsx: Correct implementation of communityNavId.
  • src/components/sections/courses/Wrapper.tsx: Another occurrence of communityNavId.
Analysis chain

Confirm that the placeholders and test IDs are correctly implemented in the MobileNav and CommunityNavigation components.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Confirm the placeholders and test IDs are correctly implemented.

# Test: Search for placeholders and test IDs in the component definitions.
rg --type python $'screen.getByPlaceholderText("showTopBorder")'
rg --type python $'screen.getByTestId("communityNavId")'

Length of output: 185



Script:

#!/bin/bash
# Description: Confirm the placeholders and test IDs are correctly implemented.

# Test: Search for placeholders and test IDs in the component definitions.
rg --type tsx 'getByPlaceholderText("showTopBorder")'
rg --type tsx 'getByTestId("communityNavId")'

Length of output: 159



Script:

#!/bin/bash
# Description: Confirm the placeholders and test IDs are correctly implemented.

# Test: Search for placeholders and test IDs in the component definitions.
rg 'getByPlaceholderText("showTopBorder")'
rg 'getByTestId("communityNavId")'

Length of output: 81



Script:

#!/bin/bash
# Description: Locate the component definitions for `MobileNav` and `CommunityNavigation`.

# Search for the `MobileNav` component definition.
rg 'MobileNav' --files-with-matches | xargs rg 'placeholder="showTopBorder"'

# Search for the `CommunityNavigation` component definition.
rg 'CommunityNavigation' --files-with-matches | xargs rg 'data-testid="communityNavId"'

Length of output: 439


{course.trailer.description && (
<div
<div data-testid="trailerTextId"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using dangerouslySetInnerHTML due to potential XSS vulnerabilities.

- <div dangerouslySetInnerHTML={{ __html: course.trailer.description }} />
+ <div>{course.trailer.description}</div>

Committable suggestion was skipped due to low confidence.

Comment on lines +23 to +29
<Section data-testid="trailerId" title={t("communities.overview.trailer")}>
<Duration data-testid="trailerDurationId" text={t("communities.overview.trailer.video")} value={course.trailer.duration} />
<div data-testid="trailerVideoSummary" className="block text-lg mt-3 mb-4.5">{course.trailer.summary}</div>
<Video data-testid="trailerVideoId" className="-mx-0 md:w-full md:mx-auto" url={course.trailer.video} />

{course.trailer.description && (
<div
<div data-testid="trailerTextId"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use optional chaining for safer access to deeply nested properties.

- {course.trailer.description && (
+ {course.trailer?.description && (
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<Section data-testid="trailerId" title={t("communities.overview.trailer")}>
<Duration data-testid="trailerDurationId" text={t("communities.overview.trailer.video")} value={course.trailer.duration} />
<div data-testid="trailerVideoSummary" className="block text-lg mt-3 mb-4.5">{course.trailer.summary}</div>
<Video data-testid="trailerVideoId" className="-mx-0 md:w-full md:mx-auto" url={course.trailer.video} />
{course.trailer.description && (
<div
<div data-testid="trailerTextId"
<Section data-testid="trailerId" title={t("communities.overview.trailer")}>
<Duration data-testid="trailerDurationId" text={t("communities.overview.trailer.video")} value={course.trailer.duration} />
<div data-testid="trailerVideoSummary" className="block text-lg mt-3 mb-4.5">{course.trailer.summary}</div>
<Video data-testid="trailerVideoId" className="-mx-0 md:w-full md:mx-auto" url={course.trailer.video} />
{course.trailer?.description && (
<div data-testid="trailerTextId"

@gabrielaacha gabrielaacha self-assigned this Jun 17, 2024
Copy link
Collaborator

@mhenriette mhenriette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we decided that it would be best if we could make the testing PRs as small as possible, consider dividing this pr into small prs. each component should have its own corresponding pr to reduce the size of this pr.

avoid using the data-testid as a first option of accessing the elements, consider accessing them using ARIA roles or by text where possible.

use the mocks we already have on dev instead of creating new ones

Comment on lines +1 to +11
import { wrapper } from "@/store";
import { ReactNode } from "react";
import { Provider } from "react-redux";

const ReduxProvider = ({ children }: { children: ReactNode }) => {
const { store } = wrapper.useWrappedStore(children);

return <Provider store={store}>{children}</Provider>;
};

export default ReduxProvider;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a reduxProvider, there's no need of creating this one

Comment on lines +17 to +24
const RenderCommunityNavigation = () => {
render(
<ReduxProvider>
<CommunityNavigation />
</ReduxProvider>
);
return screen.getByTestId("communityNavigationShow");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to do this since we have renderWithRedux mock in our test, refer to others tests that were done to checkout how it is used

Comment on lines +16 to +23
const RenderMobileNav = () => {
render(
<ReduxProvider>
<MobileNav />
</ReduxProvider>
);
return screen.getByTestId("mobile-nav-show");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@@ -15,15 +15,15 @@ export default function CommunityNavigation({ paths }: { paths?: string[] }): Re
const path = useMemo(() => (community ? `/communities/${community.slug}` : ""), [community]);

return community ? (
<div>
<div data-testid="communityNavigationShow">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to pass this data-testid as an optional prop instead of passing it like this

<div className="relative flex items-center py-4 pt-4 text-sm border-b-2 md:pt-7 lg:border-0">
<div className="leading-none text-gray-500">
<div className="leading-none text-gray-500" data-testid="communityNavLink">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can test the content of this div element instead of using a data-testid. Let's avoid using data-testid to access elements unless absolutely necessary, as it should be our last resort to prevent changes to the code structure for testing purposes.

<div className="h-auto lg:flex">
<div className="sticky top-0 self-start hidden w-1/4 py-3 lg:block pr-9 lg:py-14">
<div className="sticky top-0 self-start hidden w-1/4 py-3 lg:block pr-9 lg:py-14" data-testid="NavId">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no need of this data-testid

<Navigation />
</div>
<div className="w-full pt-6 lg:hidden">
<div className="w-full pt-6 lg:hidden" data-testid="MobileNavId">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

<MobileNav showTopBorder />
</div>
<div className="w-full lg:w-3/4">
<div className="w-full lg:w-3/4" data-testid="communityNavId">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here as well

@@ -15,8 +15,8 @@ export default function Challenge(): ReactElement {

const { t } = useTranslation();
return course && course.challenge ? (
<Section title={t("communities.overview.challenge")}>
<span className="block mt-3 text-base font-normal md:text-lg">{course.challenge.description}</span>
<Section data-testid="challengeDescriptionId" title={t("communities.overview.challenge")}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass data-testid as a prop

Comment on lines +26 to 35
<Header data-testid="headerId" title={course.name} description={course.description} />
<RewardsSection data-testid=""/>
<ObjectivesSection data-testid=""/>
<PrerequisiteSection data-testid=""/>
<DisclaimerSection data-testid="disclaimerSectionId"/>
<TrailerSection data-testid="trailerSectionId"/>
<LearningModulesSection data-testid="learningModulesSection" />
<ChallengeSection data-testid="challengeSectionId"/>
<PageNavigation data-testid="pageNavigationId"/>
</div>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let us avoid this kind of implementation of passing data-testid on every imported component to test if it is available in the current component

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants