Angular Testing. Override component's service
component.ts
@Component({
selector: 'my-component',
template: '',
providers: [ComponentService], // <=== component's service
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyComponent implements OnInit, OnDestroy { ... }
angular error during cleanup of component
component.spec.ts
const windowMock = {
location: {
href: null
}
};
const ComponentServiceSpy = {
...jasmine.createSpyObj<ComponentService>('ComponentService', [
'init',
'orderDomains',
'destroy',
'ngOnDestroy' // <=== angular error during cleanup of component
]),
state$: of({
ok: {
preloader: false,
disabled: true,
},
cancel: {
disabled: false,
},
} as FormButtonsState),
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
ReactiveFormsModule,
RouterTestingModule,
MyTestModule,
],
providers: [
{ provide: ModalService, useValue: modalServiceSpy },
{ provide: WINDOW, useValue: windowMock }
],
declarations: [MyComponent, MyComponentChildrenMockComponent],
})
.overrideProvider(ComponentService, { // override
useValue: ComponentServiceSpy,
})
.compileComponents();
}));