Here the method I want to test. It´s the ngOnInit lifecicle
ngOnInit(): void {
this.editable.subscribe((data) => {
this.enableEdition = data.enabled;
});
}
Here my test:
it('ngOnInit', () => {
const editableMock = of({ enabled: false });
component.editable = editableMock;
component.ngOnInit();
expect(component.enableEdition).toBe(false);
});
I am getting this error: TypeError: Cannot read property 'subscribe' of undefined
Maybe the problem is coming from the way the editable is sending to .ts from another component and the setTimeout where the first emision of the observable is located:
The subject which is the source of the observable is localted on table component which is a sibling of table-action component and the observable is shared from table to table-action this way:
Parent template:
<table-actions
[editable]="_editableState">
</table-actions>
<table>
</table>
parent get the subject from table component using a viewChild and then share it to table-action using an input
Parent.ts
@ViewChild('table', { static: true })
murTable!: MurTableComponent;
public _editableState!: Observable<any>;
this._editableState = this.murTable.stateEditing.asObservable();
First emision of the subject is located within a setTimeout(). Maybe this produce an asynchrony error
table.ts
ngOnInit(): void {
setTimeout(() => {
this.onFirstDataRendered(null);
this.stateEditing.next({ enabled: !!this.editType });
}, 1500);
}