vendredi 19 mars 2021

Jest test fails form.get(...).value.getFullYear is not a function

Component is defined as:

  signup() {
    if (this.form.valid) {
      this.signupService.signup(
        this.form.get('email').value,
        this.form.get('document').value,
        this.form.get('type').value,
        this.form.get('birthday').value.getFullYear()
      ).subscribe(() => {
        this.router.navigate(['signup/thanks']);
      });
    }
  }

Here is my testing part.

test('should call signup when its valid form', () => {
      const mockGetFullYear = jest.fn(() => '2021');
      (global.Date as any) = jest.fn(() => ({ getFullYear: mockGetFullYear }));
      jest.spyOn(routerMock, 'navigate');
      signupServiceMock.signup.mockReturnValue(of(null));

      const type = 'NIF';
      const document = '12345678A';
      const birthday = '01-01-2021';
      const email = 'test001@hotmail.com';
      const repeatEmail = 'test001@hotmail.com';
      const checkbox = true;

      signupFormComponent.ngOnInit();
      signupFormComponent.form.setValue({
        type,
        document,
        birthday,
        email,
        repeatEmail,
        checkbox
      });

      signupFormComponent.signup();

      expect(signupServiceMock.signup).toHaveBeenCalledWith(email, document, type, birthday);
      expect(routerMock.navigate).toHaveBeenCalledWith(['signup/thanks']);
    });

Any ideas? I've tried many things but still don't go, the last one I tried was mockGetFullYear

Here is the error

TypeError: this.form.get(...).value.getFullYear is not a function

Thanks by the way. ;)

Aucun commentaire:

Enregistrer un commentaire