mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-02 20:30:10 +00:00
20c8fb0898
Introduce user-reported-issues and Goodreads shelf sync features and wire them into notifications. Adds Prisma migrations and schema changes (ReportedIssue, GoodreadsShelf, GoodreadsBookMapping), API endpoints for reporting (POST /audiobooks/[asin]/report-issue) and admin management (list, resolve/dismiss, replace), and an admin UI section to view/dismiss/replace reported issues. Adds a new notification event (issue_reported) with updates to notification schemas, docs and provider handling, plus a notification-events constants file. Refactors request creation to use createRequestForUser service, adds a Goodreads sync processor/service/hooks/UI modals, a scrape-resilience util, and related tests and minor integration updates.
123 lines
4.0 KiB
TypeScript
123 lines
4.0 KiB
TypeScript
/**
|
|
* Component: Change Password Modal Tests
|
|
* Documentation: documentation/frontend/components.md
|
|
*/
|
|
|
|
// @vitest-environment jsdom
|
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
import { ChangePasswordModal } from '@/components/ui/ChangePasswordModal';
|
|
|
|
describe('ChangePasswordModal', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.useRealTimers();
|
|
localStorage.clear();
|
|
});
|
|
|
|
it('shows validation errors when required fields are missing', () => {
|
|
render(<ChangePasswordModal isOpen onClose={vi.fn()} />);
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Change Password' }));
|
|
|
|
expect(screen.getByText('Current password is required')).toBeInTheDocument();
|
|
expect(screen.getByText('New password is required')).toBeInTheDocument();
|
|
expect(screen.getByText('Please confirm your new password')).toBeInTheDocument();
|
|
});
|
|
|
|
it('rejects submission when access token is missing', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) });
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
render(<ChangePasswordModal isOpen onClose={vi.fn()} />);
|
|
|
|
fireEvent.change(screen.getByLabelText('Current Password'), {
|
|
target: { value: 'old-password' },
|
|
});
|
|
fireEvent.change(screen.getByLabelText('New Password'), {
|
|
target: { value: 'new-password' },
|
|
});
|
|
fireEvent.change(screen.getByLabelText('Confirm New Password'), {
|
|
target: { value: 'new-password' },
|
|
});
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Change Password' }));
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('Not authenticated')).toBeInTheDocument();
|
|
});
|
|
|
|
// Only the password policy fetch should have fired (useEffect on mount), not a password change call
|
|
expect(fetchMock).not.toHaveBeenCalledWith('/api/auth/change-password', expect.anything());
|
|
});
|
|
|
|
it('submits successfully and auto-closes after showing success', async () => {
|
|
vi.useFakeTimers();
|
|
const onClose = vi.fn();
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ success: true }),
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
localStorage.setItem('accessToken', 'token');
|
|
|
|
render(<ChangePasswordModal isOpen onClose={onClose} />);
|
|
|
|
fireEvent.change(screen.getByLabelText('Current Password'), {
|
|
target: { value: 'old-password' },
|
|
});
|
|
fireEvent.change(screen.getByLabelText('New Password'), {
|
|
target: { value: 'new-password' },
|
|
});
|
|
fireEvent.change(screen.getByLabelText('Confirm New Password'), {
|
|
target: { value: 'new-password' },
|
|
});
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Change Password' }));
|
|
|
|
await act(async () => {
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
});
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'/api/auth/change-password',
|
|
expect.objectContaining({ method: 'POST' })
|
|
);
|
|
|
|
expect(screen.getByText('Password changed successfully!')).toBeInTheDocument();
|
|
|
|
act(() => {
|
|
vi.advanceTimersByTime(2000);
|
|
});
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('shows server error responses', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: false,
|
|
json: async () => ({ error: 'Invalid password' }),
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
localStorage.setItem('accessToken', 'token');
|
|
|
|
render(<ChangePasswordModal isOpen onClose={vi.fn()} />);
|
|
|
|
fireEvent.change(screen.getByLabelText('Current Password'), {
|
|
target: { value: 'old-password' },
|
|
});
|
|
fireEvent.change(screen.getByLabelText('New Password'), {
|
|
target: { value: 'new-password' },
|
|
});
|
|
fireEvent.change(screen.getByLabelText('Confirm New Password'), {
|
|
target: { value: 'new-password' },
|
|
});
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Change Password' }));
|
|
|
|
expect(await screen.findByText('Invalid password')).toBeInTheDocument();
|
|
});
|
|
});
|