尝试这样做,您可以给tr元素一个类名,并在提交后对ngfor元素进行计数,检查它是否不等于0,元素长度是否从0传递到1,并向表单“id='formid”添加一个ID。
<form id="FormId" name="editForm" role="form" novalidate (ngSubmit)="save()" #editForm="ngForm">
<tr class="patient" *ngFor="let paciente of pacientes ;trackBy: trackId">
这里是测试,新元素插入到末尾,所以它的索引是length-1,您可以检查它的值是否等于您的输入值。
it('should submit the form and save data', async(() => {
let formElement: DebugElement;
formElement = fixture.debugElement.query(By.css('#FormId'));
formElement .triggerEventHandler('ngSubmit', null);
expect(component.save).toHaveBeenCalledTimes(1); // check that save is called on sybmit
const tableLength = fixture.debugElement.queryAll(By.css('.patient')).length;
expect(tableLength ).toBe(1) // check that the element is added and length pass from 0 to 1
expect(fixture.debugElement.queryAll(By.css('.patient'))[tableLength-1].nativeElement.value).toEqual(component.paciente.nombre)
}))