[email protected]
[email protected]
[email protected]
When writing unit tests for components that are OnPush it's common to call the ngOnChanges life-cycle hook. Unlike ngOnInit the ngOnChanges life-cycle hook is not called for a component under test: https://github.com/angular/angular/issues/9866
describe('MyComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule,
MyComponentModule
]
}).compileComponents();
}));
it(`should NOT render a 'SUBMIT' button in read-only mode`, fakeAsync(() => {
const fixture: ComponentFixture<MyComponent> = TestBed.createComponent(MyComponent);
const component: MyComponent = fixture.debugElement.componentInstance;
component.isReadOnly = true;
component.ngOnChanges({
isReadOnly: new SimpleChange(undefined, true, true)
});
fixture.detectChanges();
const compiled: HTMLElement = fixture.debugElement.nativeElement;
const submitButton: HTMLButtonElement =
compiled.querySelector<HTMLButtonElement >('[data-submit-button]');
expect(submitButton).toBeFalsy();
fixture.destroy();
}));
});
Since I am using Angular CLI I want to avoid having to add a spec-only tslint.json and npm script.
How can I prevent seeing the error my.component.spec.ts: Avoid explicit calls to life cycle hooks. for these kind of component unit test files?
You can add: // tslint:disable:no-life-cycle-call in the beginning of the file.