import { render, screen, fireEvent } from '@testing-library/react'; import { describe, it, expect, vi } from 'vitest'; import OtpInput5 from '../OtpInput5'; describe('OtpInput5 Component', () => { it('renders 5 input boxes', () => { render(); const inputs = screen.getAllByRole('textbox'); expect(inputs).toHaveLength(5); }); it('updates digit and focuses next input on typing', () => { const handleChange = vi.fn(); render(); const inputs = screen.getAllByRole('textbox'); fireEvent.change(inputs[0], { target: { value: '4' } }); expect(handleChange).toHaveBeenCalledWith('4'); }); it('converts Persian digits automatically to English', () => { const handleChange = vi.fn(); render(); const inputs = screen.getAllByRole('textbox'); fireEvent.change(inputs[0], { target: { value: '۹' } }); expect(handleChange).toHaveBeenCalledWith('9'); }); it('distributes full 5-digit pasted code and calls onComplete', () => { const handleChange = vi.fn(); const handleComplete = vi.fn(); render( , ); const inputs = screen.getAllByRole('textbox'); fireEvent.paste(inputs[0], { clipboardData: { getData: () => '12345', }, }); expect(handleChange).toHaveBeenCalledWith('12345'); expect(handleComplete).toHaveBeenCalledWith('12345'); }); it('handles backspace to navigate to previous input when current is empty', () => { const handleChange = vi.fn(); render(); const inputs = screen.getAllByRole('textbox'); fireEvent.keyDown(inputs[2], { key: 'Backspace' }); expect(handleChange).toHaveBeenCalledWith('1'); }); });