I have an Angular project where I use the Clarity Design UI framework. My project uses Clarity's DataGrid component and listens to its clrDgRefresh
EventEmitter
which triggers in response to changes in the underlying data or user interactions. I'm writing tests for a component that involves changes that trigger this EventEmitter
and I'm trying to figure out how to stub the callback on these events so that they are not triggered during testing. How can I do this?
My component template contains
<clr-datagrid (clrDgRefresh)="dgRefresh($event)">
where the @Output
on Clarity's DataGrid component is
@Output('clrDgRefresh') public refresh = new EventEmitter<ClrDatagridStateInterface<T>>(false);
and my component has something like
@Debounce(400)
dgRefresh(event: any) {
this.doSomething();
}
where I want to stub doSomething
in tests (or just prevent it from being called).
My spec file has:
TestBed.configureTestingModule({
imports: [
ClarityModule,
...
],
...
})
.compileComponents();
I can't just spyOn(component, 'doSomething')
since it only stubs direct calls to the function and not those triggered asynchronously through Clarity. I also can't do something like this since I'm using the entire module and not just a single component.
I also tried to get the instance of the DataGrid component that is created within my component and stub that with
let datagridDebugElement = fixture.debugElement.query(By.css('.datagrid'));
let datagrid = datagridDebugElement.componentInstance;
spyOn(datagrid, 'refresh');
But that also doesn't work (doSomething
is still called). What's the right way to approach this?