Skip to content

Commit

Permalink
Merge pull request #1821 from ably/ECO-4890/fix-presence-hook
Browse files Browse the repository at this point in the history
fix: add missing `ablyId` argument to  `useConnectionStateListener`
  • Loading branch information
ttypic authored Jul 29, 2024
2 parents f606332 + a62b162 commit 672a5e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/platform/react-hooks/src/hooks/usePresence.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<AblyProvider ablyId="otherClient" client={otherClient as unknown as Ably.RealtimeClient}>
<ChannelProvider channelName={testChannelName} ablyId="otherClient">
<UsePresenceComponentWithOtherClient />
</ChannelProvider>
</AblyProvider>,
);

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();
Expand Down Expand Up @@ -246,6 +270,22 @@ const UsePresenceComponentMultipleClients = () => {
);
};

const UsePresenceComponentWithOtherClient = () => {
const { updateStatus } = usePresence({ channelName: testChannelName, ablyId: 'otherClient' }, 'bar');

return (
<>
<button
onClick={() => {
updateStatus('baz');
}}
>
Update
</button>
</>
);
};

interface UsePresenceStateErrorsComponentProps {
onConnectionError?: (err: Ably.ErrorInfo) => unknown;
onChannelError?: (err: Ably.ErrorInfo) => unknown;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/react-hooks/src/hooks/usePresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function usePresence<T = any>(
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.
Expand Down

0 comments on commit 672a5e5

Please sign in to comment.