-
-
Notifications
You must be signed in to change notification settings - Fork 802
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: ReCAPTCHA refresh on submitting Login/SignUp form #2943
Fix: ReCAPTCHA refresh on submitting Login/SignUp form #2943
Conversation
…oginPage.test.tsx to Vitest
WalkthroughThe pull request introduces modifications to the Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
Our Pull Request Approval ProcessThanks for contributing! Testing Your CodeRemember, your PRs won't be reviewed until these criteria are met:
Our policies make our code better. ReviewersDo not assign reviewers. Our Queue Monitors will review your PR and assign them.
Reviewing Your CodeYour reviewer(s) will have the following roles:
CONTRIBUTING.mdRead our CONTRIBUTING.md file. Most importantly:
Other
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/screens/LoginPage/LoginPage.spec.tsx (1)
1024-1028
: Consider verifying fetch error response details as well.Clearing mocks is good. For completeness, you might also want to assert that an appropriate user-facing error message – or a relevant toast – is displayed when the fetch fails.
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/screens/LoginPage/LoginPage.spec.tsx
(29 hunks)src/screens/LoginPage/LoginPage.tsx
(6 hunks)
🔇 Additional comments (7)
src/screens/LoginPage/LoginPage.spec.tsx (4)
27-27
: Good use of Vitest imports.
You've successfully replaced Jest dependencies with the Vitest counterparts (vi, beforeEach, expect, it, describe
). Ensure you remove or adapt any leftover Jest-specific config or scripts to avoid confusion.
218-220
: Mocking strategy looks correct.
Your mocks for react-toastify
follow best practices for partial mocking. This ensures test isolation and consistency.
Line range hint 236-253
: Comprehensive ReCAPTCHA mock.
This approach correctly simulates reset
functionality in the mock and provides full control needed for testing. Great job.
634-665
: ReCAPTCHA reset test is well-structured.
Verifying resetReCAPTCHA
is called after an unsuccessful login aligns perfectly with the PR's objective. The test ensures the fix won't regress in the future.
src/screens/LoginPage/LoginPage.tsx (3)
64-65
: ReCAPTCHA refs add clarity for separate login and signup flows.
Using two distinct refs, loginRecaptchaRef
and SignupRecaptchaRef
, is a clean way to isolate recaptcha resets for each form.
259-263
: Resetting ReCAPTCHA on signup exceptions.
Great job resetting ReCAPTCHA upon both successful and failed signup attempts. This ensures a consistent user experience.
335-339
: Resetting ReCAPTCHA on login exceptions.
Similarly, resetting the ReCAPTCHA upon login errors helps clear the stale token for subsequent login tries.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop-postgres #2943 +/- ##
=====================================================
+ Coverage 58.52% 88.60% +30.08%
=====================================================
Files 299 316 +17
Lines 7414 8278 +864
Branches 1621 1813 +192
=====================================================
+ Hits 4339 7335 +2996
+ Misses 2828 721 -2107
+ Partials 247 222 -25 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/screens/LoginPage/LoginPage.spec.tsx (2)
Line range hint
235-277
: Consider simplifying the ReCAPTCHA mock implementationThe current mock implementation is quite complex. Consider simplifying it by directly mocking the reset function without the full component implementation.
-const recaptcha = react.forwardRef( - ( - props: { - onChange: (value: string) => void; - } & React.InputHTMLAttributes<HTMLInputElement>, - ref: React.LegacyRef<HTMLInputElement> | undefined, - ): JSX.Element => { - const { onChange, ...otherProps } = props; - - Object.defineProperty(ref, 'current', { - value: { - reset: resetReCAPTCHA, - }, - }); - - const handleChange = ( - event: React.ChangeEvent<HTMLInputElement>, - ): void => { - if (onChange) { - onChange(event.target.value); - } - }; - - return ( - <> - <input - type="text" - data-testid="mock-recaptcha" - {...otherProps} - onChange={handleChange} - ref={ref} - /> - </> - ); - }, -); +const recaptcha = react.forwardRef((props, ref) => { + Object.defineProperty(ref, 'current', { + value: { reset: resetReCAPTCHA }, + }); + return <input data-testid="mock-recaptcha" {...props} />; +});
Line range hint
1024-1061
: Enhance error handling test coverageThe API error handling tests could be more comprehensive:
- Different types of network errors
- Various HTTP status codes
- Invalid response formats
it('displays warning message when resource loading fails', async () => { - const mockError = new Error('Network error'); - global.fetch = vi.fn(() => Promise.reject(mockError)); + // Test different error scenarios + const errorScenarios = [ + { error: new Error('Network error'), desc: 'network error' }, + { error: new Error('Timeout'), desc: 'timeout error' }, + { error: new Response(null, { status: 404 }), desc: '404 status' }, + { error: new Response(null, { status: 500 }), desc: '500 status' }, + ]; + + for (const scenario of errorScenarios) { + global.fetch = vi.fn(() => + scenario.error instanceof Error + ? Promise.reject(scenario.error) + : Promise.resolve(scenario.error) + ); await act(async () => { render( <MockedProvider addTypename={false} link={link}> <BrowserRouter> <Provider store={store}> <I18nextProvider i18n={i18nForTest}> <LoginPage /> </I18nextProvider> </Provider> </BrowserRouter> </MockedProvider>, ); }); expect(fetch).toHaveBeenCalledWith(BACKEND_URL); + // Add specific assertions for each error scenario + // Clean up after each scenario + cleanup(); + } });
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/screens/LoginPage/LoginPage.spec.tsx
(29 hunks)
🔇 Additional comments (4)
src/screens/LoginPage/LoginPage.spec.tsx (4)
3-9
: LGTM: Clean import organization
The imports are well-organized, and the addition of waitFor
from @testing-library/react
is appropriate for handling asynchronous operations in tests.
215-219
: LGTM: Clean migration to Vitest mocking
The toast mock has been correctly migrated from Jest to Vitest, maintaining the same functionality while using the new framework's syntax.
633-664
: LGTM: Comprehensive test coverage for ReCAPTCHA reset
The test case effectively verifies that the ReCAPTCHA component is reset after an unsuccessful login attempt. The use of waitFor
ensures proper handling of asynchronous behavior.
Line range hint 1-1061
: Verify test coverage for all LoginPage component features
The test suite appears comprehensive but should be verified against the component's features.
✅ Verification successful
Let me gather more information about the LoginPage component to verify test coverage.
Let me gather more information about the component's state and mutations to ensure complete coverage.
Based on the gathered information, I can now provide the final response:
Test coverage appears comprehensive for LoginPage component
The test suite provides thorough coverage of the LoginPage component's features:
-
Core functionality tests:
- Login and registration form submissions
- Password validation and error warnings
- ReCAPTCHA integration
- Role toggle (admin/user)
- Form state management
- Pre-login data handling
- Password visibility toggles
- Organization list integration
-
Edge cases and error scenarios:
- Invalid input validation
- Password mismatch scenarios
- Failed API connections
- Authentication redirects
- ReCAPTCHA token handling
-
UI interaction tests:
- Tab switching
- Form field interactions
- Password visibility toggles
- Organization dropdown selection
The test suite covers all identified component features including state management, mutations, queries, form submissions, and user interactions.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check test coverage for LoginPage component features
# Find all props, handlers, and state in LoginPage component
echo "Searching for LoginPage component features..."
ast-grep --pattern 'const LoginPage = () => {
$$$
}'
# Find all event handlers
echo "Searching for event handlers..."
ast-grep --pattern 'const handle$_($$$) {
$$$
}'
# Compare with test coverage
echo "Searching for corresponding test cases..."
rg -l 'it\(' --type ts
Length of output: 9933
Script:
#!/bin/bash
# Find the LoginPage component implementation
echo "Searching for LoginPage component implementation..."
rg -A 10 "const LoginPage = \(\)" src/screens/LoginPage/LoginPage.tsx
# Find all event handlers in LoginPage
echo "Searching for event handlers in LoginPage..."
rg -A 5 "const handle[A-Z][a-zA-Z]+" src/screens/LoginPage/LoginPage.tsx
# Find all test cases in LoginPage.spec.tsx
echo "Searching for test cases in LoginPage.spec.tsx..."
rg "^ it\(" src/screens/LoginPage/LoginPage.spec.tsx
Length of output: 3463
Script:
#!/bin/bash
# Find state management in LoginPage
echo "Searching for state hooks in LoginPage..."
rg -A 2 "const \[.*?, set.*?\] = useState" src/screens/LoginPage/LoginPage.tsx
# Find mutations and queries
echo "Searching for GraphQL mutations and queries..."
rg -A 2 "const \[.*?Mutation\]" src/screens/LoginPage/LoginPage.tsx
rg -A 2 "const \{.*?\} = useQuery" src/screens/LoginPage/LoginPage.tsx
# Find form submission handlers
echo "Searching for form submissions..."
rg -A 5 "const.*?submit|onSubmit" src/screens/LoginPage/LoginPage.tsx
Length of output: 2227
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@palisadoes I've added test case for Register functionality too. Ready for you review, guide me if I'm missing something. |
ab3b14f
into
PalisadoesFoundation:develop-postgres
What kind of change does this PR introduce?
Issue Number:
Fixes #2928 & #2554
Did you add tests for your changes?
Yes
Snapshots/Videos:
Screen.Recording.2024-12-26.at.22.52.55.mov
If relevant, did you update the documentation?
No
Summary
Does this PR introduce a breaking change?
No
Other information
N/A
Have you read the contributing guide?
Yes
Summary by CodeRabbit
Summary by CodeRabbit
New Features
Bug Fixes
Tests