-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5582 from daily-co/eng-4927-add-hook-to-get-local…
…-participant ENG-4927 Implement useLocalSessionId hook
- Loading branch information
Showing
5 changed files
with
71 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useRecoilValue } from 'recoil'; | ||
|
||
import { localIdState } from '../DailyParticipants'; | ||
|
||
/** | ||
* Returns the local participant's session_id or null, | ||
* if the local participant doesn't exist. | ||
*/ | ||
export const useLocalSessionId = () => { | ||
return useRecoilValue(localIdState) || null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/// <reference types="@types/jest" /> | ||
|
||
import DailyIframe, { DailyCall } from '@daily-co/daily-js'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import React from 'react'; | ||
|
||
import { DailyProvider } from '../../src/DailyProvider'; | ||
import { useLocalSessionId } from '../../src/hooks/useLocalSessionId'; | ||
|
||
/** | ||
* Mock DailyRoom. | ||
* It's not required for useLocalSessionId and causes unwanted state updates. | ||
*/ | ||
jest.mock('../../src/DailyRoom', () => ({ | ||
DailyRoom: (({ children }) => <>{children}</>) as React.FC, | ||
})); | ||
|
||
const createWrapper = | ||
(callObject: DailyCall = DailyIframe.createCallObject()): React.FC => | ||
({ children }) => | ||
<DailyProvider callObject={callObject}>{children}</DailyProvider>; | ||
|
||
describe('useLocalSessionId', () => { | ||
it('returns null, if daily.participants() does not contain local user yet', async () => { | ||
const daily = DailyIframe.createCallObject(); | ||
(daily.participants as jest.Mock).mockImplementation(() => ({})); | ||
const { result, waitFor } = renderHook(() => useLocalSessionId(), { | ||
wrapper: createWrapper(daily), | ||
}); | ||
await waitFor(() => { | ||
expect(result.current).toBeNull(); | ||
}); | ||
}); | ||
it('returns local user session_id', async () => { | ||
const daily = DailyIframe.createCallObject(); | ||
(daily.participants as jest.Mock).mockImplementation(() => ({ | ||
local: { | ||
local: true, | ||
session_id: 'local', | ||
user_name: '', | ||
}, | ||
})); | ||
const { result, waitFor } = renderHook(() => useLocalSessionId(), { | ||
wrapper: createWrapper(daily), | ||
}); | ||
await waitFor(() => { | ||
expect(result.current).toEqual('local'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters