在使用Jasmine和Angular进行单元测试时,无法获得选择按钮的点击。

问题描述 投票:0回答:1

嗨,我在这里使用Angular,试图为Reactive表单创建一个单元测试,但无法成功。

我需要的是:如何使用jasmine在angular中用false和truthhy值填写表单后执行按钮点击。

我做了什么:我创建了一个逻辑,但它不工作。

 it('Form check is valid or not if no values entered', () => {

    expect(component.userCreationForm.valid).toBeFalsy();
  });

  it('Form check is valid or not when values entered', () => {

    component.userCreationForm.controls['firstname'].setValue('luther');
    component.userCreationForm.controls['lastname'].setValue('adams');
    component.userCreationForm.controls['email'].setValue('[email protected]');
    component.userCreationForm.controls['password'].setValue('123456');
    expect(component.userCreationForm.valid).toBeTruthy();
  });

  it('Form Submitted should check from is submitted', () => {
    // check form is invalid
    expect(component.userCreationForm.invalid).toBeTruthy();
    let btn = fixture.debugElement.query(By.css('button[type=submit]'));
    // Check button is disabled
    expect(btn.nativeElement.disabled).toBeTruthy();
    component.userCreationForm.controls['firstname'].setValue('luther');
    component.userCreationForm.controls['lastname'].setValue('adams');
    component.userCreationForm.controls['email'].setValue('[email protected]');
    component.userCreationForm.controls['password'].setValue('testpassword');
    fixture.detectChanges();
    // check button is enabled
    expect(btn.nativeElement.disabled).toBeFalsy();

    component.onUserCreation();
    fixture.detectChanges();

    let success = fixture.debugElement.query(By.css('#success-msg')).nativeElement;
    expect(success.textContent).toBe('Bubba');

 });

逻各斯

<div class="inv-buttons">
          <button mat-raised-button color="primary" [disabled]="userCreationForm.invalid" type="submit">Create User</button>
        </div>

下面是我的stackblitz 。https:/stackblitz.comeditangular-9-material-starter-q9wxvq

javascript angular jasmine karma-jasmine
1个回答
1
投票

在任何地方你想点击你创建的btn。使用它像下面:-

 btn.nativeElement.click();

我改变了你的测试案例如下:-

it('Form Submitted should check from is submitted',async () => {
    // check form is invalid
    expect(component.userCreationForm.invalid).toBeTruthy();
    let btn = fixture.debugElement.query(By.css('button[type=submit]'));
    // Check button is disabled
    expect(btn.nativeElement.disabled).toBeTruthy();
    component.userCreationForm.controls['firstname'].setValue('luther');
    component.userCreationForm.controls['lastname'].setValue('adams');
    component.userCreationForm.controls['email'].setValue('[email protected]');
    component.userCreationForm.controls['password'].setValue('testpassword');
    fixture.detectChanges();
    // check button is enabled
    expect(btn.nativeElement.disabled).toBeFalsy();
    await fixture.whenStable();
    console.clear();
    btn.nativeElement.click();
    fixture.detectChanges();
    //component.onUserCreation();
    //fixture.detectChanges();

    //let success = fixture.debugElement.query(By.css('#success-msg')).nativeElement;
    //expect(success.textContent).toBe('Bubba');

 });
© www.soinside.com 2019 - 2024. All rights reserved.