-
-
Notifications
You must be signed in to change notification settings - Fork 35
test(frontend): add OTP and email reminder page coverage (issue #127) #226
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
Open
savanigit
wants to merge
1
commit into
shrixtacy:master
Choose a base branch
from
savanigit:test/issue-127-frontend-otp-coverage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
web_app/frontend-react/src/pages/VerifyEmailReminder.test.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import React from 'react'; | ||
| import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
| import VerifyEmailReminder from './VerifyEmailReminder'; | ||
| import { authAPI } from '../utils/api'; | ||
| import toast from 'react-hot-toast'; | ||
|
|
||
| jest.mock('react-hot-toast'); | ||
| jest.mock('../utils/api', () => ({ | ||
| authAPI: { | ||
| post: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe('VerifyEmailReminder Component', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('renders reminder content and resend button', () => { | ||
| render(<VerifyEmailReminder />); | ||
|
|
||
| expect(screen.getByRole('heading', { name: /verify your email/i })).toBeInTheDocument(); | ||
| expect(screen.getByRole('button', { name: /resend verification email/i })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('resends verification email successfully', async () => { | ||
| authAPI.post.mockResolvedValueOnce({ data: { success: true } }); | ||
|
|
||
| render(<VerifyEmailReminder />); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: /resend verification email/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(authAPI.post).toHaveBeenCalledWith('/auth/resend-verification'); | ||
| expect(toast.success).toHaveBeenCalledWith('Verification email sent! Check your inbox.'); | ||
| }); | ||
| }); | ||
|
|
||
| it('shows API error message on resend failure', async () => { | ||
| authAPI.post.mockRejectedValueOnce({ | ||
| response: { data: { message: 'Too many requests' } }, | ||
| }); | ||
|
|
||
| render(<VerifyEmailReminder />); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: /resend verification email/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(toast.error).toHaveBeenCalledWith('Too many requests'); | ||
| }); | ||
| }); | ||
|
|
||
| it('shows fallback error message on resend failure without API message', async () => { | ||
| authAPI.post.mockRejectedValueOnce(new Error('Network down')); | ||
|
|
||
| render(<VerifyEmailReminder />); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: /resend verification email/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(toast.error).toHaveBeenCalledWith('Failed to resend email'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import React from 'react'; | ||
| import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
| import VerifyOTP from './VerifyOTP'; | ||
| import toast from 'react-hot-toast'; | ||
| import useAuthStore from '../store/authStore'; | ||
| import { authAPI } from '../utils/api'; | ||
|
|
||
| const mockNavigate = jest.fn(); | ||
| const mockSetAuth = jest.fn(); | ||
|
|
||
| jest.mock('react-hot-toast'); | ||
| jest.mock('../store/authStore'); | ||
| jest.mock('../utils/api', () => ({ | ||
| authAPI: { | ||
| post: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('react-router-dom', () => { | ||
| const actual = jest.requireActual('react-router-dom'); | ||
| return { | ||
| ...actual, | ||
| useNavigate: () => mockNavigate, | ||
| useLocation: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| const { useLocation } = require('react-router-dom'); | ||
|
|
||
| describe('VerifyOTP Component', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| useAuthStore.mockImplementation((selector) => { | ||
| const state = { setAuth: mockSetAuth }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| useLocation.mockReturnValue({ | ||
| state: { | ||
| userId: 'user-123', | ||
| email: 'test@example.com', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('renders OTP screen with six input boxes', () => { | ||
| render(<VerifyOTP />); | ||
|
|
||
| expect(screen.getByText(/verify your email/i)).toBeInTheDocument(); | ||
| expect(screen.getByText(/test@example.com/i)).toBeInTheDocument(); | ||
| expect(screen.getAllByRole('textbox')).toHaveLength(6); | ||
| }); | ||
|
|
||
| it('shows error for incomplete OTP and does not call API', async () => { | ||
| render(<VerifyOTP />); | ||
|
|
||
| const inputs = screen.getAllByRole('textbox'); | ||
| fireEvent.change(inputs[0], { target: { value: '1' } }); | ||
| fireEvent.change(inputs[1], { target: { value: '2' } }); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: /verify email/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(toast.error).toHaveBeenCalledWith('Please enter complete OTP'); | ||
| expect(authAPI.post).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| it('verifies OTP successfully and navigates to dashboard', async () => { | ||
| authAPI.post.mockResolvedValueOnce({ | ||
| data: { | ||
| success: true, | ||
| user: { id: 'user-123', email: 'test@example.com' }, | ||
| token: 'token-123', | ||
| }, | ||
| }); | ||
|
|
||
| render(<VerifyOTP />); | ||
|
|
||
| const inputs = screen.getAllByRole('textbox'); | ||
| ['1', '2', '3', '4', '5', '6'].forEach((digit, idx) => { | ||
| fireEvent.change(inputs[idx], { target: { value: digit } }); | ||
| }); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: /verify email/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(authAPI.post).toHaveBeenCalledWith('/auth/verify-otp', { | ||
| userId: 'user-123', | ||
| otp: '123456', | ||
| }); | ||
| expect(mockSetAuth).toHaveBeenCalledWith( | ||
| { id: 'user-123', email: 'test@example.com' }, | ||
| 'token-123', | ||
| ); | ||
| expect(toast.success).toHaveBeenCalledWith('Email verified successfully!'); | ||
| expect(mockNavigate).toHaveBeenCalledWith('/dashboard'); | ||
| }); | ||
| }); | ||
|
|
||
| it('handles verification failure and resets OTP', async () => { | ||
| authAPI.post.mockRejectedValueOnce({ | ||
| response: { data: { message: 'Invalid OTP' } }, | ||
| }); | ||
|
|
||
| render(<VerifyOTP />); | ||
|
|
||
| const inputs = screen.getAllByRole('textbox'); | ||
| ['1', '2', '3', '4', '5', '6'].forEach((digit, idx) => { | ||
| fireEvent.change(inputs[idx], { target: { value: digit } }); | ||
| }); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: /verify email/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(toast.error).toHaveBeenCalledWith('Invalid OTP'); | ||
| expect(inputs[0]).toHaveValue(''); | ||
| expect(inputs[5]).toHaveValue(''); | ||
| }); | ||
| }); | ||
|
|
||
| it('resends OTP successfully', async () => { | ||
| authAPI.post.mockResolvedValueOnce({ data: { success: true } }); | ||
|
|
||
| render(<VerifyOTP />); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: /resend otp/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(authAPI.post).toHaveBeenCalledWith('/auth/resend-otp', { userId: 'user-123' }); | ||
| expect(toast.success).toHaveBeenCalledWith('OTP resent successfully!'); | ||
| }); | ||
| }); | ||
|
|
||
| it('redirects to register when userId is missing', async () => { | ||
| useLocation.mockReturnValueOnce({ state: {} }); | ||
|
|
||
| render(<VerifyOTP />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockNavigate).toHaveBeenCalledWith('/register'); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Strengthen OTP reset assertion to all six fields.
On failure, the component resets every OTP slot, but the test only checks Line 118 and Line 119. This can let partial-reset regressions slip through.
✅ Suggested test assertion update
await waitFor(() => { expect(toast.error).toHaveBeenCalledWith('Invalid OTP'); - expect(inputs[0]).toHaveValue(''); - expect(inputs[5]).toHaveValue(''); + inputs.forEach((input) => expect(input).toHaveValue('')); });📝 Committable suggestion
🤖 Prompt for AI Agents