@@ -33,6 +33,15 @@ class TestAutofocusDynamicComponent {
3333 autofocusEnabled = false ;
3434}
3535
36+ @Component ( {
37+ standalone : false ,
38+ selector : 'test-autofocus-undefined' ,
39+ template : `<input type="text" [pAutoFocus]="autofocus" />`
40+ } )
41+ class TestAutofocusUndefinedComponent {
42+ autofocus : any = undefined ;
43+ }
44+
3645@Component ( {
3746 standalone : false ,
3847 selector : 'test-autofocus-button' ,
@@ -198,6 +207,7 @@ describe('AutoFocus', () => {
198207 TestAutofocusDisabledComponent ,
199208 TestAutofocusEnabledComponent ,
200209 TestAutofocusDynamicComponent ,
210+ TestAutofocusUndefinedComponent ,
201211 TestAutofocusButtonComponent ,
202212 TestAutofocusDivComponent ,
203213 TestAutofocusMultipleElementsComponent ,
@@ -300,6 +310,38 @@ describe('AutoFocus', () => {
300310 } ) ;
301311 } ) ;
302312
313+ describe ( 'Undefined Binding' , ( ) => {
314+ it ( 'should not set autofocus attribute when bound value is undefined' , async ( ) => {
315+ const fixture = TestBed . createComponent ( TestAutofocusUndefinedComponent ) ;
316+ const component = fixture . componentInstance ;
317+ await fixture . whenStable ( ) ;
318+
319+ const element = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
320+
321+ spyOn ( element , 'focus' ) ;
322+
323+ expect ( element . hasAttribute ( 'autofocus' ) ) . toBe ( false ) ;
324+
325+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
326+ expect ( element . focus ) . not . toHaveBeenCalled ( ) ;
327+ } ) ;
328+
329+ it ( 'should set autofocus attribute and focus when property changes to true' , async ( ) => {
330+ const fixture = TestBed . createComponent ( TestAutofocusUndefinedComponent ) ;
331+ const component = fixture . componentInstance ;
332+ await fixture . whenStable ( ) ;
333+
334+ const element = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
335+
336+ component . autofocus = true ;
337+ fixture . changeDetectorRef . markForCheck ( ) ;
338+ await fixture . whenStable ( ) ;
339+
340+ expect ( element . hasAttribute ( 'autofocus' ) ) . toBe ( true ) ;
341+ expect ( element . getAttribute ( 'autofocus' ) ) . toBe ( 'true' ) ;
342+ } ) ;
343+ } ) ;
344+
303345 describe ( 'Focus Behavior - Browser Platform' , ( ) => {
304346 it ( 'should focus input element when autofocus is enabled' , async ( ) => {
305347 const fixture = TestBed . createComponent ( TestAutofocusEnabledComponent ) ;
0 commit comments