'Angular test mat menu triggered click and count items
I am trying to test mat menu items length, but it seems there aren't any items at all (buttons length is 0) after click. Maybe the problem is in the click itself
const fixture = TestBed.createComponent(MatComponent);
const trigger = fixture.debugElement.query(By.css('.mat-menu-trigger'));
trigger.triggerEventHandler('click', {});
const buttons = document.querySelectorAll('.mat-menu-item');
expect(buttons.length).toEqual(2);
Solution 1:[1]
The beforeEach needs to be as follows:
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | eladr |
