summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts
blob: 9ef2078ec0cd3aba5e12d7abc988c62f76b14f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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: `
    <form>
      <input id="x" type="text" />
      <input id="y" type="password" autofocus />
    </form>
  `
})
export class PasswordFormComponent {}

@Component({
  template: `
    <form>
      <input id="x" type="checkbox" [autofocus]="edit" />
      <input id="y" type="text" />
    </form>
  `
})
export class CheckboxFormComponent {
  public edit = true;
}

@Component({
  template: `
    <form>
      <input id="x" type="text" [autofocus]="foo" />
    </form>
  `
})
export class TextFormComponent {
  foo() {
    return false;
  }
}

describe('AutofocusDirective', () => {
  configureTestBed({
    declarations: [
      AutofocusDirective,
      CheckboxFormComponent,
      PasswordFormComponent,
      TextFormComponent
    ]
  });

  it('should create an instance', () => {
    const directive = new AutofocusDirective(null);
    expect(directive).toBeTruthy();
  });

  it('should focus the password form field', () => {
    const fixture: ComponentFixture<PasswordFormComponent> = 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<CheckboxFormComponent> = 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();
  });

  it('should not focus the text form field', () => {
    const fixture: ComponentFixture<TextFormComponent> = TestBed.createComponent(TextFormComponent);
    fixture.detectChanges();
    const focused = fixture.debugElement.query(By.css(':focus'));
    expect(focused).toBeNull();
    const element = document.getElementById('x');
    expect(element !== document.activeElement).toBeTruthy();
  });
});