Create a mock object for mat-selection-list

Viewed 24

Am working on the angular test cases for mat-selection-list ,On the click event am calling onClick method and passing searchList as an argument.

<mat-selection-list #searchList class="search-selection-list">
      <mat-list-option class='child-list-dimension' checkboxPosition='before' *ngFor='let data of gridDimensionsData | filter:searchText'
                       (click)='onClicked(searchList)'>
        {{data}}
      </mat-list-option>
    </mat-selection-list>

onClicked(matSelectionList: MatSelectionList): void {
    this.selectedCount = matSelectionList.selectedOptions.selected.length;
  }

How can i pass matselectionList mock data as an argument to the onClick method?

This is the .spec file

describe('When onClicked method is called', () => {
    it('should set selectedCount', () => {
      spyOn(component, 'onClicked');
      const atSelectionList: MatSelectionList[] = [];
      component.onClicked(selectionList);

    });

  })

i made some changes on beforeEach but wont work .

Chnages 1:

selectionList = fixture.debugElement.query(By.directive(MatSelectionList));

Changes 2:

describe('When onClicked method is called', () => {
    it('should set selectedCount', async () => {
      spyOn(component, 'onClicked');
      const trigger = fixture.debugElement.query(By.css('.search-selection-list')).nativeElement;
      fixture.detectChanges();
      component.onClicked(trigger);
      await fixture.whenStable().then(() => {
      });
    });
  });

Thanks in advance. Expecting a better solution.

0 Answers
Related