1

I am trying to write a unit test for one of the dropdown with a search box at its top associated with it. The problem is that valueChanges is not getting fired and I am unable to see the filteredValues to put an assert on. I see the searchbox being updated with value 'S2' but don't see valueChanges from component getting called. Any help on what am I doing wrong here is much appreciated.

Unit Test :

it('should search for stores', fakeAsync(() => {
        initTest('add');
        fixture.detectChanges();
        fixture.nativeElement.querySelector('#select').click();
        fixture.detectChanges();
        const d = overlayContainer.getContainerElement().querySelector('#search');
        d.textContent = 'S2';
        d.dispatchEvent(new Event('input'));
        flush();
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            component.filteredValues.subscribe(value => {
                console.log('Value :: ' + JSON.stringify(value));
            });
        });
    }));

Inside component :

this.search.valueChanges.pipe(takeUntil(this._destroyed)).subscribe(searchText => {
            this.filteredValues = of(this.filterValue(searchText));
        });
a p
  • 1,121
  • 6
  • 26
  • 51

1 Answers1

0

you could use the reference to the <ngx-mat-select-search> component in your test as follows:

component.matSelectSearch.onInputChange('c');

https://github.com/bithost-gmbh/ngx-mat-select-search/blob/d19c777ded98457cbdd4be476246ae96c52d7f02/src/app/mat-select-search/mat-select-search.component.spec.ts#L348-L349

alternatively you would need to trigger an event with the target correctly set to the input field and set the d.value = 'my search value', see also https://github.com/bithost-gmbh/ngx-mat-select-search/blob/d19c777ded98457cbdd4be476246ae96c52d7f02/src/app/mat-select-search/mat-select-search.component.html#L25

Esteban Gehring
  • 821
  • 8
  • 8