From f58b4e43e75300f34a92e83a663256f22ffdd81c Mon Sep 17 00:00:00 2001 From: Harley Alexander Date: Wed, 21 Aug 2019 12:52:46 +1000 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20no=20errors=20when=20pusherClientRe?= =?UTF-8?q?f=20is=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/PusherProvider.tsx | 1 - src/__tests__/useChannel.tsx | 21 ++++++++++++++----- src/__tests__/useEvent.tsx | 2 -- src/__tests__/usePresenceChannel.tsx | 19 ++++++++++++++++-- src/__tests__/useTrigger.tsx | 30 +++++++++++++++++++--------- src/useTrigger.ts | 2 +- 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/PusherProvider.tsx b/src/PusherProvider.tsx index d3cb3f2..c0843e8 100644 --- a/src/PusherProvider.tsx +++ b/src/PusherProvider.tsx @@ -8,7 +8,6 @@ import dequal from "dequal"; // context setup const PusherContext = React.createContext({}); export const __PusherContext = PusherContext; -export const __PusherConsumer = PusherContext.Consumer; /** * Provider for the pusher service in an app diff --git a/src/__tests__/useChannel.tsx b/src/__tests__/useChannel.tsx index 18a068a..1cce6fb 100644 --- a/src/__tests__/useChannel.tsx +++ b/src/__tests__/useChannel.tsx @@ -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; }); @@ -23,6 +19,21 @@ const config = { }; describe("useChannel hook", () => { + test("should render without error", () => { + const wrapper = ({ children }: any) => ( + + ); + 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) => ( {children} diff --git a/src/__tests__/useEvent.tsx b/src/__tests__/useEvent.tsx index 3d69115..7774ff2 100644 --- a/src/__tests__/useEvent.tsx +++ b/src/__tests__/useEvent.tsx @@ -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(); }); diff --git a/src/__tests__/usePresenceChannel.tsx b/src/__tests__/usePresenceChannel.tsx index b6b6795..345d501 100644 --- a/src/__tests__/usePresenceChannel.tsx +++ b/src/__tests__/usePresenceChannel.tsx @@ -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(); }); @@ -33,6 +31,23 @@ const setup = (channelName = "my-channel", customConfig = {}) => { }; describe("usePresenceChannel hook", () => { + test("should render without error", () => { + const wrapper = ({ children }: any) => ( + + ); + 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( diff --git a/src/__tests__/useTrigger.tsx b/src/__tests__/useTrigger.tsx index c0e4f5a..44df46a 100644 --- a/src/__tests__/useTrigger.tsx +++ b/src/__tests__/useTrigger.tsx @@ -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) => ( + + ); + 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) => ( - {children} + {children} ); const { result, rerender } = renderHook(() => useTrigger("my-channel"), { wrapper @@ -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", @@ -58,7 +70,7 @@ test("should push event to trigger endpoint with authentication", async () => { }; const wrapper = ({ children }: any) => ( - {children} + {children} ); const { result, rerender } = renderHook(() => useTrigger("my-channel"), { wrapper diff --git a/src/useTrigger.ts b/src/useTrigger.ts index 83d7c8b..5eceaf2 100644 --- a/src/useTrigger.ts +++ b/src/useTrigger.ts @@ -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(