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

Remove console warning for falsy channel #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 2 additions & 5 deletions docs/modules/_core_usechannel_.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ <h3>Functions</h3>
<section class="tsd-panel-group tsd-member-group ">
<h2>Variables</h2>
<section class="tsd-panel tsd-member tsd-kind-variable tsd-parent-kind-module">
<a name="no_channel_name_warning" class="tsd-anchor"></a>
<h3><span class="tsd-flag ts-flagConst">Const</span> NO_<wbr>CHANNEL_<wbr>NAME_<wbr>WARNING</h3>
<div class="tsd-signature tsd-kind-icon">NO_<wbr>CHANNEL_<wbr>NAME_<wbr>WARNING<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">"No channel name passed to useChannel. No channel has been subscribed to."</span><span class="tsd-signature-symbol"> = &quot;No channel name passed to useChannel. No channel has been subscribed to.&quot;</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in core/useChannel.ts:19</li>
Expand All @@ -108,7 +105,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> NO_<wbr>CHANNEL_<wbr>NAME_<
<dd><p>of channel you&#39;re subscribing to. Can be one of <code>Channel</code> or <code>PresenceChannel</code> from <code>pusher-js</code>.</p>
</dd>
<dt>returns</dt>
<dd><p>Instance of the channel you just subscribed to.</p>
<dd><p>Instance of the channel you just subscribed to (or <code>undefined</code> if the channel name provided is falsy).</p>
</dd>
<dt>example</dt>
<dd><pre><code class="language-javascript"><span class="hljs-keyword">const</span> channel = useChannel(<span class="hljs-string">&quot;my-channel&quot;</span>)
Expand Down Expand Up @@ -224,4 +221,4 @@ <h2>Legend</h2>
<div class="overlay"></div>
<script src="../assets/js/main.js"></script>
</body>
</html>
</html>
12 changes: 7 additions & 5 deletions src/__tests__/useChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { PusherChannelMock } from "pusher-js-mock";
import React from "react";
import { renderHook } from "@testing-library/react-hooks";
import { renderHookWithProvider } from "../testUtils";
import { useChannel, NO_CHANNEL_NAME_WARNING } from "../core/useChannel";
import { useChannel } from "../core/useChannel";
import { __PusherContext } from "../core/PusherProvider";

describe("useChannel()", () => {
test("should throw an error when no channelName present", () => {
test("should return undefined when channelName is falsy", () => {
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);
const { result } = renderHook(() => useChannel(""), {
wrapper,
});

expect(result.current).toBeUndefined();
});

test("should return undefined if no pusher client present", () => {
Expand Down
10 changes: 2 additions & 8 deletions src/core/useChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ 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
) {
Expand All @@ -28,11 +25,8 @@ export function useChannel<T extends Channel & PresenceChannel>(
/** 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;
}
/** Return early if channel name is falsy */
if (!channelName) return;

/** Subscribe to channel and set it in state */
const pusherChannel = client.subscribe(channelName);
Expand Down