Skip to content

Commit cfc9b75

Browse files
fix: null-check for getSettings (#6611)
1 parent 1b67412 commit cfc9b75

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

packages/react-liveness/src/components/FaceLivenessDetector/hooks/__tests__/useMediaStreamInVideo.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { renderHook } from '@testing-library/react';
33

44
import { useMediaStreamInVideo } from '../useMediaStreamInVideo';
5+
import { STATIC_VIDEO_CONSTRAINTS } from '../../utils/helpers';
56

67
describe('useMediaStreamInVideo', () => {
78
it('should return videoRef, videoHeight, videoWidth', () => {
@@ -32,4 +33,47 @@ describe('useMediaStreamInVideo', () => {
3233
expect(stream.removeTrack).toHaveBeenCalledWith(track);
3334
expect(track.stop).toHaveBeenCalled();
3435
});
36+
37+
it('should handle stream with no tracks', () => {
38+
const stream = {
39+
getTracks: () => [],
40+
removeTrack: jest.fn(),
41+
} as unknown as MediaStream;
42+
43+
const { result, unmount } = renderHook(() => useMediaStreamInVideo(stream));
44+
45+
const height = (STATIC_VIDEO_CONSTRAINTS.height as ConstrainULongRange)
46+
.ideal;
47+
const width = (STATIC_VIDEO_CONSTRAINTS.width as ConstrainULongRange).ideal;
48+
49+
expect(result.current.videoHeight).toBe(height);
50+
expect(result.current.videoWidth).toBe(width);
51+
52+
unmount();
53+
});
54+
55+
it('should handle stream track with no settings', () => {
56+
const track = {
57+
getSettings: () => undefined,
58+
stop: jest.fn(),
59+
} as unknown as MediaStreamTrack;
60+
61+
const stream = {
62+
getTracks: () => [track],
63+
removeTrack: jest.fn(),
64+
} as unknown as MediaStream;
65+
66+
const { result, unmount } = renderHook(() => useMediaStreamInVideo(stream));
67+
68+
const height = (STATIC_VIDEO_CONSTRAINTS.height as ConstrainULongRange)
69+
.ideal;
70+
const width = (STATIC_VIDEO_CONSTRAINTS.width as ConstrainULongRange).ideal;
71+
72+
expect(result.current.videoHeight).toBe(height);
73+
expect(result.current.videoWidth).toBe(width);
74+
75+
unmount();
76+
expect(stream.removeTrack).toHaveBeenCalledWith(track);
77+
expect(track.stop).toHaveBeenCalled();
78+
});
3579
});

packages/react-liveness/src/components/FaceLivenessDetector/hooks/useMediaStreamInVideo.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ export function useMediaStreamInVideo(
2525
if (isObject(videoRef.current)) {
2626
videoRef.current.srcObject = stream;
2727
}
28-
const { height: streamHeight, width: streamWidth } = stream
29-
.getTracks()[0]
30-
.getSettings();
3128

32-
setVideoHeight(streamHeight);
33-
setVideoWidth(streamWidth);
29+
const settings = stream.getTracks()?.[0]?.getSettings();
30+
if (settings) {
31+
setVideoHeight(settings.height);
32+
setVideoWidth(settings.width);
33+
}
3434
}
3535

3636
return () => {

0 commit comments

Comments
 (0)