Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing ablyId argument to useConnectionStateListener #1821

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading