Skip to content

Fix #1867: Added test for ProjectTypeDashboardCard Components #2003

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

Merged
merged 6 commits into from
Aug 9, 2025

Conversation

anurag2787
Copy link
Contributor

@anurag2787 anurag2787 commented Aug 7, 2025

Resolves #1867

Description

Added Unit Tests for ProjectTypeDashboardCard Component
Added comprehensive test suite with 32 test cases covering:

  • Renders successfully with minimal required props
  • Conditional rendering logic
  • Prop-based behavior – different props affect output
  • Event handling – simulate user actions and verify callbacks
  • State changes / internal logic
  • Default values and fallbacks
  • Text and content rendering
  • Handles edge cases and invalid inputs
  • Accessibility roles and labels
  • DOM structure / classNames / styles

Checklist

  • I've read and followed the contributing guidelines.
  • I've run make check-test locally; all checks and tests passed.

Copy link
Contributor

coderabbitai bot commented Aug 7, 2025

Summary by CodeRabbit

  • Tests
    • Added comprehensive unit tests for the ProjectTypeDashboardCard component, covering rendering, behavior, styling, accessibility, and integration scenarios.

Walkthrough

A new unit test suite for the ProjectTypeDashboardCard React component has been added. The tests cover rendering, conditional logic, prop-driven behavior, event handling, accessibility, DOM structure, edge cases, and integration with dependencies, ensuring comprehensive validation of component functionality.

Changes

