Skip to content

Commit

Permalink
✅ no errors when pusherClientRef is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
Harley Alexander committed Aug 21, 2019
1 parent 7935835 commit f58b4e4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/PusherProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import dequal from "dequal";
// context setup
const PusherContext = React.createContext<PusherContextValues>({});
export const __PusherContext = PusherContext;
export const __PusherConsumer = PusherContext.Consumer;

/**
* Provider for the pusher service in an app
Expand Down
21 changes: 16 additions & 5 deletions src/__tests__/useChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ import React from "react";
import { renderHook } from "@testing-library/react-hooks";
import { PusherProvider } from "../PusherProvider";
import { useChannel } from "../useChannel";
import { cleanup } from "@testing-library/react";

beforeEach(() => {
cleanup();
jest.resetAllMocks();
});

jest.mock("pusher-js", () => {
const { PusherMock } = require("pusher-js-mock");
// monkey patch missing function
PusherMock.prototype.disconnect = () => {};
const { PusherMock } = require("../mocks");
return PusherMock;
});

Expand All @@ -23,6 +19,21 @@ const config = {
};

describe("useChannel hook", () => {
test("should render without error", () => {
const wrapper = ({ children }: any) => (
<PusherProvider
{...config}
children={children}
value={{ client: { current: undefined }, triggerEndpoint: "d" }}
/>
);
const { result, rerender } = renderHook(() => useChannel("my-channel"), {
wrapper
});
rerender();
expect(result.current).toBeUndefined();
});

test("should subscribe to a channel with default options", () => {
const wrapper = ({ children }: any) => (
<PusherProvider {...config}>{children}</PusherProvider>
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/useEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { useEvent } from "../";
import { PusherProvider } from "../PusherProvider";
import { PusherChannelMock } from "../mocks";
import { Channel } from "pusher-js";
import { cleanup } from "@testing-library/react";

beforeEach(() => {
cleanup();
jest.resetAllMocks();
});

Expand Down
19 changes: 17 additions & 2 deletions src/__tests__/usePresenceChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import React from "react";
import { renderHook } from "@testing-library/react-hooks";
import { usePresenceChannel } from "../usePresenceChannel";
import { PusherProvider } from "../PusherProvider";
import { cleanup } from "@testing-library/react";
import { act } from "@testing-library/react-hooks";

beforeEach(() => {
cleanup();
jest.resetAllMocks();
});

Expand All @@ -33,6 +31,23 @@ const setup = (channelName = "my-channel", customConfig = {}) => {
};

describe("usePresenceChannel hook", () => {
test("should render without error", () => {
const wrapper = ({ children }: any) => (
<PusherProvider
{...config}
children={children}
value={{ client: { current: undefined }, triggerEndpoint: "d" }}
/>
);
const { result, rerender } = renderHook(
() => usePresenceChannel("presence-channel"),
{ wrapper }
);
rerender();
// no client was provided
expect(result.current.channel).toBeUndefined();
});

test('should throw an error if channelName doesn\'t have "presence-" in it', () => {
const { result } = setup("public-channel");
expect(result.error.message).toBe(
Expand Down
30 changes: 21 additions & 9 deletions src/__tests__/useTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@ beforeEach(() => {
(fetch as FetchMock).resetMocks();
});

const config = {
clientKey: "client-key",
cluster: "ap4",
triggerEndpoint: "trigger-endpoint"
};

test("should render without error", () => {
const wrapper = ({ children }: any) => (
<PusherProvider
{...config}
children={children}
value={{ client: { current: undefined }, triggerEndpoint: "d" }}
/>
);
const { result } = renderHook(() => useTrigger("my-channel"), { wrapper });
result.current("my-event", "data");
});

test("should push event to trigger endpoint without authentication and warn", async () => {
(fetch as FetchMock).mockResponseOnce("success");
jest.spyOn(global.console, "warn");

const props = {
clientKey: "client-key",
cluster: "ap4",
triggerEndpoint: "trigger-endpoint"
};

const wrapper = ({ children }: any) => (
<PusherProvider {...props}>{children}</PusherProvider>
<PusherProvider {...config}>{children}</PusherProvider>
);
const { result, rerender } = renderHook(() => useTrigger("my-channel"), {
wrapper
Expand All @@ -47,7 +59,7 @@ test("should push event to trigger endpoint without authentication and warn", as

test("should push event to trigger endpoint with authentication", async () => {
(fetch as FetchMock).mockResponseOnce("success");
const props = {
const config = {
clientKey: "client-key",
cluster: "ap4",
triggerEndpoint: "trigger-endpoint",
Expand All @@ -58,7 +70,7 @@ test("should push event to trigger endpoint with authentication", async () => {
};

const wrapper = ({ children }: any) => (
<PusherProvider {...props}>{children}</PusherProvider>
<PusherProvider {...config}>{children}</PusherProvider>
);
const { result, rerender } = renderHook(() => useTrigger("my-channel"), {
wrapper
Expand Down
2 changes: 1 addition & 1 deletion src/useTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function useTrigger(channelName: string) {
body: JSON.stringify({ channelName, eventName, data })
};

if (client.current.config && client.current.config.auth) {
if (client.current && client.current.config.auth) {
fetchOptions.headers = client.current.config.auth.headers;
} else {
console.warn(
Expand Down

0 comments on commit f58b4e4

Please sign in to comment.