Skip to content

Commit

Permalink
♻️ Loosen channelName requirement in use(presence)Channel to allow ch…
Browse files Browse the repository at this point in the history
…annelName to be undefined (plus tests)
  • Loading branch information
mayteio committed Jul 22, 2020
1 parent 1702c53 commit ca6e1af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/__tests__/useChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import React from "react";
import { __PusherContext } from "../PusherProvider";
import { renderHook } from "@testing-library/react-hooks";
import { renderHookWithProvider } from "../testUtils";
import { useChannel } from "../useChannel";
import { useChannel, NO_CHANNEL_NAME_WARNING } from "../useChannel";

describe("useChannel()", () => {
test("should throw an error when no channelName present", () => {
const { result } = renderHook(() => useChannel(undefined));
expect(result.error.message).toBe(
"channelName required to subscribe to a channel"
const wrapper: React.FC = (props) => (
<__PusherContext.Provider value={{ client: {} as any }} {...props} />
);

jest.spyOn(console, "warn");
renderHook(() => useChannel(undefined), { wrapper });
expect(console.warn).toHaveBeenCalledWith(NO_CHANNEL_NAME_WARNING);
});

test("should return undefined if no pusher client present", () => {
Expand Down
18 changes: 10 additions & 8 deletions src/useChannel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Channel, PresenceChannel } from "pusher-js";
import { useEffect, useState } from "react";

import invariant from "invariant";
import { usePusher } from "./usePusher";

/**
Expand All @@ -18,19 +16,26 @@ import { usePusher } from "./usePusher";
* ```
*/

export const NO_CHANNEL_NAME_WARNING =
"No channel name passed to useChannel. No channel has been subscribed to.";

export function useChannel<T extends Channel & PresenceChannel>(
channelName: string | undefined
) {
// errors for missing arguments
invariant(channelName, NO_CHANNEL_NAME_ERROR);
const { client } = usePusher();
const [channel, setChannel] = useState<T | undefined>();
useEffect(() => {
/** Return early if there's no client */
if (!client) return;

/** Return early and warn if there's no channel */
if (!channelName) {
console.warn(NO_CHANNEL_NAME_WARNING);
return;
}

/** Subscribe to channel and set it in state */
const pusherChannel = client?.subscribe(channelName);
const pusherChannel = client.subscribe(channelName);
setChannel(pusherChannel as T);

/** Cleanup on unmount/re-render */
Expand All @@ -40,6 +45,3 @@ export function useChannel<T extends Channel & PresenceChannel>(
/** Return the channel for use. */
return channel;
}

export const NO_CHANNEL_NAME_ERROR =
"channelName required to subscribe to a channel";
4 changes: 2 additions & 2 deletions src/usePresenceChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ export const presenceChannelReducer = (
};

export function usePresenceChannel(
channelName: string
channelName: string | undefined
): usePresenceChannelValue {
// errors for missing arguments
invariant(
channelName.includes("presence-"),
channelName && channelName.includes("presence-"),
"Presence channels should use prefix 'presence-' in their name. Use the useChannel hook instead."
);

Expand Down

0 comments on commit ca6e1af

Please sign in to comment.