summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/trim.directive.spec.ts
blob: daef6b3c8152e26bbcaca53dea6b4d0f32cddd3c (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
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';

import { configureTestBed } from '~/testing/unit-test-helper';
import { CdFormGroup } from '../forms/cd-form-group';
import { TrimDirective } from './trim.directive';

@Component({
  template: `
    <form [formGroup]="trimForm">
      <input type="text" formControlName="trimInput" cdTrim />
    </form>
  `
})
export class TrimComponent {
  trimForm: CdFormGroup;
  constructor() {
    this.trimForm = new CdFormGroup({
      trimInput: new FormControl()
    });
  }
}

describe('TrimDirective', () => {
  configureTestBed({
    imports: [FormsModule, ReactiveFormsModule],
    declarations: [TrimDirective, TrimComponent]
  });

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

  it('should trim', () => {
    const fixture: ComponentFixture<TrimComponent> = TestBed.createComponent(TrimComponent);
    const component: TrimComponent = fixture.componentInstance;
    const inputElement: HTMLInputElement = fixture.debugElement.query(By.css('input'))
      .nativeElement;
    fixture.detectChanges();

    inputElement.value = ' a b ';
    inputElement.dispatchEvent(new Event('input'));
    const expectedValue = 'a b';
    expect(inputElement.value).toBe(expectedValue);
    expect(component.trimForm.getValue('trimInput')).toBe(expectedValue);
  });
});