diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 6879b0e..5695e62 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -15,15 +15,32 @@ describe('AppComponent', () => { ] }).compileComponents(); })); + it('should create the app', waitForAsync(() => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; expect(app).toBeTruthy(); })); + it('should render title in a h1 tag', waitForAsync(() => { const fixture = TestBed.createComponent(AppComponent); fixture.detectChanges(); const compiled = fixture.debugElement.nativeElement; expect(compiled.querySelector('h1').textContent).toContain('ngx-webcam Demo'); })); + + it('closing camera connection when turning off camera', waitForAsync(() => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.debugElement.componentInstance; + app.deviceId = '111'; + app.showWebcam = true; + const compiled = fixture.debugElement.nativeElement; + + app.turnOffCamera(); + + expect(app.deviceId).toBeNull(); + expect(app.showWebcam).toBeFalse(); + expect(compiled.querySelector('webcam')).toBeNull(); + })); + }); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index f500bb0..95950b2 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -36,6 +36,15 @@ export class AppComponent implements OnInit { public toggleWebcam(): void { this.showWebcam = !this.showWebcam; + if (!this.showWebcam) { + this.turnOffCamera(); + } + } + + public turnOffCamera(): void { + this.deviceId = null; + this.showWebcam = false; + this.nextWebcam.complete(); } public handleInitError(error: WebcamInitError): void {