Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
Testing Reactive Forms in Angular with Karma

I am testing a function that has a reference to a form in it (this.form), so when I try to test it I am wondering how I can mock that form so the function has access to it.

Here is the function I am testing:

 export function isInvalid(control: string) {
      const _control = this.form?.controls[control];
      if (!_control) {
        console.log('isInvalid - no control', control)
      }
    
      const _touched = _control?.touched;
      const _invalid = _control?.invalid;
      return _touched && _invalid;
    }

Here is the test for the isInvalid function. I created a mock Form but the problem is that the reference to the form (this.form) is inside the function so it can't access the mock form. Any ideas of how I can get around this? Thanks.

   it('should check isInvalid - return true', () => {
        
        const form: FormGroup = new FormGroup({
          name: new FormControl(''),
        });
    
        const control = "name";
        expect(isInvalid(control)).toEqual(-1);
      });
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda