Skip to content

Instantly share code, notes, and snippets.

@jeroenheijmans
Created February 16, 2020 14:00
Show Gist options
  • Save jeroenheijmans/3b6228735ada4e272c8dea438399391b to your computer and use it in GitHub Desktop.
Save jeroenheijmans/3b6228735ada4e272c8dea438399391b to your computer and use it in GitHub Desktop.
Destroyable Angular component
import { Subject } from 'rxjs';
import { Destroyable } from './destroyable.component';
class TestableDestroyable extends Destroyable { }
describe('Destroyable', () => {
it('should construct with observable', () => {
const component = new TestableDestroyable();
expect(component.destroyed$ instanceof Subject).toBeTruthy();
});
it('should emit destroyed observable OnDestroy', done => {
const component = new TestableDestroyable();
component.destroyed$.subscribe(() => done());
component.ngOnDestroy();
});
it('should emit destroyed observable OnDestroy', () => {
const component = new TestableDestroyable();
component.destroyed$.subscribe(() => { });
component.ngOnDestroy();
expect(component.destroyed$.isStopped).toBeTruthy();
});
});
import { OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
export abstract class Destroyable implements OnDestroy {
destroyed$ = new Subject();
ngOnDestroy(): void {
this.destroyed$.next();
this.destroyed$.complete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment