Created
February 16, 2020 14:00
-
-
Save jeroenheijmans/3b6228735ada4e272c8dea438399391b to your computer and use it in GitHub Desktop.
Destroyable Angular component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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