Skip to content

Commit

Permalink
fix: add missing ablyId argument to useConnectionStateListener in…
Browse files Browse the repository at this point in the history
… `usePresence` implementation
  • Loading branch information
ttypic committed Jul 29, 2024
1 parent f606332 commit a62b162
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 a62b162

Please sign in to comment.