Angular Jasmine FormControl Unit Test valueChanges

Discussão em 'Angular' iniciado por user5155835, Outubro 31, 2024.

  1. user5155835

    user5155835 Guest

    I want to unit test my following method:

    this.boxValue = '';

    subscribeToFilterChanges(): void {
    this.filterBox.valueChanges
    .subscribe(
    data => {
    if (data) {
    this.boxValue = data.trim().toLowerCase();
    }
    }
    );
    }


    filterBox is a FormControl:

    filterBox = new FormControl('');


    HTML is:

    <mat-form-field appearance="standard">
    <input matInput [formControl]="filterBox"
    id="filterBox-input">
    </mat-form-field>


    I've written the unit test as:

    it('verify filter changes', () => {
    let filterBoxInput = fixture.debugElement.query(By.css('#filterBox-input'));
    filterBoxInput.nativeElement.value = 'dummy';
    filterBoxInput.nativeElement.dispatchEvent(new Event('input'));
    fixture.detectChanges();
    fixture.whenStable().then(() => {
    expect(component.boxValue).toBe('dummy1');
    });
    });


    This test should fail, but still it is showing as passed, even though incorrect value is specified in .toBe()

    What could be the issue?

    I referred to Angular Testing: FormControl valueChanges Observable

    Continue reading...

Compartilhe esta Página