mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Add series fields to audiobooks and update related logic
Introduces 'series' and 'seriesPart' fields to the Audiobook model and database schema. Updates API routes, file organization, and path template utilities to support series metadata. Enhances chapter merging logic, improves notification backend testing, and expands test coverage for admin and API routes.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Component: Card Size Controls Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import React from 'react';
|
||||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
describe('CardSizeControls', () => {
|
||||
beforeEach(() => {
|
||||
window.innerWidth = 1300;
|
||||
});
|
||||
|
||||
it('moves to the next visible size when zooming in or out', async () => {
|
||||
const onSizeChange = vi.fn();
|
||||
const { CardSizeControls } = await import('@/components/ui/CardSizeControls');
|
||||
|
||||
render(<CardSizeControls size={5} onSizeChange={onSizeChange} />);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Zoom in' }));
|
||||
expect(onSizeChange).toHaveBeenCalledWith(6);
|
||||
|
||||
onSizeChange.mockClear();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Zoom out' }));
|
||||
expect(onSizeChange).toHaveBeenCalledWith(4);
|
||||
});
|
||||
|
||||
it('disables zoom controls at the size boundaries', async () => {
|
||||
const onSizeChange = vi.fn();
|
||||
const { CardSizeControls } = await import('@/components/ui/CardSizeControls');
|
||||
|
||||
const { rerender } = render(<CardSizeControls size={1} onSizeChange={onSizeChange} />);
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Zoom out' })).toBeDisabled();
|
||||
|
||||
rerender(<CardSizeControls size={9} onSizeChange={onSizeChange} />);
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Zoom in' })).toBeDisabled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* 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();
|
||||
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();
|
||||
});
|
||||
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Component: Modal Component Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
import { Modal } from '@/components/ui/Modal';
|
||||
|
||||
describe('Modal', () => {
|
||||
it('locks body scroll while open and closes on escape', () => {
|
||||
const onClose = vi.fn();
|
||||
|
||||
const { unmount } = render(
|
||||
<Modal isOpen onClose={onClose} title="Test Modal">
|
||||
<div>Modal Content</div>
|
||||
</Modal>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Modal Content')).toBeInTheDocument();
|
||||
expect(document.body.style.overflow).toBe('hidden');
|
||||
|
||||
fireEvent.keyDown(document, { key: 'Escape' });
|
||||
expect(onClose).toHaveBeenCalledTimes(1);
|
||||
|
||||
unmount();
|
||||
expect(document.body.style.overflow).toBe('unset');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Component: Pagination Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import React from 'react';
|
||||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { Pagination } from '@/components/ui/Pagination';
|
||||
|
||||
describe('Pagination', () => {
|
||||
it('renders nothing when there is only one page', () => {
|
||||
const { container } = render(<Pagination currentPage={1} totalPages={1} onPageChange={vi.fn()} />);
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
|
||||
it('renders ellipses for large page ranges', () => {
|
||||
render(<Pagination currentPage={5} totalPages={10} onPageChange={vi.fn()} />);
|
||||
const ellipses = screen.getAllByText('...');
|
||||
expect(ellipses.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('calls onPageChange for navigation controls', () => {
|
||||
const onPageChange = vi.fn();
|
||||
render(<Pagination currentPage={2} totalPages={5} onPageChange={onPageChange} />);
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Previous page'));
|
||||
expect(onPageChange).toHaveBeenCalledWith(1);
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Next page'));
|
||||
expect(onPageChange).toHaveBeenCalledWith(3);
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Page 4'));
|
||||
expect(onPageChange).toHaveBeenCalledWith(4);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Component: Sticky Pagination Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import React from 'react';
|
||||
import { act, fireEvent, render, screen } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { StickyPagination } from '@/components/ui/StickyPagination';
|
||||
|
||||
type ObserverEntry = {
|
||||
isIntersecting: boolean;
|
||||
intersectionRatio: number;
|
||||
target: Element;
|
||||
};
|
||||
|
||||
describe('StickyPagination', () => {
|
||||
const observers: { callback: IntersectionObserverCallback }[] = [];
|
||||
|
||||
beforeEach(() => {
|
||||
observers.length = 0;
|
||||
class MockIntersectionObserver {
|
||||
callback: IntersectionObserverCallback;
|
||||
observe = vi.fn();
|
||||
unobserve = vi.fn();
|
||||
disconnect = vi.fn();
|
||||
takeRecords = vi.fn();
|
||||
|
||||
constructor(callback: IntersectionObserverCallback) {
|
||||
this.callback = callback;
|
||||
observers.push(this);
|
||||
}
|
||||
}
|
||||
|
||||
(global as any).IntersectionObserver = MockIntersectionObserver;
|
||||
});
|
||||
|
||||
it('returns null when there is only one page', () => {
|
||||
const sectionRef = { current: document.createElement('div') };
|
||||
const { container } = render(
|
||||
<StickyPagination
|
||||
currentPage={1}
|
||||
totalPages={1}
|
||||
onPageChange={vi.fn()}
|
||||
sectionRef={sectionRef}
|
||||
label="Popular"
|
||||
/>
|
||||
);
|
||||
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
|
||||
it('shows and hides based on section and footer visibility', () => {
|
||||
const sectionRef = { current: document.createElement('div') };
|
||||
const footerRef = { current: document.createElement('div') };
|
||||
|
||||
const { container } = render(
|
||||
<StickyPagination
|
||||
currentPage={2}
|
||||
totalPages={5}
|
||||
onPageChange={vi.fn()}
|
||||
sectionRef={sectionRef}
|
||||
footerRef={footerRef}
|
||||
label="Popular"
|
||||
/>
|
||||
);
|
||||
|
||||
const root = container.querySelector('div.fixed') as HTMLElement;
|
||||
expect(root).toHaveClass('opacity-0');
|
||||
|
||||
act(() => {
|
||||
observers[0].callback(
|
||||
[
|
||||
{
|
||||
isIntersecting: true,
|
||||
intersectionRatio: 0.2,
|
||||
target: sectionRef.current as Element,
|
||||
} as ObserverEntry,
|
||||
],
|
||||
observers[0] as unknown as IntersectionObserver
|
||||
);
|
||||
});
|
||||
|
||||
expect(root).toHaveClass('opacity-100');
|
||||
|
||||
act(() => {
|
||||
observers[1].callback(
|
||||
[
|
||||
{
|
||||
isIntersecting: true,
|
||||
intersectionRatio: 0.2,
|
||||
target: footerRef.current as Element,
|
||||
} as ObserverEntry,
|
||||
],
|
||||
observers[1] as unknown as IntersectionObserver
|
||||
);
|
||||
});
|
||||
|
||||
expect(root).toHaveClass('opacity-0');
|
||||
});
|
||||
|
||||
it('handles navigation and jump input updates', () => {
|
||||
const sectionRef = { current: document.createElement('div') };
|
||||
const onPageChange = vi.fn();
|
||||
|
||||
render(
|
||||
<StickyPagination
|
||||
currentPage={2}
|
||||
totalPages={4}
|
||||
onPageChange={onPageChange}
|
||||
sectionRef={sectionRef}
|
||||
label="Popular"
|
||||
/>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Next page'));
|
||||
expect(onPageChange).toHaveBeenCalledWith(3);
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Previous page'));
|
||||
expect(onPageChange).toHaveBeenCalledWith(1);
|
||||
|
||||
const input = screen.getByLabelText('Current page') as HTMLInputElement;
|
||||
fireEvent.change(input, { target: { value: '4' } });
|
||||
fireEvent.blur(input);
|
||||
expect(onPageChange).toHaveBeenCalledWith(4);
|
||||
|
||||
fireEvent.change(input, { target: { value: '99' } });
|
||||
fireEvent.blur(input);
|
||||
expect(input.value).toBe('2');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Component: Toast Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import React from 'react';
|
||||
import { act, fireEvent, render, screen } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { ToastProvider, useToast } from '@/components/ui/Toast';
|
||||
|
||||
const ToastHarness = () => {
|
||||
const { success, error } = useToast();
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button type="button" onClick={() => success('Saved', 1000)}>
|
||||
Add Success
|
||||
</button>
|
||||
<button type="button" onClick={() => error('Failed', 0)}>
|
||||
Add Error
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
describe('ToastProvider', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('adds and auto-removes toasts after the duration', async () => {
|
||||
render(
|
||||
<ToastProvider>
|
||||
<ToastHarness />
|
||||
</ToastProvider>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Add Success' }));
|
||||
expect(screen.getByText('Saved')).toBeInTheDocument();
|
||||
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(1000);
|
||||
});
|
||||
|
||||
expect(screen.queryByText('Saved')).toBeNull();
|
||||
});
|
||||
|
||||
it('removes a toast when the close button is clicked', () => {
|
||||
render(
|
||||
<ToastProvider>
|
||||
<ToastHarness />
|
||||
</ToastProvider>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Add Error' }));
|
||||
expect(screen.getByText('Failed')).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Close' }));
|
||||
expect(screen.queryByText('Failed')).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Component: Version Badge Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { VersionBadge } from '@/components/ui/VersionBadge';
|
||||
|
||||
const originalCommit = process.env.NEXT_PUBLIC_GIT_COMMIT;
|
||||
|
||||
describe('VersionBadge', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
if (originalCommit === undefined) {
|
||||
delete process.env.NEXT_PUBLIC_GIT_COMMIT;
|
||||
} else {
|
||||
process.env.NEXT_PUBLIC_GIT_COMMIT = originalCommit;
|
||||
}
|
||||
});
|
||||
|
||||
it('renders short version from build-time commit', async () => {
|
||||
process.env.NEXT_PUBLIC_GIT_COMMIT = 'abcdef1234';
|
||||
const fetchMock = vi.fn();
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
render(<VersionBadge />);
|
||||
|
||||
expect(await screen.findByText('v.abcdef1')).toBeInTheDocument();
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('falls back to API when build-time commit is unavailable', async () => {
|
||||
process.env.NEXT_PUBLIC_GIT_COMMIT = 'unknown';
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
json: async () => ({ version: 'v.1.2.3' }),
|
||||
});
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
render(<VersionBadge />);
|
||||
|
||||
expect(await screen.findByText('v.1.2.3')).toBeInTheDocument();
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/version');
|
||||
});
|
||||
|
||||
it('shows dev version when API fetch fails', async () => {
|
||||
process.env.NEXT_PUBLIC_GIT_COMMIT = 'unknown';
|
||||
const fetchMock = vi.fn().mockRejectedValue(new Error('down'));
|
||||
const errorMock = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
render(<VersionBadge />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('v.dev')).toBeInTheDocument();
|
||||
});
|
||||
expect(errorMock).toHaveBeenCalledWith('Failed to fetch version:', expect.any(Error));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user