import { TestBed , async , ComponentFixture } from '@angular/core/testing' ; import { Component , DebugElement } from '@angular/core' ; import { By } from '@angular/platform-browser' ; import { NgForm , FormsModule } from '@angular/forms' ; import { RoundTrip } from './roundtrip.validator' ; @ Component ( { template : `
<form #f="ngForm" round-trip>
        <input [(ngModel)]="from" name="from">
        <input [(ngModel)]="to" name="to">
</form>
` } ) class TestComponent { } let fixture ; let rootFormGroup ; let fromInput ; let toInput ; let compInstance ; describe ( 'RoundTrip' , ( ) => { beforeEach ( async ( ( ) => { fixture = TestBed . configureTestingModule ( { imports : [ FormsModule ] , declarations : [ RoundTrip , TestComponent ] } ) . createComponent ( TestComponent ) ; fixture . detectChanges ( ) ; let inputs = fixture . debugElement . queryAll ( By . css ( 'input' ) ) ; fromInput = inputs [ 0 ] . nativeElement ; toInput = inputs [ 1 ] . nativeElement ; compInstance = fixture . componentInstance ; let form = fixture . debugElement . query ( By . directive ( RoundTrip ) ) ; rootFormGroup = form . references [ 'f' ] ; } ) ) ; it ( 'should raise error if from and to city are the same location' , ( ) => { expect ( rootFormGroup . control . hasError ( 'round-trip' ) ) . toBe ( true ) ; expect ( rootFormGroup . control . valid ) . toBe ( false ) ; fromInput . value = 'Graz' ; fromInput . dispatchEvent ( new Event ( 'input' ) ) ; expect ( rootFormGroup . control . hasError ( 'round-trip' ) ) . toBe ( false ) ; expect ( rootFormGroup . control . valid ) . toBe ( true ) ; toInput . value = 'Graz' ; toInput . dispatchEvent ( new Event ( 'input' ) ) ; expect ( rootFormGroup . control . hasError ( 'round-trip' ) ) . toBe ( true ) ; expect ( rootFormGroup . control . valid ) . toBe ( false ) ; } ) ; it ( 'should get error object' , ( ) => { fromInput . value = 'Graz' ; fromInput . dispatchEvent ( new Event ( 'input' ) ) ; expect ( rootFormGroup . control . hasError ( 'round-trip' ) ) . toBe ( false ) ; expect ( rootFormGroup . control . valid ) . toBe ( true ) ; expect ( rootFormGroup . control . errors ) . toBe ( null ) ; toInput . value = 'Graz' ; toInput . dispatchEvent ( new Event ( 'input' ) ) ; expect ( rootFormGroup . control . hasError ( 'round-trip' ) ) . toBe ( true ) ; expect ( rootFormGroup . control . valid ) . toBe ( false ) ; expect ( rootFormGroup . control . errors ) . toEqual ( { 'round-trip' : { city : 'Graz' } } ) ; } ) ; } ) ;