|
| 1 | +/// <reference types="@types/jest" /> |
| 2 | + |
| 3 | +import DailyIframe, { DailyCall } from '@daily-co/daily-js'; |
| 4 | +import { renderHook } from '@testing-library/react-hooks'; |
| 5 | +import React from 'react'; |
| 6 | + |
| 7 | +import { DailyProvider } from '../../src/DailyProvider'; |
| 8 | +import { useLocalSessionId } from '../../src/hooks/useLocalSessionId'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Mock DailyRoom. |
| 12 | + * It's not required for useLocalSessionId and causes unwanted state updates. |
| 13 | + */ |
| 14 | +jest.mock('../../src/DailyRoom', () => ({ |
| 15 | + DailyRoom: (({ children }) => <>{children}</>) as React.FC, |
| 16 | +})); |
| 17 | + |
| 18 | +const createWrapper = |
| 19 | + (callObject: DailyCall = DailyIframe.createCallObject()): React.FC => |
| 20 | + ({ children }) => |
| 21 | + <DailyProvider callObject={callObject}>{children}</DailyProvider>; |
| 22 | + |
| 23 | +describe('useLocalSessionId', () => { |
| 24 | + it('returns null, if daily.participants() does not contain local user yet', async () => { |
| 25 | + const daily = DailyIframe.createCallObject(); |
| 26 | + (daily.participants as jest.Mock).mockImplementation(() => ({})); |
| 27 | + const { result, waitFor } = renderHook(() => useLocalSessionId(), { |
| 28 | + wrapper: createWrapper(daily), |
| 29 | + }); |
| 30 | + await waitFor(() => { |
| 31 | + expect(result.current).toBeNull(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + it('returns local user session_id', async () => { |
| 35 | + const daily = DailyIframe.createCallObject(); |
| 36 | + (daily.participants as jest.Mock).mockImplementation(() => ({ |
| 37 | + local: { |
| 38 | + local: true, |
| 39 | + session_id: 'local', |
| 40 | + user_name: '', |
| 41 | + }, |
| 42 | + })); |
| 43 | + const { result, waitFor } = renderHook(() => useLocalSessionId(), { |
| 44 | + wrapper: createWrapper(daily), |
| 45 | + }); |
| 46 | + await waitFor(() => { |
| 47 | + expect(result.current).toEqual('local'); |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
0 commit comments