Repository navigation
refactor: migrate forms to React Hook Form #23
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import React, { useState } from 'react' | ||
| import { useForm } from 'react-hook-form' | ||
| import { useAppContext } from '../../../../context/AppContext' | ||
| import { adminApi } from '../../../../api' | ||
| import { Button, Input } from '../../../ui' | ||
|
|
@@ -7,16 +8,14 @@ import { MESSAGES } from '../../../../constants/messages' | |
|
|
||
| function Login() { | ||
| const { setToken } = useAppContext() | ||
| const [email, setEmail] = useState('') | ||
| const [password, setPassword] = useState('') | ||
| const [loading, setLoading] = useState(false) | ||
| const { register, handleSubmit } = useForm() | ||
|
|
||
| const handleSubmit = async (e) => { | ||
| e.preventDefault() | ||
| const onSubmit = async (data) => { | ||
| setLoading(true) | ||
|
|
||
| try { | ||
| const response = await adminApi.login({ email, password }) | ||
| const response = await adminApi.login(data) | ||
|
|
||
| if (response.data.success) { | ||
| setToken(response.data.token) | ||
|
|
@@ -42,30 +41,28 @@ function Login() { | |
| </h1> | ||
| <p className='font-light'>Enter your credentials to access the admin panel</p> | ||
| </div> | ||
| <form onSubmit={handleSubmit} className='mt-6 w-full sm:max-w-md text-gray-600'> | ||
|
|
||
| <form onSubmit={handleSubmit(onSubmit)} className='mt-6 w-full sm:max-w-md text-gray-600'> | ||
| <Input | ||
| label='Email' | ||
| type='email' | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| placeholder='your email id' | ||
| required | ||
| className='border-b-2 border-gray-300 mb-6 rounded-none border-x-0 border-t-0' | ||
| {...register('email', { required: true })} | ||
| /> | ||
|
|
||
| <Input | ||
| label='Password' | ||
| type='password' | ||
| value={password} | ||
| onChange={(e) => setPassword(e.target.value)} | ||
| placeholder='your password' | ||
| required | ||
| className='border-b-2 border-gray-300 mb-6 rounded-none border-x-0 border-t-0' | ||
| {...register('password', { required: true })} | ||
| /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General Reviewer · Login form keeps native validation path, so RHF required rules never run (suggestion) The form has no Suggestion: Either add |
||
| <Button | ||
| type='submit' | ||
|
|
||
| <Button | ||
| type='submit' | ||
| variant='primary' | ||
| fullWidth | ||
| loading={loading} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,94 +1,80 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
| import { render, screen, waitFor } from '@testing-library/react' | ||
| import userEvent from '@testing-library/user-event' | ||
| import { setupServer } from 'msw/node' | ||
| import { http, HttpResponse } from 'msw' | ||
| import toast from 'react-hot-toast' | ||
| import Login from '.' | ||
|
|
||
| vi.mock('react-hot-toast', () => ({ | ||
| default: { success: vi.fn(), error: vi.fn() }, | ||
| })) | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
| import Login from './Login' | ||
|
|
||
| const mockSetToken = vi.fn() | ||
|
|
||
| vi.mock('../../../../context/AppContext', () => ({ | ||
| useAppContext: () => ({ | ||
| setToken: mockSetToken, | ||
| }), | ||
| useAppContext: () => ({ setToken: mockSetToken }) | ||
| })) | ||
|
|
||
| const server = setupServer() | ||
| const mockLogin = vi.fn() | ||
| vi.mock('../../../../api', () => ({ | ||
| adminApi: { login: (...args) => mockLogin(...args) } | ||
| })) | ||
|
|
||
| beforeAll(() => server.listen()) | ||
| afterEach(() => { | ||
| server.resetHandlers() | ||
| vi.clearAllMocks() | ||
| localStorage.clear() | ||
| }) | ||
| afterAll(() => server.close()) | ||
| vi.mock('react-hot-toast', () => ({ | ||
| default: { success: vi.fn(), error: vi.fn() } | ||
| })) | ||
|
|
||
| vi.mock('../../../../constants/messages', () => ({ | ||
| MESSAGES: { | ||
| SUCCESS_LOGIN: 'Login successful', | ||
| ERROR_LOGIN: 'Login failed' | ||
| } | ||
| })) | ||
|
|
||
| describe('Login', () => { | ||
| it('successful login sets token and shows success toast', async () => { | ||
| server.use( | ||
| http.post('http://localhost:5001/api/admin/login', () => | ||
| HttpResponse.json({ success: true, token: 'jwt123' }) | ||
| ) | ||
| ) | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| const user = userEvent.setup() | ||
| it('renders email and password inputs', () => { | ||
| render(<Login />) | ||
|
|
||
| await user.type(screen.getByPlaceholderText('your email id'), 'admin@quickblog.com') | ||
| await user.type(screen.getByPlaceholderText('your password'), 'admin123') | ||
| await user.click(screen.getByRole('button', { name: /login/i })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockSetToken).toHaveBeenCalledWith('jwt123') | ||
| }) | ||
|
|
||
| expect(localStorage.getItem('token')).toBe('jwt123') | ||
| expect(toast.success).toHaveBeenCalled() | ||
| expect(screen.getByPlaceholderText('your email id')).toBeInTheDocument() | ||
| expect(screen.getByPlaceholderText('your password')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('shows error toast on invalid credentials', async () => { | ||
| server.use( | ||
| http.post('http://localhost:5001/api/admin/login', () => | ||
| HttpResponse.json({ message: 'Invalid credentials' }, { status: 401 }) | ||
| ) | ||
| ) | ||
| it('renders login button', () => { | ||
| render(<Login />) | ||
| expect(screen.getByRole('button', { name: /login/i })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('submits form with email and password', async () => { | ||
| const user = userEvent.setup() | ||
| mockLogin.mockResolvedValue({ data: { success: true, token: 'abc123' } }) | ||
|
|
||
| render(<Login />) | ||
|
|
||
| await user.type(screen.getByPlaceholderText('your email id'), 'wrong@email.com') | ||
| await user.type(screen.getByPlaceholderText('your password'), 'wrongpass') | ||
| await user.type(screen.getByPlaceholderText('your email id'), 'test@example.com') | ||
| await user.type(screen.getByPlaceholderText('your password'), 'password123') | ||
| await user.click(screen.getByRole('button', { name: /login/i })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(toast.error).toHaveBeenCalled() | ||
| expect(mockLogin).toHaveBeenCalledWith({ | ||
| email: 'test@example.com', | ||
| password: 'password123' | ||
| }) | ||
| }) | ||
|
|
||
| expect(mockSetToken).not.toHaveBeenCalled() | ||
| await waitFor(() => { | ||
| expect(mockSetToken).toHaveBeenCalledWith('abc123') | ||
| }) | ||
| }) | ||
|
|
||
| it('disables submit button while loading', async () => { | ||
| server.use( | ||
| http.post('http://localhost:5001/api/admin/login', async () => { | ||
| await new Promise((resolve) => setTimeout(resolve, 500)) | ||
| return HttpResponse.json({ success: true, token: 'jwt123' }) | ||
| }) | ||
| ) | ||
|
|
||
| it('shows error toast on failed login', async () => { | ||
| const user = userEvent.setup() | ||
| const toast = (await import('react-hot-toast')).default | ||
| mockLogin.mockRejectedValue({ response: { data: { message: 'Bad creds' } } }) | ||
|
|
||
| render(<Login />) | ||
|
|
||
| await user.type(screen.getByPlaceholderText('your email id'), 'admin@quickblog.com') | ||
| await user.type(screen.getByPlaceholderText('your password'), 'admin123') | ||
| await user.type(screen.getByPlaceholderText('your email id'), 'test@example.com') | ||
| await user.type(screen.getByPlaceholderText('your password'), 'wrong') | ||
| await user.click(screen.getByRole('button', { name: /login/i })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByRole('button', { name: /loading/i })).toBeDisabled() | ||
| expect(toast.error).toHaveBeenCalledWith('Bad creds') | ||
| }) | ||
| }) | ||
| }) |
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.
General Reviewer · Login and CommentForm can silently do nothing on whitespace-only required inputs (warning)