Cohort / File(s) Change Summary
ProjectTypeDashboardCard Unit Tests
frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
Introduces a detailed unit test suite for ProjectTypeDashboardCard, covering rendering, props, events, styling, accessibility, edge cases, and integration with dependencies.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Renders successfully with minimal required props (#1867)
Conditional rendering logic, prop-based behavior, event handling, state changes, default values, text/content rendering, edge cases, accessibility, DOM structure/styles (#1867)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Possibly related issues

Possibly related PRs

Suggested labels

frontend, frontend-tests

Suggested reviewers

  • arkid15r
  • kasya

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 91618e1 and 775c869.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (6)
📓 Common learnings
Learnt from: Rajgupta36
PR: OWASP/Nest#1717
File: frontend/__tests__/unit/pages/createProgram.test.tsx:70-86
Timestamp: 2025-07-12T17:36:57.255Z
Learning: When testing React page components that use mocked form components, validation logic should be tested at the form component level, not the page level. Page-level tests should focus on authentication, role checking, submission handling, and navigation logic.
Learnt from: ahmedxgouda
PR: OWASP/Nest#1714
File: frontend/src/components/ProjectTypeDashboardCard.tsx:8-12
Timestamp: 2025-07-08T17:07:50.988Z
Learning: In the OWASP/Nest project, union types for component props are not necessary when they would require creating separate type definitions. The project prefers inline prop type definitions even for props with specific string values, maintaining consistency with the single-use component prop pattern.
Learnt from: ahmedxgouda
PR: OWASP/Nest#1703
File: frontend/src/components/BarChart.tsx:33-46
Timestamp: 2025-07-03T03:08:03.290Z
Learning: In the OWASP Nest project's BarChart component (frontend/src/components/BarChart.tsx), the days and requirements arrays are guaranteed to always have the same length in their use cases, so input validation for array length matching is not needed.
Learnt from: kasya
PR: OWASP/Nest#1680
File: frontend/src/components/SponsorCard.tsx:3-3
Timestamp: 2025-06-30T00:55:03.133Z
Learning: In the OWASP/Nest project, interfaces or types are not created for component props that are used only once. Inline prop type definitions are preferred for single-use cases.
📚 Learning: 2025-07-12T17:36:57.255Z
Learnt from: Rajgupta36
PR: OWASP/Nest#1717
File: frontend/__tests__/unit/pages/createProgram.test.tsx:70-86
Timestamp: 2025-07-12T17:36:57.255Z
Learning: When testing React page components that use mocked form components, validation logic should be tested at the form component level, not the page level. Page-level tests should focus on authentication, role checking, submission handling, and navigation logic.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: 2025-06-20T16:12:59.256Z
Learnt from: ahmedxgouda
PR: OWASP/Nest#1633
File: frontend/src/components/HealthMetrics.tsx:30-30
Timestamp: 2025-06-20T16:12:59.256Z
Learning: In the DetailsCard component (frontend/src/components/CardDetailsPage.tsx), there's a length check before rendering HealthMetrics: `healthMetricsData.length > 0`. This ensures that when HealthMetrics is rendered, the data array has at least one element, making accessing data[0] safe within the HealthMetrics component.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: 2025-06-20T16:12:59.256Z
Learnt from: ahmedxgouda
PR: OWASP/Nest#1633
File: frontend/src/components/HealthMetrics.tsx:30-30
Timestamp: 2025-06-20T16:12:59.256Z
Learning: In the DetailsCard component (frontend/src/components/CardDetailsPage.tsx), there's a safety check that ensures HealthMetrics component is only rendered when healthMetricsData exists and has at least one element: `healthMetricsData && healthMetricsData.length > 0`. This makes accessing data[0] safe within the HealthMetrics component.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: 2025-07-03T03:08:03.290Z
Learnt from: ahmedxgouda
PR: OWASP/Nest#1703
File: frontend/src/components/BarChart.tsx:33-46
Timestamp: 2025-07-03T03:08:03.290Z
Learning: In the OWASP Nest project's BarChart component (frontend/src/components/BarChart.tsx), the days and requirements arrays are guaranteed to always have the same length in their use cases, so input validation for array length matching is not needed.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: 2025-06-30T00:55:03.133Z
Learnt from: kasya
PR: OWASP/Nest#1680
File: frontend/src/components/SponsorCard.tsx:3-3
Timestamp: 2025-06-30T00:55:03.133Z
Learning: In the OWASP/Nest project, interfaces or types are not created for component props that are used only once. Inline prop type definitions are preferred for single-use cases.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
🔇 Additional comments (15)
frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx (15)

1-6: LGTM: Clean imports and proper setup.

The imports are well-organized and include all necessary dependencies for comprehensive testing. The FontAwesome icons match the expected component usage patterns.


7-25: LGTM: Proper Next.js Link mocking.

The mock implementation correctly simulates Next.js Link behavior by rendering a standard anchor element with proper prop forwarding, which is essential for testing navigation functionality.


27-54: LGTM: Well-structured SecondaryCard mock.

The mock provides comprehensive test data attributes and maintains the component's interface while enabling thorough testing of prop passing and content rendering.


56-65: LGTM: Proper test setup with cleanup.

The base props configuration and beforeEach cleanup ensure test isolation and provide a consistent starting point for all test cases.


67-94: LGTM: Comprehensive essential rendering tests.

These tests effectively verify the core rendering functionality, text content accuracy, and link structure. The assertions properly validate both content and styling classes.


96-116: LGTM: Thorough conditional rendering coverage.

The tests properly validate that each type prop value renders the correct title, covering all supported component variants.


118-159: LGTM: Comprehensive prop behavior validation.

These tests effectively verify that the component responds correctly to different prop values, including count variations, icon changes, and URL generation for each type.


161-178: LGTM: Appropriate event handling tests.

The tests validate link functionality and click behavior, though they focus on DOM integrity rather than navigation since this is handled by Next.js Link.


180-192: LGTM: Good edge case coverage for count values.

These tests ensure the component handles boundary cases like zero and large numbers gracefully.


194-220: LGTM: Thorough edge case and type safety validation.

The tests cover negative numbers, very large values, and all type variants. The type definition and iteration approach ensures type safety compliance.


222-243: LGTM: Solid accessibility testing.

The tests properly verify semantic HTML structure and meaningful link destinations, ensuring the component meets accessibility standards.


245-322: LGTM: Comprehensive CSS class validation.

These tests thoroughly verify that conditional styling is applied correctly for each component type, including responsive classes and theme variations (light/dark mode support).


324-353: LGTM: Excellent component integration testing.

The tests validate proper prop passing to child components and maintain correct DOM hierarchy, ensuring the component integrates well with its dependencies.


355-383: LGTM: Strong TypeScript compliance validation.

The tests ensure type safety by validating that only acceptable type values work correctly and that different icon types are handled properly.


385-411: LGTM: Valuable performance testing.

These tests verify that the component handles multiple re-renders and rapid prop changes efficiently, which is important for dashboard components that may update frequently.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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
Contributor

@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: 0

🧹 Nitpick comments (1)
frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx (1)

386-406: Redundant title mapping tests.

These tests duplicate the functionality already covered in the "Conditional Rendering Logic" section (lines 96-116). The title mapping is already thoroughly tested there.

Consider removing this section to avoid test duplication:

-  describe('Title Mapping Logic', () => {
-    it('correctly maps healthy type to Healthy title', () => {
-      render(<ProjectTypeDashboardCard type="healthy" count={10} icon={faHeartPulse} />)
-
-      expect(screen.getByTestId('card-title')).toHaveTextContent('Healthy')
-    })
-
-    it('correctly maps needsAttention type to Need Attention title', () => {
-      render(
-        <ProjectTypeDashboardCard type="needsAttention" count={5} icon={faExclamationTriangle} />
-      )
-
-      expect(screen.getByTestId('card-title')).toHaveTextContent('Need Attention')
-    })
-
-    it('correctly maps unhealthy type to Unhealthy title', () => {
-      render(<ProjectTypeDashboardCard type="unhealthy" count={2} icon={faSkull} />)
-
-      expect(screen.getByTestId('card-title')).toHaveTextContent('Unhealthy')
-    })
-  })
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f4424d3 and 91618e1.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (6)
📓 Common learnings
Learnt from: Rajgupta36
PR: OWASP/Nest#1717
File: frontend/__tests__/unit/pages/createProgram.test.tsx:70-86
Timestamp: 2025-07-12T17:36:57.255Z
Learning: When testing React page components that use mocked form components, validation logic should be tested at the form component level, not the page level. Page-level tests should focus on authentication, role checking, submission handling, and navigation logic.
Learnt from: ahmedxgouda
PR: OWASP/Nest#1714
File: frontend/src/components/ProjectTypeDashboardCard.tsx:8-12
Timestamp: 2025-07-08T17:07:50.988Z
Learning: In the OWASP/Nest project, union types for component props are not necessary when they would require creating separate type definitions. The project prefers inline prop type definitions even for props with specific string values, maintaining consistency with the single-use component prop pattern.
Learnt from: ahmedxgouda
PR: OWASP/Nest#1703
File: frontend/src/components/BarChart.tsx:33-46
Timestamp: 2025-07-03T03:08:03.290Z
Learning: In the OWASP Nest project's BarChart component (frontend/src/components/BarChart.tsx), the days and requirements arrays are guaranteed to always have the same length in their use cases, so input validation for array length matching is not needed.
Learnt from: kasya
PR: OWASP/Nest#1680
File: frontend/src/components/SponsorCard.tsx:3-3
Timestamp: 2025-06-30T00:55:03.133Z
Learning: In the OWASP/Nest project, interfaces or types are not created for component props that are used only once. Inline prop type definitions are preferred for single-use cases.
📚 Learning: when testing react page components that use mocked form components, validation logic should be teste...
Learnt from: Rajgupta36
PR: OWASP/Nest#1717
File: frontend/__tests__/unit/pages/createProgram.test.tsx:70-86
Timestamp: 2025-07-12T17:36:57.255Z
Learning: When testing React page components that use mocked form components, validation logic should be tested at the form component level, not the page level. Page-level tests should focus on authentication, role checking, submission handling, and navigation logic.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: in the detailscard component (frontend/src/components/carddetailspage.tsx), there's a length check b...
Learnt from: ahmedxgouda
PR: OWASP/Nest#1633
File: frontend/src/components/HealthMetrics.tsx:30-30
Timestamp: 2025-06-20T16:12:59.256Z
Learning: In the DetailsCard component (frontend/src/components/CardDetailsPage.tsx), there's a length check before rendering HealthMetrics: `healthMetricsData.length > 0`. This ensures that when HealthMetrics is rendered, the data array has at least one element, making accessing data[0] safe within the HealthMetrics component.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: in the detailscard component (frontend/src/components/carddetailspage.tsx), there's a safety check t...
Learnt from: ahmedxgouda
PR: OWASP/Nest#1633
File: frontend/src/components/HealthMetrics.tsx:30-30
Timestamp: 2025-06-20T16:12:59.256Z
Learning: In the DetailsCard component (frontend/src/components/CardDetailsPage.tsx), there's a safety check that ensures HealthMetrics component is only rendered when healthMetricsData exists and has at least one element: `healthMetricsData && healthMetricsData.length > 0`. This makes accessing data[0] safe within the HealthMetrics component.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: in the owasp nest project's barchart component (frontend/src/components/barchart.tsx), the days and ...
Learnt from: ahmedxgouda
PR: OWASP/Nest#1703
File: frontend/src/components/BarChart.tsx:33-46
Timestamp: 2025-07-03T03:08:03.290Z
Learning: In the OWASP Nest project's BarChart component (frontend/src/components/BarChart.tsx), the days and requirements arrays are guaranteed to always have the same length in their use cases, so input validation for array length matching is not needed.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
📚 Learning: in the owasp/nest project, interfaces or types are not created for component props that are used onl...
Learnt from: kasya
PR: OWASP/Nest#1680
File: frontend/src/components/SponsorCard.tsx:3-3
Timestamp: 2025-06-30T00:55:03.133Z
Learning: In the OWASP/Nest project, interfaces or types are not created for component props that are used only once. Inline prop type definitions are preferred for single-use cases.

Applied to files:

  • frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx
🔇 Additional comments (15)
frontend/__tests__/unit/components/ProjectTypeDashboardCard.test.tsx (15)

1-6: LGTM! Proper imports and dependencies.

The imports are well-structured, including all necessary testing utilities, React, FontAwesome icons, and the component under test. The setup follows standard React Testing Library patterns.


7-25: Well-implemented Next.js Link mock.

The mock correctly simulates Next.js Link behavior by rendering a standard anchor element with all necessary props passed through. This allows testing of href attributes and link functionality without Next.js router dependencies.


27-54: Comprehensive SecondaryCard mock with good testability.

The mock implementation provides excellent testability by:

  • Exposing props as data attributes for easy assertion
  • Maintaining the expected DOM structure with title and content sections
  • Serializing complex props like icons for verification

56-65: Clean test setup with appropriate base props.

The base props provide a good foundation for testing, and the beforeEach cleanup ensures test isolation. The use of as const for the type prop maintains type safety.


67-94: Essential rendering tests cover core functionality well.

These tests effectively verify the component's basic rendering capabilities, including proper integration with SecondaryCard, correct text content, and link structure. The assertions are specific and meaningful.


96-116: Comprehensive conditional rendering coverage.

The tests properly verify that each project type renders the correct title, covering all three variants (healthy, needsAttention, unhealthy) as specified in the PR objectives.


118-159: Thorough prop-based behavior testing.

These tests excellently cover how different props affect component output, including count variations, icon changes, and URL generation for each type. The use of rerender for testing prop changes is efficient and appropriate.


161-178: Event handling tests verify link functionality.

The tests confirm that the component renders as a clickable link with correct href attributes. The click simulation ensures the link maintains functionality after user interaction.


180-192: Good coverage of edge cases for count values.

These tests handle zero and large count values, ensuring the component gracefully displays various numeric inputs as mentioned in the PR objectives.


194-223: Comprehensive edge case testing.

The tests cover negative numbers, very large values, and all type variants systematically. The loop-based approach for testing all types is efficient and thorough.


225-246: Accessibility tests ensure semantic structure.

These tests verify that the component maintains proper accessibility by rendering semantic link elements and providing meaningful destinations, addressing the PR objective for accessibility compliance.


248-325: Excellent CSS class verification covering all styling scenarios.

The tests comprehensively verify:

  • Base classes on link elements
  • Responsive design classes
  • Conditional styling for each project type (healthy, unhealthy, needsAttention)
  • Proper Tailwind CSS class application

This thorough coverage ensures UI integrity as specified in the PR objectives.


327-356: Strong component integration testing.

These tests verify proper integration with SecondaryCard, including correct prop passing, content structure, and component hierarchy. This ensures the component works correctly within the larger system.


358-384: Type safety tests ensure robust TypeScript compliance.

The tests verify that only valid type values are accepted and different icon types work correctly, ensuring type safety and preventing runtime errors.


408-433: Performance tests add valuable stability coverage.

These tests ensure the component handles multiple re-renders and rapid prop changes gracefully, which is important for a dashboard component that may receive frequent updates.

Copy link
Collaborator

@kasya kasya left a comment

Choose a reason for hiding this comment

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

Thanks for adding these 👍🏼

Copy link

sonarqubecloud bot commented Aug 9, 2025

@kasya kasya enabled auto-merge August 9, 2025 00:46
@kasya kasya added this pull request to the merge queue Aug 9, 2025
Merged via the queue into OWASP:main with commit 0079230 Aug 9, 2025
24 checks passed
trucodd pushed a commit to trucodd/Nest that referenced this pull request Aug 9, 2025
…WASP#2003)

* Added test for ProjectTypeDashboardCard Component

* Refactor test and removed redundant from test

---------

Co-authored-by: Kate Golovanova <[email protected]>
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.

Add tests for <ProjectTypeDashboardCard> component
2 participants