Skip to content

Instantly share code, notes, and snippets.

@ali-kamalizade
Last active February 17, 2022 17:15
Show Gist options
  • Save ali-kamalizade/14f7f0ab19f6592adf2f05cd6215dabf to your computer and use it in GitHub Desktop.
Save ali-kamalizade/14f7f0ab19f6592adf2f05cd6215dabf to your computer and use it in GitHub Desktop.
A function which can be used in Angular component tests to trigger change detection for components using OnPush strategy.
/**
* Changes in components using OnPush strategy are only applied once when calling .detectChanges(),
* This function solves this issue.
*/
export async function runOnPushChangeDetection(fixture: ComponentFixture<any>): Promise<void> {
const changeDetectorRef = fixture.debugElement.injector.get<ChangeDetectorRef>(ChangeDetectorRef);
changeDetectorRef.detectChanges();
return fixture.whenStable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment