|
| 1 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 2 | + |
| 3 | +import ControlledPasswordInput from '../ControlledPasswordInput'; |
| 4 | +import React from 'react'; |
| 5 | + |
| 6 | +describe('ControlledPasswordInput Component', () => { |
| 7 | + beforeEach(() => { |
| 8 | + // Mock console.warn before each test |
| 9 | + jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + jest.restoreAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('renders the component properly', () => { |
| 17 | + render( |
| 18 | + <ControlledPasswordInput |
| 19 | + id="password-input" |
| 20 | + labelText="Password" |
| 21 | + placeholder="Enter password" |
| 22 | + /> |
| 23 | + ); |
| 24 | + |
| 25 | + const input = screen.getByPlaceholderText('Enter password'); |
| 26 | + expect(input).toBeInTheDocument(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('renders the component with initial type as password', () => { |
| 30 | + render( |
| 31 | + <ControlledPasswordInput |
| 32 | + id="password-input" |
| 33 | + labelText="Password" |
| 34 | + placeholder="Enter password" |
| 35 | + showPasswordLabel="Show password" |
| 36 | + hidePasswordLabel="Hide password" |
| 37 | + /> |
| 38 | + ); |
| 39 | + |
| 40 | + const input = screen.getByPlaceholderText('Enter password'); |
| 41 | + expect(input).toBeInTheDocument(); |
| 42 | + expect(input).toHaveAttribute('type', 'password'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('allows user to enter text into the input field', () => { |
| 46 | + render( |
| 47 | + <ControlledPasswordInput |
| 48 | + id="password-input" |
| 49 | + labelText="Password" |
| 50 | + placeholder="Enter password" |
| 51 | + showPasswordLabel="Show password" |
| 52 | + hidePasswordLabel="Hide password" |
| 53 | + /> |
| 54 | + ); |
| 55 | + |
| 56 | + const input = screen.getByPlaceholderText('Enter password'); |
| 57 | + fireEvent.change(input, { target: { value: 'mypassword' } }); |
| 58 | + |
| 59 | + expect(input).toHaveValue('mypassword'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('calls onChange handler when value changes', () => { |
| 63 | + const handleChange = jest.fn(); |
| 64 | + |
| 65 | + render( |
| 66 | + <ControlledPasswordInput |
| 67 | + id="password-input" |
| 68 | + labelText="Password" |
| 69 | + placeholder="Enter password" |
| 70 | + showPasswordLabel="Show password" |
| 71 | + hidePasswordLabel="Hide password" |
| 72 | + onChange={handleChange} |
| 73 | + /> |
| 74 | + ); |
| 75 | + |
| 76 | + const input = screen.getByPlaceholderText('Enter password'); |
| 77 | + fireEvent.change(input, { target: { value: 'newpassword' } }); |
| 78 | + |
| 79 | + expect(handleChange).toHaveBeenCalledTimes(1); |
| 80 | + }); |
| 81 | + |
| 82 | + it('calls onBlur handler when input loses focus', () => { |
| 83 | + const handleBlur = jest.fn(); |
| 84 | + |
| 85 | + render( |
| 86 | + <ControlledPasswordInput |
| 87 | + id="password-input" |
| 88 | + labelText="Password" |
| 89 | + placeholder="Enter password" |
| 90 | + showPasswordLabel="Show password" |
| 91 | + hidePasswordLabel="Hide password" |
| 92 | + onBlur={handleBlur} |
| 93 | + /> |
| 94 | + ); |
| 95 | + |
| 96 | + const input = screen.getByPlaceholderText('Enter password'); |
| 97 | + input.focus(); |
| 98 | + input.blur(); |
| 99 | + |
| 100 | + expect(handleBlur).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | + |
| 103 | + it('has a button with accessible role and label', () => { |
| 104 | + render( |
| 105 | + <ControlledPasswordInput |
| 106 | + id="password-input" |
| 107 | + labelText="Password" |
| 108 | + placeholder="Enter password" |
| 109 | + showPasswordLabel="Show password" |
| 110 | + hidePasswordLabel="Hide password" |
| 111 | + /> |
| 112 | + ); |
| 113 | + |
| 114 | + const toggleButton = screen.getByRole('button', { name: /show password/i }); |
| 115 | + expect(toggleButton).toBeInTheDocument(); |
| 116 | + expect(toggleButton).toHaveAccessibleName('Show password'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('renders controlled input with value from props', () => { |
| 120 | + const { rerender } = render( |
| 121 | + <ControlledPasswordInput |
| 122 | + id="password-input" |
| 123 | + labelText="Password" |
| 124 | + placeholder="Enter password" |
| 125 | + showPasswordLabel="Show password" |
| 126 | + hidePasswordLabel="Hide password" |
| 127 | + value="initialValue" |
| 128 | + /> |
| 129 | + ); |
| 130 | + |
| 131 | + const input = screen.getByPlaceholderText('Enter password'); |
| 132 | + expect(input).toHaveValue('initialValue'); |
| 133 | + |
| 134 | + rerender( |
| 135 | + <ControlledPasswordInput |
| 136 | + id="password-input" |
| 137 | + labelText="Password" |
| 138 | + placeholder="Enter password" |
| 139 | + showPasswordLabel="Show password" |
| 140 | + hidePasswordLabel="Hide password" |
| 141 | + value="newValue" |
| 142 | + /> |
| 143 | + ); |
| 144 | + |
| 145 | + expect(input).toHaveValue('newValue'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('does not toggle visibility when disabled', () => { |
| 149 | + render( |
| 150 | + <ControlledPasswordInput |
| 151 | + id="password-input" |
| 152 | + labelText="Password" |
| 153 | + placeholder="Enter password" |
| 154 | + showPasswordLabel="Show password" |
| 155 | + hidePasswordLabel="Hide password" |
| 156 | + disabled |
| 157 | + /> |
| 158 | + ); |
| 159 | + |
| 160 | + const input = screen.getByPlaceholderText('Enter password'); |
| 161 | + const toggleButton = screen.getByRole('button', { name: /show password/i }); |
| 162 | + |
| 163 | + fireEvent.click(toggleButton); |
| 164 | + |
| 165 | + expect(input).toHaveAttribute('type', 'password'); |
| 166 | + expect(toggleButton).toHaveTextContent('Show password'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('handles input without value prop (uncontrolled)', () => { |
| 170 | + render( |
| 171 | + <ControlledPasswordInput |
| 172 | + id="password-input" |
| 173 | + labelText="Password" |
| 174 | + placeholder="Enter password" |
| 175 | + showPasswordLabel="Show password" |
| 176 | + hidePasswordLabel="Hide password" |
| 177 | + /> |
| 178 | + ); |
| 179 | + |
| 180 | + const input = screen.getByPlaceholderText('Enter password'); |
| 181 | + fireEvent.change(input, { target: { value: 'uncontrolledValue' } }); |
| 182 | + |
| 183 | + expect(input).toHaveValue('uncontrolledValue'); |
| 184 | + }); |
| 185 | + |
| 186 | + it('associates the label with the input field', () => { |
| 187 | + render( |
| 188 | + <ControlledPasswordInput |
| 189 | + id="password-input" |
| 190 | + labelText="Password" |
| 191 | + placeholder="Enter password" |
| 192 | + /> |
| 193 | + ); |
| 194 | + |
| 195 | + const label = screen.getByText('Password'); |
| 196 | + const input = screen.getByPlaceholderText('Enter password'); |
| 197 | + |
| 198 | + expect(label).toHaveAttribute('for', 'password-input'); |
| 199 | + expect(input).toHaveAttribute('id', 'password-input'); |
| 200 | + }); |
| 201 | + |
| 202 | + it('calls onFocus handler when input gains focus', () => { |
| 203 | + const handleFocus = jest.fn(); |
| 204 | + |
| 205 | + render( |
| 206 | + <ControlledPasswordInput |
| 207 | + id="password-input" |
| 208 | + labelText="Password" |
| 209 | + placeholder="Enter password" |
| 210 | + showPasswordLabel="Show password" |
| 211 | + hidePasswordLabel="Hide password" |
| 212 | + onFocus={handleFocus} |
| 213 | + /> |
| 214 | + ); |
| 215 | + |
| 216 | + const input = screen.getByPlaceholderText('Enter password'); |
| 217 | + input.focus(); |
| 218 | + |
| 219 | + expect(handleFocus).toHaveBeenCalledTimes(1); |
| 220 | + }); |
| 221 | + |
| 222 | + it('renders correctly without show/hide password labels', () => { |
| 223 | + render( |
| 224 | + <ControlledPasswordInput |
| 225 | + id="password-input" |
| 226 | + labelText="Password" |
| 227 | + placeholder="Enter password" |
| 228 | + /> |
| 229 | + ); |
| 230 | + |
| 231 | + const input = screen.getByPlaceholderText('Enter password'); |
| 232 | + expect(input).toBeInTheDocument(); |
| 233 | + |
| 234 | + const toggleButton = screen.getByRole('button'); |
| 235 | + expect(toggleButton).toBeInTheDocument(); |
| 236 | + }); |
| 237 | + |
| 238 | + it('does not call onChange when input is disabled', () => { |
| 239 | + const handleChange = jest.fn(); |
| 240 | + |
| 241 | + render( |
| 242 | + <ControlledPasswordInput |
| 243 | + id="password-input" |
| 244 | + labelText="Password" |
| 245 | + placeholder="Enter password" |
| 246 | + showPasswordLabel="Show password" |
| 247 | + hidePasswordLabel="Hide password" |
| 248 | + disabled |
| 249 | + onChange={handleChange} |
| 250 | + /> |
| 251 | + ); |
| 252 | + |
| 253 | + const input = screen.getByPlaceholderText('Enter password'); |
| 254 | + fireEvent.change(input, { target: { value: 'newpassword' } }); |
| 255 | + |
| 256 | + expect(handleChange).not.toHaveBeenCalled(); |
| 257 | + }); |
| 258 | +}); |
0 commit comments