Angular 6 - Unit Testing Mat-Select

5,098 views
Skip to first unread message

Knight

unread,
Sep 26, 2018, 12:40:28 AM9/26/18
to Angular and AngularJS discussion

1: The mat-select has 4 values, 1,2,3,4.


The code below works good for the select. So I'd like to share if it helps the readers.


it('check the length of drop down', async () => {

    const trigger = fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
    trigger.click();
    fixture.detectChanges();
    await fixture.whenStable().then(() => {
        const inquiryOptions = fixture.debugElement.queryAll(By.css('.mat-option-text'));
        expect(inquiryOptions.length).toEqual(4);
    });
});


2: I need another test to verify the default value in the same mat-select is 3 or not. When page loads the default value for the drop down is set to 3.


it('should validate the drop down value if it is set by default', async () => {

    const trigger = fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
    trigger.click();
    fixture.detectChanges();
    await fixture.whenStable().then(() => {
        const inquiryOptions = fixture.debugElement.queryAll(By.css('.mat-option-text'));
        const value = trigger.options[0].value;
        expect(value).toContain(3);
    });
});

Any help is appreciated.

Knight

unread,
Sep 26, 2018, 12:40:28 AM9/26/18
to Angular and AngularJS discussion
Reply all
Reply to author
Forward
0 new messages