I am having trouble testing a component with OnPush
change detection strategy.
The test goes like this
it('should show edit button for featured only for owners', () => {
let selector = '.edit-button';
component.isOwner = false;
fixture.detectChanges();
expect(fixture.debugElement.query(By.css(selector))).toBeFalsy();
component.isOwner = true;
fixture.detectChanges();
expect(fixture.debugElement.query(By.css(selector))).toBeTruthy();
});
If I use Default
strategy it works as expected, but with OnPush
the change to isOwner
is not rerendered by the call to detectChanges
. Am I missing something?
If you check out this great @Günter's answer angular 2 change detection and ChangeDetectionStrategy.OnPush then you can work around it by using event handler like:
const fixture = TestBed.overrideComponent(TestComponent, {set: {host: { "(click)": "dummy" }}}).createComponent(TestComponent);
// write your test
fixture.debugElement.triggerEventHandler('click', null);
fixture.detectChanges();
Here's Plunker Example