Skip to content

Commit

Permalink
* implementing turning off camera (setting deviceId to null; showWebc…
Browse files Browse the repository at this point in the history
…am to false; and most importantly releasing resources by calling nextWebcam.complete() on a subject)

* adding unit test
  • Loading branch information
kamilhawdziejukhidglobal committed Sep 20, 2022
1 parent db71daa commit f5b2372
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));

});
9 changes: 9 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f5b2372

Please sign in to comment.