@@ -32,6 +32,13 @@ describe('useUserMedia hook', () => {
32
32
33
33
beforeEach ( ( ) => {
34
34
gumMock = mockGetUserMedia ( ) ;
35
+ Object . defineProperty ( global . navigator , 'permissions' , {
36
+ value : {
37
+ query : jest . fn ( ( ) => Promise . reject ( ) ) ,
38
+ } ,
39
+ configurable : true ,
40
+ writable : true ,
41
+ } ) ;
35
42
} ) ;
36
43
37
44
afterEach ( ( ) => {
@@ -125,6 +132,56 @@ describe('useUserMedia hook', () => {
125
132
unmount ( ) ;
126
133
} ) ;
127
134
135
+ it ( 'should return a NotAllowed for webpage error in case of camera permission error' , async ( ) => {
136
+ const videoRef = { current : { } } as RefObject < HTMLVideoElement > ;
137
+ const nativeError = new Error ( ) ;
138
+ nativeError . name = 'NotAllowedError' ;
139
+ mockGetUserMedia ( { createMock : ( ) => jest . fn ( ( ) => Promise . reject ( nativeError ) ) } ) ;
140
+ navigator . permissions . query = jest . fn ( ( ) =>
141
+ Promise . resolve ( { state : 'granted' } as PermissionStatus ) ,
142
+ ) ;
143
+ const { result } = renderUseUserMedia ( { constraints : { } , videoRef } ) ;
144
+ await waitFor ( ( ) => {
145
+ expect ( result . current ) . toEqual ( {
146
+ stream : null ,
147
+ dimensions : null ,
148
+ error : {
149
+ type : UserMediaErrorType . NOT_ALLOWED_BROWSER ,
150
+ nativeError,
151
+ } ,
152
+ isLoading : false ,
153
+ retry : expect . any ( Function ) ,
154
+ availableCameraDevices : [ ] ,
155
+ selectedCameraDeviceId : null ,
156
+ } ) ;
157
+ } ) ;
158
+ } ) ;
159
+
160
+ it ( 'should return a NotAllowed for browser error in case of camera permission error' , async ( ) => {
161
+ const videoRef = { current : { } } as RefObject < HTMLVideoElement > ;
162
+ const nativeError = new Error ( ) ;
163
+ nativeError . name = 'NotAllowedError' ;
164
+ mockGetUserMedia ( { createMock : ( ) => jest . fn ( ( ) => Promise . reject ( nativeError ) ) } ) ;
165
+ navigator . permissions . query = jest . fn ( ( ) =>
166
+ Promise . resolve ( { state : 'denied' } as PermissionStatus ) ,
167
+ ) ;
168
+ const { result } = renderUseUserMedia ( { constraints : { } , videoRef } ) ;
169
+ await waitFor ( ( ) => {
170
+ expect ( result . current ) . toEqual ( {
171
+ stream : null ,
172
+ dimensions : null ,
173
+ error : {
174
+ type : UserMediaErrorType . NOT_ALLOWED_WEBPAGE ,
175
+ nativeError,
176
+ } ,
177
+ isLoading : false ,
178
+ retry : expect . any ( Function ) ,
179
+ availableCameraDevices : [ ] ,
180
+ selectedCameraDeviceId : null ,
181
+ } ) ;
182
+ } ) ;
183
+ } ) ;
184
+
128
185
it ( 'should return an InvalidStream error if the stream has no tracks' , async ( ) => {
129
186
const videoRef = { current : { } } as RefObject < HTMLVideoElement > ;
130
187
mockGetUserMedia ( { tracks : [ ] } ) ;
0 commit comments