Skip to content

Add unit tests for <AnimatedCounter /> component#1829

Closed
gaurigupta0604 wants to merge 9 commits intoOWASP:mainfrom
gaurigupta0604:animated-counter-tests
Closed

Add unit tests for <AnimatedCounter /> component#1829
gaurigupta0604 wants to merge 9 commits intoOWASP:mainfrom
gaurigupta0604:animated-counter-tests

Conversation

@gaurigupta0604
Copy link

Proposed change

Added unit tests for the <AnimatedCounter /> component to improve test coverage and ensure correct behavior across different animation types, roles, and accessibility attributes.

Resolves #1798

Checklist

  • I've read and followed the contributing guidelines
  • I've run yarn test locally; all checks and tests passed
  • The PR addresses the assigned issue and follows the repo's code style

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 24, 2025

Summary by CodeRabbit

  • New Features

    • AnimatedCounter component now supports an optional aria-label for improved accessibility.
    • AnimatedCounter can trigger a callback function when the animation ends.
  • Bug Fixes

    • Improved accessibility by adding appropriate roles and aria-labels to the AnimatedCounter.
  • Chores

    • Updated and restructured test scripts for improved reliability.
    • Added and removed development dependencies for better environment compatibility.
    • Introduced comprehensive automated tests for the AnimatedCounter component.

Walkthrough

A new test suite for the AnimatedCounter React component has been introduced. The tests verify rendering, animation progression, prop-based behavior, event handling, edge case handling, accessibility attributes, and DOM structure using Jest and React Testing Library, with fake timers for animation control. Additionally, the component was enhanced with new props for accessibility and animation end callback. The project’s test scripts and dependencies were reorganized, and Sentry configuration was adjusted to fix the environment setting.

Changes

Cohort / File(s) Change Summary
AnimatedCounter component and tests
frontend/src/components/AnimatedCounter.tsx, frontend/src/components/AnimatedCounter.test.tsx
Extended AnimatedCounter component with optional aria-label and onEnd callback props; added accessibility attributes and callback invocation on animation end. Added detailed tests covering rendering, animation progression, props behavior, callbacks, edge cases, and accessibility.
Project configuration and dependencies
frontend/package.json
Reorganized test scripts to use cross-env for environment variables; added cross-env and which to devDependencies; removed ts-jest.
Sentry configuration
frontend/src/sentry.server.config.ts
Added a blank line before Sentry.init() call; no functional change to Sentry initialization logic.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

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

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Sentry environment setting changed to static 'development' (frontend/src/sentry.server.config.ts) This change is unrelated to adding tests or enhancing the AnimatedCounter component.
Test scripts and dependency modifications (frontend/package.json) These changes relate to project setup and test environment but are outside the scope of #1798.

Suggested labels

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 028e125 and c11da16.

