| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /**
- * Tests for the ReprintModal component.
- */
- import { describe, it, expect, vi, beforeEach } from 'vitest';
- import { screen, waitFor } from '@testing-library/react';
- import userEvent from '@testing-library/user-event';
- import { render } from '../utils';
- import { ReprintModal } from '../../components/ReprintModal';
- import { http, HttpResponse } from 'msw';
- import { server } from '../mocks/server';
- const mockPrinters = [
- { id: 1, name: 'X1 Carbon', model: 'X1C', ip_address: '192.168.1.100', enabled: true, is_active: true },
- { id: 2, name: 'P1S', model: 'P1S', ip_address: '192.168.1.101', enabled: true, is_active: true },
- ];
- describe('ReprintModal', () => {
- const mockOnClose = vi.fn();
- const mockOnSuccess = vi.fn();
- beforeEach(() => {
- vi.clearAllMocks();
- server.use(
- http.get('/api/v1/printers/', () => {
- return HttpResponse.json(mockPrinters);
- }),
- http.get('/api/v1/archives/:id/plates', () => {
- return HttpResponse.json({ is_multi_plate: false, plates: [] });
- }),
- http.get('/api/v1/archives/:id/filament-requirements', () => {
- return HttpResponse.json({ filaments: [] });
- }),
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ connected: true, state: 'IDLE', ams: [], vt_tray: null });
- }),
- http.post('/api/v1/archives/:id/reprint', () => {
- return HttpResponse.json({ success: true });
- })
- );
- });
- describe('rendering', () => {
- it('renders the modal title', () => {
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- expect(screen.getByText('Re-print')).toBeInTheDocument();
- });
- it('shows archive name', () => {
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- expect(screen.getByText('Benchy')).toBeInTheDocument();
- });
- it('shows printer selection buttons', async () => {
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.getByText('P1S')).toBeInTheDocument();
- });
- });
- });
- describe('printer selection', () => {
- it('shows active printers as buttons', async () => {
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- await waitFor(() => {
- // Printer buttons should be present
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- });
- it('shows no printers message when none active', async () => {
- server.use(
- http.get('/api/v1/printers/', () => {
- return HttpResponse.json([]);
- })
- );
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('No active printers available')).toBeInTheDocument();
- });
- });
- });
- describe('actions', () => {
- it('has print button', () => {
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- expect(screen.getByRole('button', { name: /print/i })).toBeInTheDocument();
- });
- it('has cancel button', () => {
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- expect(screen.getByRole('button', { name: /cancel/i })).toBeInTheDocument();
- });
- it('calls onClose when cancel is clicked', async () => {
- const user = userEvent.setup();
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- await user.click(screen.getByRole('button', { name: /cancel/i }));
- expect(mockOnClose).toHaveBeenCalled();
- });
- it('print button is disabled until printer is selected', async () => {
- render(
- <ReprintModal
- archiveId={1}
- archiveName="Benchy"
- onClose={mockOnClose}
- onSuccess={mockOnSuccess}
- />
- );
- // Print button should be disabled initially (no printer selected)
- const printButton = screen.getByRole('button', { name: /print/i });
- expect(printButton).toBeDisabled();
- });
- });
- });
|