From a62b162fcfd1b69e467842689012499008dc50c9 Mon Sep 17 00:00:00 2001 From: evgeny Date: Mon, 29 Jul 2024 08:50:36 +0100 Subject: [PATCH] fix: add missing `ablyId` argument to `useConnectionStateListener` in `usePresence` implementation --- .../src/hooks/usePresence.test.tsx | 40 +++++++++++++++++++ .../react-hooks/src/hooks/usePresence.ts | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) 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.