Skip to content

Commit f86eb0d

Browse files
authored
Add await for toggleContentShare (#946)
1 parent 289449a commit f86eb0d

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/providers/ContentShareProvider/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ export const ContentShareProvider: React.FC<
128128
audioVideo.stopContentShare();
129129
} else {
130130
if (source && typeof source === 'string') {
131-
audioVideo.startContentShareFromScreenCapture(source);
132-
} else if (source instanceof MediaStream) {
133-
audioVideo.startContentShare(source);
131+
await audioVideo.startContentShareFromScreenCapture(source);
132+
} else if (source && source instanceof MediaStream) {
133+
await audioVideo.startContentShare(source);
134134
} else {
135-
audioVideo.startContentShareFromScreenCapture();
135+
await audioVideo.startContentShareFromScreenCapture();
136136
}
137137
dispatch({ type: ContentActionType.STARTING });
138138
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)