I am utilizing an abstract class to prevent redundant code for unsubscribing observables. Here is what it looks like:
export abstract class SubscriptionManagmentDirective implements OnDestroy {
componetDestroyed = new Subject<void>()
constructor() {}
ngOnDestroy(): void {
this.componetDestroyed.next()
this.componetDestroyed.unsubscribe()
}
}
Occasionally, I forget to call super.ngOnDestroy()
when overriding ngOnDestroy
, causing the subscriptions to misbehave.
To ensure that un-subscriptions are properly called in extending components, I have added unit tests. However, remembering to add these tests can be a challenge.
Therefore, my query is,
- Is there a way to mandate the calling of
super.ngOnDestroy()
whenever the component is extended? - Or is there a method to create a unit test for the abstract class that would automatically be applied to any component that extends it?
Your assistance on this matter would be greatly appreciated. Thank you.