1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import React from 'react' ;
5
+ import { renderHook , act } from '@testing-library/react' ;
6
+ import {
7
+ ContentShareProvider ,
8
+ useContentShareControls ,
9
+ } from '../../../src/providers/ContentShareProvider' ;
10
+
11
+ // Mock audioVideo object
12
+ const mockAudioVideo = {
13
+ startContentShareFromScreenCapture : jest . fn ( ) ,
14
+ startContentShare : jest . fn ( ) ,
15
+ stopContentShare : jest . fn ( ) ,
16
+ addObserver : jest . fn ( ) ,
17
+ removeObserver : jest . fn ( ) ,
18
+ addContentShareObserver : jest . fn ( ) ,
19
+ removeContentShareObserver : jest . fn ( )
20
+ } ;
21
+ // Mock the module containing useAudioVideo hook
22
+ jest . mock ( '../../../src/providers/AudioVideoProvider' , ( ) => ( {
23
+ useAudioVideo : ( ) => mockAudioVideo
24
+ } ) ) ;
25
+
26
+ describe ( 'toggleContentShare' , ( ) => {
27
+ it ( 'Should call startContentShareFromScreenCapture' , async ( ) => {
28
+ const { result } = renderHook ( ( ) => useContentShareControls ( ) , {
29
+ wrapper : ( { children } ) => (
30
+ < ContentShareProvider > { children } </ ContentShareProvider >
31
+ ) ,
32
+ } ) ;
33
+ await act ( async ( ) => {
34
+ await result . current . toggleContentShare ( ) ;
35
+ } ) ;
36
+ expect ( mockAudioVideo . startContentShareFromScreenCapture ) . toHaveBeenCalledTimes ( 1 ) ;
37
+ } ) ;
38
+
39
+ it ( 'Can catch error from startContentShareFromScreenCapture' , async ( ) => {
40
+ mockAudioVideo . startContentShareFromScreenCapture = jest . fn ( ( ) => Promise . reject ( new Error ( 'startContentShare error' ) ) ) ;
41
+ const { result } = renderHook ( ( ) => useContentShareControls ( ) , {
42
+ wrapper : ( { children } ) => (
43
+ < ContentShareProvider > { children } </ ContentShareProvider >
44
+ ) ,
45
+ } ) ;
46
+ await expect ( async ( ) => {
47
+ await act ( async ( ) => {
48
+ await result . current . toggleContentShare ( ) ;
49
+ } ) ;
50
+ } ) . rejects . toThrow ( 'startContentShare error' ) ;
51
+ // expect(() => {
52
+ // result.current.toggleContentShare();
53
+ // }).toThrow('startContentShare error');
54
+ } ) ;
55
+ } ) ;
0 commit comments