@@ -149,11 +149,45 @@ describe('useUserMedia hook', () => {
149
149
unmount ( ) ;
150
150
} ) ;
151
151
152
- it ( 'should return a NotAllowed error in case of camera permission error ' , async ( ) => {
152
+ it ( 'should return a UserMediaErrorType.BROWSER_NOT_ALLOWED error in case of immediate rejection ' , async ( ) => {
153
153
const videoRef = { current : { } } as RefObject < HTMLVideoElement > ;
154
154
const nativeError = new Error ( ) ;
155
155
nativeError . name = 'NotAllowedError' ;
156
156
mockGetUserMedia ( { createMock : ( ) => jest . fn ( ( ) => Promise . reject ( nativeError ) ) } ) ;
157
+ navigator . permissions . query = jest . fn ( ( ) =>
158
+ Promise . resolve ( { state : 'denied' } as PermissionStatus ) ,
159
+ ) ;
160
+ const { result } = renderUseUserMedia ( { constraints : { } , videoRef } ) ;
161
+ await waitFor ( ( ) => {
162
+ expect ( result . current ) . toEqual ( {
163
+ getUserMedia : expect . any ( Function ) ,
164
+ stream : null ,
165
+ dimensions : null ,
166
+ error : {
167
+ type : UserMediaErrorType . BROWSER_NOT_ALLOWED ,
168
+ nativeError,
169
+ } ,
170
+ isLoading : false ,
171
+ retry : expect . any ( Function ) ,
172
+ availableCameraDevices : [ ] ,
173
+ selectedCameraDeviceId : null ,
174
+ } ) ;
175
+ } ) ;
176
+ } ) ;
177
+
178
+ it ( 'should return a UserMediaErrorType.NOT_ALLOWED error in case of camera permission error' , async ( ) => {
179
+ const videoRef = { current : { } } as RefObject < HTMLVideoElement > ;
180
+ const nativeError = new Error ( ) ;
181
+ nativeError . name = 'NotAllowedError' ;
182
+ mockGetUserMedia ( {
183
+ createMock : ( ) =>
184
+ jest . fn (
185
+ ( ) =>
186
+ new Promise ( ( _resolve , reject ) => {
187
+ setTimeout ( ( ) => reject ( nativeError ) , 150 ) ;
188
+ } ) ,
189
+ ) ,
190
+ } ) ;
157
191
const { result, unmount } = renderUseUserMedia ( { constraints : { } , videoRef } ) ;
158
192
await waitFor ( ( ) => {
159
193
expect ( result . current ) . toEqual ( {
@@ -173,13 +207,21 @@ describe('useUserMedia hook', () => {
173
207
unmount ( ) ;
174
208
} ) ;
175
209
176
- it ( 'should return a NotAllowed for webpage error in case of camera permission error' , async ( ) => {
210
+ it ( 'should return a UserMediaErrorType.WEBPAGE_NOT_ALLOWED for webpage error in case of camera permission error' , async ( ) => {
177
211
const videoRef = { current : { } } as RefObject < HTMLVideoElement > ;
178
212
const nativeError = new Error ( ) ;
179
213
nativeError . name = 'NotAllowedError' ;
180
- mockGetUserMedia ( { createMock : ( ) => jest . fn ( ( ) => Promise . reject ( nativeError ) ) } ) ;
214
+ mockGetUserMedia ( {
215
+ createMock : ( ) =>
216
+ jest . fn (
217
+ ( ) =>
218
+ new Promise ( ( _resolve , reject ) => {
219
+ setTimeout ( ( ) => reject ( nativeError ) , 150 ) ;
220
+ } ) ,
221
+ ) ,
222
+ } ) ;
181
223
navigator . permissions . query = jest . fn ( ( ) =>
182
- Promise . resolve ( { state : 'granted ' } as PermissionStatus ) ,
224
+ Promise . resolve ( { state : 'prompt ' } as PermissionStatus ) ,
183
225
) ;
184
226
const { result } = renderUseUserMedia ( { constraints : { } , videoRef } ) ;
185
227
await waitFor ( ( ) => {
@@ -188,7 +230,7 @@ describe('useUserMedia hook', () => {
188
230
stream : null ,
189
231
dimensions : null ,
190
232
error : {
191
- type : UserMediaErrorType . BROWSER_NOT_ALLOWED ,
233
+ type : UserMediaErrorType . WEBPAGE_NOT_ALLOWED ,
192
234
nativeError,
193
235
} ,
194
236
isLoading : false ,
@@ -199,11 +241,19 @@ describe('useUserMedia hook', () => {
199
241
} ) ;
200
242
} ) ;
201
243
202
- it ( 'should return a NotAllowed for browser error in case of camera permission error' , async ( ) => {
244
+ it ( 'should return a UserMediaErrorType.BROWSER_NOT_ALLOWED for browser error in case of camera permission error' , async ( ) => {
203
245
const videoRef = { current : { } } as RefObject < HTMLVideoElement > ;
204
246
const nativeError = new Error ( ) ;
205
247
nativeError . name = 'NotAllowedError' ;
206
- mockGetUserMedia ( { createMock : ( ) => jest . fn ( ( ) => Promise . reject ( nativeError ) ) } ) ;
248
+ mockGetUserMedia ( {
249
+ createMock : ( ) =>
250
+ jest . fn (
251
+ ( ) =>
252
+ new Promise ( ( _resolve , reject ) => {
253
+ setTimeout ( ( ) => reject ( nativeError ) , 150 ) ;
254
+ } ) ,
255
+ ) ,
256
+ } ) ;
207
257
navigator . permissions . query = jest . fn ( ( ) =>
208
258
Promise . resolve ( { state : 'denied' } as PermissionStatus ) ,
209
259
) ;
@@ -214,7 +264,7 @@ describe('useUserMedia hook', () => {
214
264
stream : null ,
215
265
dimensions : null ,
216
266
error : {
217
- type : UserMediaErrorType . WEBPAGE_NOT_ALLOWED ,
267
+ type : UserMediaErrorType . BROWSER_NOT_ALLOWED ,
218
268
nativeError,
219
269
} ,
220
270
isLoading : false ,
0 commit comments