diff --git a/src/platform/react-hooks/src/hooks/usePresence.test.tsx b/src/platform/react-hooks/src/hooks/usePresence.test.tsx index 352b3a781..57dadcb58 100644 --- a/src/platform/react-hooks/src/hooks/usePresence.test.tsx +++ b/src/platform/react-hooks/src/hooks/usePresence.test.tsx @@ -128,6 +128,30 @@ describe('usePresence', () => { }); }); + /** @nospec */ + it('usePresence works without default client', async () => { + const updateListener = vi.fn(); + ablyClient.channels.get(testChannelName).presence.subscribe('update', updateListener); + + render( + + + + + , + ); + + await act(async () => { + const button = screen.getByText(/Update/i); + button.click(); + await wait(2); + }); + + await waitFor(() => { + expect(updateListener).toHaveBeenCalledWith(expect.objectContaining({ data: 'baz' })); + }); + }); + /** @nospec */ it('handles channel errors', async () => { const onChannelError = vi.fn(); @@ -246,6 +270,22 @@ const UsePresenceComponentMultipleClients = () => { ); }; +const UsePresenceComponentWithOtherClient = () => { + const { updateStatus } = usePresence({ channelName: testChannelName, ablyId: 'otherClient' }, 'bar'); + + return ( + <> + + + ); +}; + interface UsePresenceStateErrorsComponentProps { onConnectionError?: (err: Ably.ErrorInfo) => unknown; onChannelError?: (err: Ably.ErrorInfo) => unknown; diff --git a/src/platform/react-hooks/src/hooks/usePresence.ts b/src/platform/react-hooks/src/hooks/usePresence.ts index d301c336a..42c7a6b6f 100644 --- a/src/platform/react-hooks/src/hooks/usePresence.ts +++ b/src/platform/react-hooks/src/hooks/usePresence.ts @@ -48,7 +48,7 @@ export function usePresence( const [connectionState, setConnectionState] = useState(ably.connection.state); useConnectionStateListener((stateChange) => { setConnectionState(stateChange.current); - }); + }, params.ablyId); // similar to connection states, we should only attempt to enter presence when in certain // channel states.