From f3d25fb3a8a72bb42f573470fb424e2794e5c973 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Tue, 4 Sep 2018 12:03:09 +0200 Subject: [PATCH] mgr/dashboard: Refactor autofocus directive Refactor the autofocus directive and add some unit tests. Signed-off-by: Volker Theile --- .../directives/autofocus.directive.spec.ts | 51 +++++++++++++++++++ .../shared/directives/autofocus.directive.ts | 17 ++++--- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts index 3126b4cfba76f..27074ef606e92 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts @@ -1,8 +1,59 @@ +import { Component } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; + +import { configureTestBed } from '../../../testing/unit-test-helper'; import { AutofocusDirective } from './autofocus.directive'; +@Component({ + template: ` +
+ + +
` +}) +export class PasswordFormComponent {} + +@Component({ + template: ` +
+ + +
` +}) +export class CheckboxFormComponent {} + describe('AutofocusDirective', () => { + configureTestBed({ + declarations: [AutofocusDirective, CheckboxFormComponent, PasswordFormComponent] + }); + it('should create an instance', () => { const directive = new AutofocusDirective(null); expect(directive).toBeTruthy(); }); + + it('should focus the password form field', () => { + const fixture: ComponentFixture = TestBed.createComponent( + PasswordFormComponent + ); + fixture.detectChanges(); + const focused = fixture.debugElement.query(By.css(':focus')); + expect(focused.attributes.id).toBe('y'); + expect(focused.attributes.type).toBe('password'); + const element = document.getElementById('y'); + expect(element === document.activeElement).toBeTruthy(); + }); + + it('should focus the checkbox form field', () => { + const fixture: ComponentFixture = TestBed.createComponent( + CheckboxFormComponent + ); + fixture.detectChanges(); + const focused = fixture.debugElement.query(By.css(':focus')); + expect(focused.attributes.id).toBe('x'); + expect(focused.attributes.type).toBe('checkbox'); + const element = document.getElementById('x'); + expect(element === document.activeElement).toBeTruthy(); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts index 14f5efef7046f..809d8b76421d6 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts @@ -1,16 +1,17 @@ -import { Directive, ElementRef, OnInit } from '@angular/core'; +import { AfterViewInit, Directive, ElementRef } from '@angular/core'; + +import * as _ from 'lodash'; @Directive({ selector: '[autofocus]' // tslint:disable-line }) -export class AutofocusDirective implements OnInit { +export class AutofocusDirective implements AfterViewInit { constructor(private elementRef: ElementRef) {} - ngOnInit() { - setTimeout(() => { - if (this.elementRef && this.elementRef.nativeElement) { - this.elementRef.nativeElement.focus(); - } - }, 0); + ngAfterViewInit() { + const el: HTMLInputElement = this.elementRef.nativeElement; + if (_.isFunction(el.focus)) { + el.focus(); + } } } -- 2.39.5