162 lines
5.3 KiB
TypeScript
162 lines
5.3 KiB
TypeScript
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(
|
|
<PhoneInput
|
|
dialCode="+91"
|
|
phone=""
|
|
country="India"
|
|
onPhoneChange={onPhoneChange}
|
|
theme="light"
|
|
/>
|
|
);
|
|
|
|
const input = screen.getByRole('textbox');
|
|
await user.type(input, '98765');
|
|
expect(onPhoneChange).toHaveBeenCalled();
|
|
expect(input).toHaveAttribute('placeholder', '10-digit mobile number');
|
|
|
|
rerender(
|
|
<PhoneInput
|
|
dialCode="+49"
|
|
phone="151"
|
|
country="Germany"
|
|
onPhoneChange={onPhoneChange}
|
|
error="Invalid phone"
|
|
theme="dark"
|
|
/>
|
|
);
|
|
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(
|
|
<div>
|
|
<div data-testid="outside">Outside</div>
|
|
<PhoneInput
|
|
dialCode="+91"
|
|
phone="9876543210"
|
|
country="India"
|
|
onDialCodeChange={onDialCodeChange}
|
|
onPhoneChange={onPhoneChange}
|
|
clearOnDialCodeChange
|
|
/>
|
|
</div>
|
|
);
|
|
|
|
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(
|
|
<PhoneInput
|
|
dialCode="+91"
|
|
phone="9876543210"
|
|
onDialCodeChange={onDialCodeChange}
|
|
onPhoneChange={onPhoneChange}
|
|
theme="light"
|
|
/>
|
|
);
|
|
|
|
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(
|
|
<PhoneInput
|
|
dialCode="+91"
|
|
phone=""
|
|
onPhoneChange={onPhoneChange}
|
|
disabled
|
|
/>
|
|
);
|
|
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(
|
|
<PhoneInput
|
|
dialCode="+91"
|
|
phone=""
|
|
country="India"
|
|
onDialCodeChange={onDialCodeChange}
|
|
onPhoneChange={onPhoneChange}
|
|
/>
|
|
);
|
|
|
|
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);
|
|
});
|
|
});
|