import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { TextMenuItemComponent } from './text-menu-item.component'; import { TranslateModule } from '@ngx-translate/core'; import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core'; import { By } from '@angular/platform-browser'; import { OnClickMenuItemComponent } from './onclick-menu-item.component'; import { OnClickMenuItemModel } from './models/onclick.model'; describe('OnClickMenuItemComponent', () => { let component: OnClickMenuItemComponent; let fixture: ComponentFixture; let debugElement: DebugElement; const text = 'HELLO'; const func = () => { }; const item = Object.assign(new OnClickMenuItemModel(), { text, function: func }); beforeEach(async(() => { TestBed.configureTestingModule({ imports: [TranslateModule.forRoot()], declarations: [OnClickMenuItemComponent], providers: [ { provide: 'itemModelProvider', useValue: item }, ], schemas: [NO_ERRORS_SCHEMA] }) .compileComponents(); })); beforeEach(() => { spyOn(item, 'function'); fixture = TestBed.createComponent(OnClickMenuItemComponent); component = fixture.componentInstance; debugElement = fixture.debugElement; fixture.detectChanges(); }); it('should contain the correct text', () => { expect(component).toBeTruthy(); }); it('should contain the text element', () => { const textContent = debugElement.query(By.css('a')).nativeElement.textContent; expect(textContent).toEqual(text); }); it('should contain call the function on the item when clicked', () => { debugElement.query(By.css('a.nav-link')).triggerEventHandler('click', {}); expect(item.function).toHaveBeenCalled(); }); });