hasValidator "@angular/forms": "12.2.5" not working
See original GitHub issueWhich @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
in unit test, I want to check validators in form control
this.userForm = this.formBuilder.group({
orgId: null,
name: [null, [Validators.required, Validators.minLength(3), Validators.maxLength(30)],
[specialCharAsyncValidator(this.infoService)]
],
....
get orgIdCtrl(): AbstractControl { return this.userForm.get('orgId'); }
get nameCtrl(): AbstractControl { return this.userForm.get('name'); }
it('form control check for validators', () => {
expect(component.orgIdCtrl.hasValidator(Validators.required)).toBeFalse();
const nameCtrl = component.nameCtrl;
expect(nameCtrl.hasValidator(Validators.required)).toBeTruthy(); // OK
expect(nameCtrl.hasValidator(Validators.minLength(3))).toBeTruthy(); // it is false, so NOT OK
expect(nameCtrl.hasValidator(Validators.maxLength(30))).toBeTruthy(); // it is false, so NOT OK
expect(nameCtrl.hasAsyncValidator(specialCharAsyncValidator(infoService))).toBeTruthy(); // it is false, so NOT OK
});
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
1) form control check for validators
testing AddComponent
Error: Expected false to be truthy.
at <Jasmine>
at UserContext.<anonymous> (src/app/components/user-add/add/add.component.spec.ts:86:60)
Please provide the environment you discovered this bug in
❯ ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 12.2.5
Node: 14.15.4
Package Manager: npm 6.14.10
OS: win32 x64
Angular: 12.2.5
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router, service-worker
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1202.5
@angular-devkit/build-angular 12.2.5
@angular-devkit/core 12.2.5
@angular-devkit/schematics 12.2.5
@schematics/angular 12.2.5
rxjs 7.3.0
typescript 4.3.5
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
What's new in Angular 12.2? - Ninja Squad
Forms. Forms are getting some love in this release, with the addition of the hasValidator , addValidators , and removeValidators methods.
Read more >Get validators present in FormGroup/FormControl
To work with Angular Material we had to add a little workaround as the 'required' attribute has to be set on the enclosing...
Read more >AbstractControl - Angular
An object containing any errors generated by failing validation, or null if there are no errors. pristine: boolean, Read-Only. A control is pristine...
Read more >@angular/forms - npm
Start using @angular/forms in your project by running `npm i @angular/forms`. There are 10630 other projects in the npm registry using ...
Read more >angular/forms@12.2.9 - runpkg
typeof define === 'function' && define.amd ? define('@angular/forms', ['exports', ... https://github.com/angular/angular/issues/3011 is implemented.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If I try to reproduce the issue (with a validator stored as in instance variable of the component), I can’t: the test passes as I expected. See https://github.com/jnizet/has-validator-repro.
@sysmat My test passes, so my guess is that yours fails for a different reason. But IMHO, this is not the best way to test your component anyway: testing that a control has a validator
min3
, which could be… any validator, doesn’t really test a requirement. IMHO, it would be best to treat the component as a black box and write the test as an end user would test it:This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.