Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add await for toggleContentShare #946

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/providers/ContentShareProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ export const ContentShareProvider: React.FC<
audioVideo.stopContentShare();
} else {
if (source && typeof source === 'string') {
audioVideo.startContentShareFromScreenCapture(source);
} else if (source instanceof MediaStream) {
audioVideo.startContentShare(source);
await audioVideo.startContentShareFromScreenCapture(source);
} else if (source && source instanceof MediaStream) {
await audioVideo.startContentShare(source);
} else {
audioVideo.startContentShareFromScreenCapture();
await audioVideo.startContentShareFromScreenCapture();
}
dispatch({ type: ContentActionType.STARTING });
}
Expand Down
55 changes: 55 additions & 0 deletions tst/providers/ContentShareProvider/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import React from 'react';
import { renderHook, act } from '@testing-library/react';
import {
ContentShareProvider,
useContentShareControls,
} from '../../../src/providers/ContentShareProvider';

// Mock audioVideo object
const mockAudioVideo = {
startContentShareFromScreenCapture: jest.fn(),
startContentShare: jest.fn(),
stopContentShare: jest.fn(),
addObserver: jest.fn(),
removeObserver: jest.fn(),
addContentShareObserver: jest.fn(),
removeContentShareObserver: jest.fn()
};
// Mock the module containing useAudioVideo hook
jest.mock('../../../src/providers/AudioVideoProvider', () => ({
useAudioVideo: () => mockAudioVideo
}));

describe('toggleContentShare', () => {
it('Should call startContentShareFromScreenCapture', async () => {
const { result } = renderHook(() => useContentShareControls(), {
wrapper: ({ children }) => (
<ContentShareProvider>{children}</ContentShareProvider>
),
});
await act(async () => {
await result.current.toggleContentShare();
});
expect(mockAudioVideo.startContentShareFromScreenCapture).toHaveBeenCalledTimes(1);
});

it('Can catch error from startContentShareFromScreenCapture', async () => {
mockAudioVideo.startContentShareFromScreenCapture = jest.fn(() => Promise.reject(new Error('startContentShare error')));
const { result } = renderHook(() => useContentShareControls(), {
wrapper: ({ children }) => (
<ContentShareProvider>{children}</ContentShareProvider>
),
});
await expect(async () => {
await act(async () => {
await result.current.toggleContentShare();
});
}).rejects.toThrow('startContentShare error');
// expect(() => {
// result.current.toggleContentShare();
// }).toThrow('startContentShare error');
});
});
Loading