Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase coverage for ControlledPasswordInput #17872

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
import { fireEvent, render, screen } from '@testing-library/react';

import ControlledPasswordInput from '../ControlledPasswordInput';
import React from 'react';

describe('ControlledPasswordInput Component', () => {
beforeEach(() => {
// Mock console.warn before each test
jest.spyOn(console, 'warn').mockImplementation(() => {});
});

afterEach(() => {
jest.restoreAllMocks();
});

it('renders the component properly', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
/>
);

const input = screen.getByPlaceholderText('Enter password');
expect(input).toBeInTheDocument();
});

it('renders the component with initial type as password', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
/>
);

const input = screen.getByPlaceholderText('Enter password');
expect(input).toBeInTheDocument();
expect(input).toHaveAttribute('type', 'password');
});

it('allows user to enter text into the input field', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
/>
);

const input = screen.getByPlaceholderText('Enter password');
fireEvent.change(input, { target: { value: 'mypassword' } });

expect(input).toHaveValue('mypassword');
});

it('calls onChange handler when value changes', () => {
const handleChange = jest.fn();

render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
onChange={handleChange}
/>
);

const input = screen.getByPlaceholderText('Enter password');
fireEvent.change(input, { target: { value: 'newpassword' } });

expect(handleChange).toHaveBeenCalledTimes(1);
});

it('calls onBlur handler when input loses focus', () => {
const handleBlur = jest.fn();

render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
onBlur={handleBlur}
/>
);

const input = screen.getByPlaceholderText('Enter password');
input.focus();
input.blur();

expect(handleBlur).toHaveBeenCalledTimes(1);
});

it('has a button with accessible role and label', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
/>
);

const toggleButton = screen.getByRole('button', { name: /show password/i });
expect(toggleButton).toBeInTheDocument();
expect(toggleButton).toHaveAccessibleName('Show password');
});

it('renders controlled input with value from props', () => {
const { rerender } = render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
value="initialValue"
/>
);

const input = screen.getByPlaceholderText('Enter password');
expect(input).toHaveValue('initialValue');

rerender(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
value="newValue"
/>
);

expect(input).toHaveValue('newValue');
});

it('does not toggle visibility when disabled', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
disabled
/>
);

const input = screen.getByPlaceholderText('Enter password');
const toggleButton = screen.getByRole('button', { name: /show password/i });

fireEvent.click(toggleButton);

expect(input).toHaveAttribute('type', 'password');
expect(toggleButton).toHaveTextContent('Show password');
});

it('handles input without value prop (uncontrolled)', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
/>
);

const input = screen.getByPlaceholderText('Enter password');
fireEvent.change(input, { target: { value: 'uncontrolledValue' } });

expect(input).toHaveValue('uncontrolledValue');
});

it('associates the label with the input field', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
/>
);

const label = screen.getByText('Password');
const input = screen.getByPlaceholderText('Enter password');

expect(label).toHaveAttribute('for', 'password-input');
expect(input).toHaveAttribute('id', 'password-input');
});

it('calls onFocus handler when input gains focus', () => {
const handleFocus = jest.fn();

render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
onFocus={handleFocus}
/>
);

const input = screen.getByPlaceholderText('Enter password');
input.focus();

expect(handleFocus).toHaveBeenCalledTimes(1);
});

it('renders correctly without show/hide password labels', () => {
render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
/>
);

const input = screen.getByPlaceholderText('Enter password');
expect(input).toBeInTheDocument();

const toggleButton = screen.getByRole('button');
expect(toggleButton).toBeInTheDocument();
});

it('does not call onChange when input is disabled', () => {
const handleChange = jest.fn();

render(
<ControlledPasswordInput
id="password-input"
labelText="Password"
placeholder="Enter password"
showPasswordLabel="Show password"
hidePasswordLabel="Hide password"
disabled
onChange={handleChange}
/>
);

const input = screen.getByPlaceholderText('Enter password');
fireEvent.change(input, { target: { value: 'newpassword' } });

expect(handleChange).not.toHaveBeenCalled();
});
});
Loading