我正在使用{{3}}
进行测试这是我的反应形式:
this.createForm = this.formBuilder.group({
'Id': new FormControl({ value: 0, disabled: true }, [Validators.required]),
'Name': new FormControl('', { validators: [ValidationService.required, ValidationService.SubsystemMxLenght, ValidationService.SubsystemPatternMatch], updateOn: 'blur' }),
'UpdatedByName': new FormControl({ value: this.appUserName, disabled: true }, []),
'UpdatedDate': new FormControl({ value: '', disabled: true }, [])
});
根据图书馆的建议,我正在为表单字段设置值,如下所示:
component.input(component.getByTestId('form-create-name-field-1'), {
target: {
value: data.Name /* value not setting since we use updateOn value, so it's null */
}
});
component.input(component.getByTestId('form-create-name-field-2'), {
target: {
value: data.UpdatedByName
}
});
我无法使用Name
设置值,如果我从表单中删除了参数updateOn: 'blur'
-则会进行设置。如何解决这个问题?否则我的表格有什么问题?
有人帮我吗?
更新: 组件功能:
onCreateFormSubmit() {
this.ssCreated.emit(created);
}
同时测试onCreateFormSubmit
和ssCreated
-但ssCreated
无效:
规格文件:
test('Testing add Subsystem operation', async () => {
const data = {
Id: 2,
Name: 'subsystem2',
IsDeletePossible: true,
CreatedBy: '',
CreatedDate: new Date(),
UpdatedBy: '',
UpdatedDate: new Date(),
UpdatedByName: 'test value',
CreatedByName: ''
} as ModelSubSystem;
const ssCreated = jest.fn();
const component = await render(SubSystemComponent, {
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [
HttpClientTestingModule,
FormsModule,
ReactiveFormsModule,
StoreModule.forRoot({}, { runtimeChecks: { strictStateImmutability: true, strictActionImmutability: true } }),
StoreModule.forFeature('pfservice', reducer),
EffectsModule.forRoot([]),
EffectsModule.forFeature([EffectsSubSystem])
],
componentProperties: {
onCreateFormSubmit: jest.fn(),
ssCreated: {
emit: data
} as any,
}
});
const componentInstance = component.fixture.componentInstance;
/*
* Testing the form by DOM.
*/
const createButton = component.getByTestId('btn-addRow');
component.click(createButton);
component.fixture.detectChanges();
// status changes because of click on button
expect(componentInstance.crud.isCreate).toBeTruthy();
component.fixture.detectChanges();
// onclick submit should not called, becasue of empty input
component.input(component.getByTestId('form-create-name-field-0'), {
target: {
value: data.Id
}
});
component.input(component.getByTestId('form-create-name-field-1'), {
target: {
value: data.Name
}
});
component.blur(component.getByTestId('form-create-name-field-1'));
component.input(component.getByTestId('form-create-name-field-2'), {
target: {
value: data.UpdatedByName
}
});
const submit = component.getAllByTestId('form-create-btn')[0];
component.click(submit);
component.fixture.detectChanges();
const name = componentInstance.createForm.controls['Name'];
const updatedByName = componentInstance.createForm.controls['UpdatedByName'];
expect(name.value).toEqual(data.Name);
expect(name.errors).toBeFalsy();
expect(updatedByName.value).toEqual(data.UpdatedByName);
expect(componentInstance.onCreateFormSubmit).toBeCalled(); //works
// console.log(componentInstance.createForm.getRawValue());
console.log(data);
expect(ssCreated).toHaveBeenCalledWith(data); //not works
});
答案 0 :(得分:1)
updateOne
仅在输入模糊时才会触发,这就是为什么您必须这样做:
component.input(component.getByTestId('form-create-name-field-1'), {
target: {
value: data.Name
}
});
// this line here
component.blur(component.getByTestId('form-create-name-field-1'));
创建了一个示例