Skip to content

Fix #1859: Added test to Modal Component#2016

Merged
kasya merged 3 commits intoOWASP:mainfrom
anurag2787:test/modal
Aug 9, 2025
Merged

Fix #1859: Added test to Modal Component#2016
kasya merged 3 commits intoOWASP:mainfrom
anurag2787:test/modal

Conversation

@anurag2787
Copy link
Contributor

Resolves #1859

Description

Added Unit Tests for Modal Component
Added comprehensive test suite 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.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 8, 2025

Summary by CodeRabbit

  • Tests
    • Improved and expanded unit tests for the modal component, including additional cases for rendering, accessibility, content structure, and interaction.
    • Enhanced assertions for modal content, accessibility attributes, and callback behavior.
    • Replaced asynchronous waits with direct assertions for faster and more reliable tests.

Walkthrough

The test suite for the DialogComp component was refactored and expanded. Imports were streamlined, custom mocks for dependencies were introduced, and numerous additional test cases were added to cover rendering, accessibility, content, and event handling. All asynchronous waits were removed, and assertions were made synchronous.

Changes

Cohort / File(s) Change Summary
DialogComp Modal Component Test Refactor & Expand
frontend/__tests__/unit/components/Modal.test.tsx
Refactored imports and mocks, converted tests to synchronous style, and added comprehensive new test cases for rendering, props, accessibility, content, and event handling.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Renders successfully with minimal required props (#1859)
Conditional rendering logic, prop-based behavior, default values, and fallbacks (#1859)
Event handling – simulate user actions and verify callbacks (#1859)
Handles edge cases, invalid inputs, accessibility, DOM/classNames/styles, text/content rendering (#1859)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Suggested reviewers

  • arkid15r
  • kasya

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.

✨ 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: 2

🧹 Nitpick comments (3)
frontend/__tests__/unit/components/Modal.test.tsx (3)

3-4: Prefer user-event over fireEvent for realistic interaction simulation

@testing-library/user-event provides higher-level, more accurate user-interaction APIs. Replacing fireEvent with userEvent (e.g. user.click()) will better reflect real-world behaviour and remove the need for manual event propagation.


92-100: Redundant mock resets

jest.clearAllMocks() is called in both beforeEach and afterEach, doubling work with no benefit. Keeping it in afterEach is sufficient and avoids unnecessary noise.


192-197: Class-name queries couple tests to implementation details

Using querySelector('.animate-scaleIn') & friends makes tests brittle against harmless style refactors. Prefer accessibility-oriented or semantic queries unless the class itself is the contract under test.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1c1f1c2 and d112ac2.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/Modal.test.tsx (3 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 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.
📚 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/Modal.test.tsx
🔇 Additional comments (2)
frontend/__tests__/unit/components/Modal.test.tsx (2)

78-90: Inconsistent camel-casing (onclick) may mask real API contracts

Even if the production DialogComp expects a lowercase onclick, the conventional React naming is onClick/onPress. Verify the source component and update for consistency to reduce cognitive load and typo risk.


150-154: Missing assertion that action button triggers callback

Presence is verified, but the click behaviour (critical for UX) isn’t. Add:

fireEvent.click(screen.getByTestId('action-button'))
expect(mockOnClick).toHaveBeenCalledTimes(1)

(After fixing the prop name to onPress / onClick).

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

♻️ Duplicate comments (1)
frontend/__tests__/unit/components/Modal.test.tsx (1)

188-189: Selector still tied to visible text “Close”

A previous review flagged this fragility; the new accessibility test re-introduces it. Prefer getByRole('button', { name: /close/i }) or getByLabelText('close-modal') to stay resilient if the button becomes icon-only.

🧹 Nitpick comments (3)
frontend/__tests__/unit/components/Modal.test.tsx (3)

23-33: Consider replacing the native <dialog> element in the mock

jsdom does not implement the HTML Dialog element API. While you only use it as a container, some future assertions (e.g. getByRole('dialog')) or code paths that expect the element’s methods (showModal, close) could break. A safer stub is a plain <div role="dialog">…</div> which keeps accessibility semantics without relying on unsupported DOM APIs.


92-99: Duplicate mock resets – clearAllMocks is called twice

beforeEach and afterEach both invoke jest.clearAllMocks(). The latter is redundant because jest.restoreAllMocks() already resets spies and mocks. Removing the extra call avoids needless work and keeps the setup tidy.


150-154: Missing assertion that the action button fires its callback

The test confirms that the action button renders but never verifies that onPress is invoked when the user clicks it. Adding a simple fireEvent.click(screen.getByTestId('action-button')) followed by expect(mockOnClick).toHaveBeenCalled() would cover the most important behavioural path.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d112ac2 and d1ed5ab.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/Modal.test.tsx (3 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 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.
📚 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/Modal.test.tsx
📚 Learning: 2025-07-12T17:12:25.807Z
Learnt from: Rajgupta36
PR: OWASP/Nest#1717
File: frontend/__tests__/unit/pages/ProgramDetails.test.tsx:35-0
Timestamp: 2025-07-12T17:12:25.807Z
Learning: In React applications, button elements should always have an explicit type attribute (type="button", type="submit", or type="reset") to prevent unintended form submission behavior, even when the button appears to be outside of a form context. The default type is "submit" which can cause unexpected behavior.

Applied to files:

  • frontend/__tests__/unit/components/Modal.test.tsx

@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 9, 2025

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.

Well done! 👍🏼

@kasya kasya added this pull request to the merge queue Aug 9, 2025
Merged via the queue into OWASP:main with commit f922963 Aug 9, 2025
24 checks passed
trucodd pushed a commit to trucodd/Nest that referenced this pull request Aug 9, 2025
* Added more test to Modol Component

* fixed  coderabbitai review

---------

Co-authored-by: Kate Golovanova <kate@kgthreads.com>
@coderabbitai coderabbitai bot mentioned this pull request Dec 31, 2025
3 tasks
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 <Modal> component

2 participants

Comments