I have declared a component like this
export class Component {
private selectedFieldType: string;
private enableAddCheck: boolean = false;
@Input('itemX') item;
}
and i have a html for two binding like this
Field : <input [(ngModel)]="item.fieldLabel" type="text" class="input-bars">
So i have created the unit testing code like this to check the two way binding like this
beforeEach(async(() => {
// refine the test module by declaring the test component
TestBed.configureTestingModule({
imports: [ FormsModule ],
declarations: [Component , DND_DIRECTIVES],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
providers: [
{ provide: ComponentFixtureAutoDetect, useValue: true },
DND_PROVIDERS ]
})
// create component and test fixture
fixture = TestBed.createComponent(Component );
// get test component from the fixture
comp = fixture.componentInstance;
}));
it('To check fieldLabel to way binding from inputbox to data', () => {
comp.item = {
fieldLabel: 'text'
};
comp.ngOnInit();
fixture.detectChanges();
let fieldTypeInput: HTMLInputElement;
const input = fixture.debugElement.query(By.css('input'));
fieldTypeInput = input[0].nativeElement;
fieldTypeInput.value = 'field';
fixture.detectChanges();
expect(comp.item.fieldLabel).toBe('field');
});
But it's giving 'fieldLabel' undefined error for me.
How to pass the data to @input in my component through the unit test??