import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { PhoneInput } from '../../../src/components/common/PhoneInput';
describe('PhoneInput', () => {
it('cleans input digits on change and displays error/helper text', async () => {
const user = userEvent.setup();
const onPhoneChange = vi.fn();
const { rerender } = render(
);
const input = screen.getByRole('textbox');
await user.type(input, '98765');
expect(onPhoneChange).toHaveBeenCalled();
expect(input).toHaveAttribute('placeholder', '10-digit mobile number');
rerender(
);
expect(screen.getByText('Invalid phone')).toBeInTheDocument();
});
it('opens dial code dropdown, searches, clears phone when clearOnDialCodeChange=true, and closes on Escape', async () => {
const user = userEvent.setup();
const onDialCodeChange = vi.fn();
const onPhoneChange = vi.fn();
render(
);
const dialBtn = screen.getByRole('button');
await user.click(dialBtn);
const searchInput = screen.getByPlaceholderText('Search country or code...');
await user.type(searchInput, '49');
await user.click(screen.getByText('Germany'));
expect(onDialCodeChange).toHaveBeenCalledWith('+49', 'Germany');
expect(onPhoneChange).toHaveBeenCalledWith('');
// Reopen and test Escape + outside click
await user.click(dialBtn);
fireEvent.keyDown(document, { key: 'Escape' });
expect(screen.queryByPlaceholderText('Search country or code...')).not.toBeInTheDocument();
await user.click(dialBtn);
fireEvent.mouseDown(screen.getByTestId('outside'));
expect(screen.queryByPlaceholderText('Search country or code...')).not.toBeInTheDocument();
});
it('handles light theme, empty search, clear search button, clamping without clearing, and disabled state', async () => {
const user = userEvent.setup();
const onDialCodeChange = vi.fn();
const onPhoneChange = vi.fn();
const { rerender } = render(
);
await user.click(screen.getByRole('button'));
const searchInput = screen.getByPlaceholderText('Search country or code...');
await user.type(searchInput, 'NoSuchCountry');
expect(screen.getByText(/No country codes found matching/)).toBeInTheDocument();
// Clear search button inside dropdown
const buttons = screen.getAllByRole('button');
const clearBtn = buttons.find((b) => b !== buttons[0]);
if (clearBtn) await user.click(clearBtn);
expect(searchInput).toHaveValue('');
// Select Singapore (+65, maxLength 8) -> clamps 10 digits to 8
await user.type(searchInput, 'Singapore');
await user.click(screen.getByText('Singapore'));
expect(onDialCodeChange).toHaveBeenCalledWith('+65', 'Singapore');
expect(onPhoneChange).toHaveBeenCalledWith('98765432');
// Disabled state does not open dropdown
rerender(
);
await user.click(screen.getByRole('button'));
expect(screen.queryByPlaceholderText('Search country or code...')).not.toBeInTheDocument();
});
it('handles international paste (+91 / 91 prefix) and keydown filtering', () => {
const onDialCodeChange = vi.fn();
const onPhoneChange = vi.fn();
render(
);
const input = screen.getByRole('textbox');
// Pasting full international number starting with +
fireEvent.change(input, { target: { value: '+447123456789' } });
expect(onDialCodeChange).toHaveBeenCalledWith('+44', 'United Kingdom');
expect(onPhoneChange).toHaveBeenCalledWith('7123456789');
// Pasting number starting with dial code digits without '+'
fireEvent.change(input, { target: { value: '919876543210' } });
expect(onPhoneChange).toHaveBeenCalledWith('9876543210');
// Keydown: allows Backspace, Ctrl+V, '+' at position 0, and blocks letters
const allowBackspace = fireEvent.keyDown(input, { key: 'Backspace' });
expect(allowBackspace).toBe(true);
const allowCtrl = fireEvent.keyDown(input, { key: 'v', ctrlKey: true });
expect(allowCtrl).toBe(true);
const allowPlus = fireEvent.keyDown(input, { key: '+' });
expect(allowPlus).toBe(true);
const blockAlpha = fireEvent.keyDown(input, { key: 'a' });
expect(blockAlpha).toBe(false);
});
});