我想通过在输入字段中设置值然后检查相关的实例成员来编写与Angular 2 / Ionic 2离子输入字段交互的单元测试。
具体来说,我想:
我有一个正常的HTML输入字段工作,但有一些关于使用我理解的离子输入字段。
我的单元测试和测试组件:
/**
* Form Tests
*/
import {Component} from '@angular/core';
import { ComponentFixture, TestBed, async, fakeAsync, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import {
AbstractControl,
FormControl,
FormGroup,
FormsModule,
ReactiveFormsModule
} from '@angular/forms';
import {dispatchEvent} from '@angular/platform-browser/testing/browser_util';
// Ionic imports
import {
App,
MenuController,
NavController,
Platform,
Config,
Keyboard,
Form,
IonicModule
} from 'ionic-angular';
describe( 'Ionic Form Tests',
() => {
// -----------------------------------
/**
* instance and element in a FormControl should match, right?
*/
it( 'nativeElement and instance should match with input and ion-input in a FormGroup',
fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [
IonicFormTestComponent
],
providers: [
App,
Platform,
Form,
{ provide: Config, useClass: ConfigMock },
],
imports: [
FormsModule,
IonicModule,
ReactiveFormsModule,
],
});
let fixture: any = TestBed.createComponent( IonicFormTestComponent );
fixture.whenStable().then(() => {
fixture.detectChanges();
tick();
let instance = fixture.componentInstance;
// first check that the initial plain input value and element match
let plainInputEl = fixture.debugElement.query( By.css( '[formControlName="plainInputControl"]' ) ).nativeElement;
expect( instance.plainInputControl.value ).toEqual( 'plain input control value' );
expect( plainInputEl.value ).toEqual( 'plain input control value' );
// now check to see if the model updates when we update the DOM element
plainInputEl.value = 'updated Plain Input Control Value';
dispatchEvent( plainInputEl, 'input' );
fixture.detectChanges();
tick();
// this works
expect( instance.plainInputControl.value ).toEqual( 'updated Plain Input Control Value' );
// -------------------------------------------------------------
// repeat with ion-input
let ionicInputEl = fixture.debugElement.query( By.css( '[formControlName="ionicInputControl"]' ) ).nativeElement;
expect( instance.ionicInputControl.value ).toEqual( 'ionic input control value' );
// this fails with ionicInputEl.value 'undefined'
// (how to correctly get the value of the ion-input element?)
expect( ionicInputEl.value ).toEqual( 'ionic input control value' );
ionicInputEl.value = 'updated Ionic Input Control Value';
dispatchEvent( ionicInputEl, 'input' );
fixture.detectChanges()
tick();
console.log( "Ionic input element value is:", ionicInputEl.value );
// this fails, instance.ionicInputControl.value not changed.
expect( instance.ionicInputControl.value ).toEqual( 'updated Ionic Input Control Value' );
});
}) // end of fakeAsync()
); // end of it()
}
); // end of describe()
// -------------------------------------------------
/**
* ionic test component with form Group
*/
@Component({
selector: 'ionic-form-test-component',
template: `
<form [formGroup]="testFormGroup">
<input type="text" value="" formControlName="plainInputControl" />
<ion-input type="text" value="" formControlName="ionicInputControl"></ion-input>
</form>
`
})
export class IonicFormTestComponent {
testFormGroup: FormGroup;
plainInputControl: AbstractControl;
ionicInputControl: AbstractControl;
constructor() {
this.testFormGroup = new FormGroup({
'plainInputControl': new FormControl( '' ),
'ionicInputControl': new FormControl( '' )
});
this.plainInputControl = this.testFormGroup.controls[ 'plainInputControl' ];
this.plainInputControl.setValue( 'plain input control value' );
this.ionicInputControl = this.testFormGroup.controls[ 'ionicInputControl' ];
this.ionicInputControl.setValue( 'ionic input control value' );
}
}
// --------------------------------------------------
export class ConfigMock {
public get(): any {
return '';
}
public getBoolean(): boolean {
return true;
}
public getNumber(): number {
return 1;
}
}
// END
如何在单元测试中以编程方式获取/设置离子输入字段的值,以便上述方法有效?
显然,我错过了一些东西。 Ionic 2文档在这个主题上非常沉默。 FormControl上的Angular 2文档似乎暗示它自动支持双向绑定(上面的成功单元测试似乎支持该断言。)答案 0 :(得分:3)
经过多次反复试验后,事实证明,为了让Ionic 2识别离子输入字段值已经改变,离子输入的第一个子元素的值,即输入字段,必须设置然后必须在该元素上触发“输入”事件。
sqoop import-all-tables \
--num-mappers 1 \
--connect "jdbc:mysql://localhost:3306/retail_db" \
--username=root \
--password=root \
--hive-import \
--hive-overwrite \
--hive-database sqoop_import \
--create-hive-table
进入离子输入区域的内部结构很难看,所以我提出了一个问题:https://github.com/driftyco/ionic/issues/9622