gpt4 book ai didi

javascript - 麻烦单元测试 Angular react 性表单字段

转载 作者:行者123 更新时间:2023-12-01 14:55:11 24 4
gpt4 key购买 nike

我正在使用 @angular/cli 1.0.0-beta.30 学习 Angular 2 和单元测试,并且在测试表单字段有效性的一个方面取得了一些成功,但不是全部。我暂时在我的组件中使用内联模板来消除一层复杂性(单独文件中的表单模板引入了异步性,对吗?)。
ngOnInit()定义了 name包含“required”和“minLength”验证器的属性。目前,一个空的表单域将正确触发“required”验证器,但不会触发“minLength”验证器。 name.errors测试中的数组根本不包含任何对 required 的引用,name.errors['minLength']返回 undefined . minLength 是否需要异步处理?我无法找到适合我的问题的文档或示例。

// signup-form.component.ts
...
export class SignupFormComponent implements OnInit {

user: FormGroup;

constructor(private fb: FormBuilder) {
}

ngOnInit() {
this.user = this.fb.group({
name: ['', [Validators.required, Validators.minLength(2)]],
account: this.fb.group({
email: ['', Validators.required, Validators.pattern("[^ @]*@[^ @]*")],
confirm: ['', Validators.required]
})
})
}

onSubmit({ value, valid }: { value: User, valid: boolean }) {
console.log(value, valid);
}

}

我的测试
// signup-form.component.spec.ts
import { SignupFormComponent } from './signup-form.component';

describe('SignupFormComponent', () => {
let component: SignupFormComponent;
let fixture: ComponentFixture<SignupFormComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [SignupFormComponent],
imports: [
ReactiveFormsModule,
FormsModule
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(SignupFormComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('form invalid when empty', () => {
expect(component.user.valid).toBeFalsy();
});

it('name field validity', () => {
let name = component.user.controls['name'];
expect(name.valid).toBeFalsy();

let errors = {};
name.setValue("");
errors = name.errors || {};
expect(errors['required']).toBeTruthy(); // this works
expect(errors['minLength']).toBeTruthy(); // this fails, "undefined"
});

});

最佳答案

回答你的问题

The name.errors['minLength'] returns undefined, Does minLength need to be handled asynchronously?



您可以只检查表格中的特定错误
expect(form.control.hasError('emailInvalid', ['email'])).toBe(true);

下面是一个完整的测试
// signup-form.component.spec.ts
import { SignupFormComponent } from './signup-form.component';

describe('SignupFormComponent', () => {
let component: SignupFormComponent;
let fixture: ComponentFixture<SignupFormComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [SignupFormComponent],
imports: [
ReactiveFormsModule,
FormsModule
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(SignupFormComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('form invalid when empty', () => {
expect(component.user.valid).toBeFalsy();
});

it('name field validity', () => {
let name = component.user.controls['name'];
expect(name.valid).toBeFalsy();

name.setValue("");
expect(name.hasError('required')).toBeTruthy();

name.setValue("A");
expect(name.hasError('minLength')).toBeTruthy();
});

});

关于javascript - 麻烦单元测试 Angular react 性表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42143815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com