⛔ Files ignored due to path filters (2)
  • frontend/pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (4)
  • frontend/package.json (3 hunks)
  • frontend/src/components/AnimatedCounter.tsx (2 hunks)
  • frontend/src/sentry.server.config.ts (1 hunks)
  • package.json (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • frontend/src/sentry.server.config.ts
  • package.json
🚧 Files skipped from review as they are similar to previous changes (2)
  • frontend/src/components/AnimatedCounter.tsx
  • frontend/package.json
✨ 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.
    • @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 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.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

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 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.

@kasya
Copy link
Collaborator

kasya commented Jul 25, 2025

@gaurigupta0604 Could you run make check and push updated files to this PR? 🙏🏼 CI\CD checks are failing right now.

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.

@gaurigupta0604 could you fix this test?

it('calls onEnd when animation finishes', () => {
    const onEndMock = jest.fn();
    render(<AnimatedCounter end={500} duration={500} onEnd={onEndMock} />);
    act(() => {
      jest.advanceTimersByTime(500);
    });
    expect(onEndMock).toHaveBeenCalledTimes(1);
  });

There's no onEnd prop in the AnimatedCounter component.
Screenshot 2025-07-27 at 11 54 09 AM

@gaurigupta0604
Copy link
Author

gaurigupta0604 commented Jul 28, 2025

@gaurigupta0604 could you fix this test?

it('calls onEnd when animation finishes', () => {
    const onEndMock = jest.fn();
    render(<AnimatedCounter end={500} duration={500} onEnd={onEndMock} />);
    act(() => {
      jest.advanceTimersByTime(500);
    });
    expect(onEndMock).toHaveBeenCalledTimes(1);
  });

There's no onEnd prop in the AnimatedCounter component. Screenshot 2025-07-27 at 11 54 09 AM

Hi @kasya,
Apologies for the delay — I just saw your comment now. Thank you for pointing it out!

You're right, the onEnd prop isn't part of the AnimatedCounter component. I'll remove that test and push the necessary updates shortly. Appreciate your review!

@arkid15r
Copy link
Collaborator

@gaurigupta0604 any updates on this?
Tomorrow is the deadline date for this task.

@gaurigupta0604
Copy link
Author

gaurigupta0604 commented Jul 30, 2025

@arkid15r, yes — I’m working on it right now. I’ll remove the invalid test and push the updated changes shortly. Thanks for the reminder!
Working on the final cleanup of test file, will submit by tomorrow morning.

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 (1)
frontend/src/sentry.server.config.ts (1)

2-2: Consider removing unused ENVIRONMENT import.

The ENVIRONMENT variable is imported but no longer used after hardcoding the environment value.

-import { SENTRY_DSN, ENVIRONMENT, RELEASE_VERSION } from 'utils/credentials'
+import { SENTRY_DSN, RELEASE_VERSION } from 'utils/credentials'
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c49887d and 028e125.

⛔ Files ignored due to path filters (1)
  • frontend/pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (4)
  • frontend/package.json (3 hunks)
  • frontend/src/components/AnimatedCounter.test.tsx (1 hunks)
  • frontend/src/components/AnimatedCounter.tsx (2 hunks)
  • frontend/src/sentry.server.config.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/src/components/AnimatedCounter.test.tsx
🧰 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: the react-apexcharts chart component does not support aria attributes like aria-label and role as di...
Learnt from: ahmedxgouda
PR: OWASP/Nest#1633
File: frontend/src/components/GradientRadialChart.tsx:67-116
Timestamp: 2025-06-21T12:21:32.372Z
Learning: The react-apexcharts Chart component does not support ARIA attributes like aria-label and role as direct props. To add accessibility attributes to ApexCharts in React, wrap the Chart component in a container div with the appropriate ARIA attributes.

Applied to files:

  • frontend/src/components/AnimatedCounter.tsx
🔇 Additional comments (7)
frontend/src/components/AnimatedCounter.tsx (3)

8-9: LGTM! Good addition of accessibility and callback props.

The addition of optional aria-label and onEnd props enhances both accessibility and functionality of the component.


35-37: LGTM! Proper onEnd callback implementation.

The onEnd callback is correctly invoked only when the animation completes (progress reaches 1) and is properly included in the dependency array to avoid stale closures.

Also applies to: 41-41


46-46: LGTM! Good accessibility enhancement.

Adding role="status" is appropriate for a counter that updates dynamically, as it indicates to screen readers that this content may change.

frontend/package.json (4)

14-16: LGTM! Good test script standardization.

The unified use of cross-env NODE_OPTIONS=--experimental-vm-modules across test scripts improves consistency and cross-platform compatibility.


87-87: LGTM! Appropriate dependency updates for test infrastructure.

The addition of cross-env and which packages supports the updated test scripts and tooling needs.

Also applies to: 113-114


88-89: ESLint patch downgrade validated

We reviewed frontend/eslint.config.mjs and confirmed there are no hardcoded ESLint version checks or usage of features introduced after 9.31.0. A patch-level downgrade within the 9.x line will not break existing rules or plugin integrations.

No further action required.


100-100: ts-jest fully removed; safe to proceed

No references to ts-jest remain in frontend/jest.config.ts or elsewhere in the repo. All existing TypeScript tests will run under the new runner without issue.

@sonarqubecloud
Copy link

@gaurigupta0604
Copy link
Author

gaurigupta0604 commented Jul 31, 2025

@gaurigupta0604 Could you run make check and push updated files to this PR? 🙏🏼 CI\CD checks are failing right now.

Hi @kasya, I’ve removed the invalid onEnd test and pushed the updated files. Let me know if there’s anything else to address. Kindly re-review when you get a chance

@kasya
Copy link
Collaborator

kasya commented Aug 2, 2025

@gaurigupta0604 hi! Please clean up this PR - your proposed changes should only include the test file.

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.

Requesting changes

@gaurigupta0604 gaurigupta0604 mentioned this pull request Aug 3, 2025
2 tasks
@kasya
Copy link
Collaborator

kasya commented Aug 5, 2025

@gaurigupta0604 Hi! The deadline for this issue was Jul 30th. I appreciate you working on this, but since this PR is still not ready for review and it's been 4 days past deadline - I'm going to close it and reassign the issue to someone else.

@kasya kasya closed this Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add tests for <AnimatedCounter> component

3 participants