From 0798afca4dc54810db04c92cd52b43a257f04b8c Mon Sep 17 00:00:00 2001 From: Harley Alexander Date: Wed, 4 Mar 2020 10:34:23 +1100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20added=20testUtils=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/useChannel.tsx | 2 +- src/index.ts | 5 ++++ src/testUtils.tsx | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/testUtils.tsx diff --git a/src/__tests__/useChannel.tsx b/src/__tests__/useChannel.tsx index edf4db9..d83b218 100644 --- a/src/__tests__/useChannel.tsx +++ b/src/__tests__/useChannel.tsx @@ -4,7 +4,7 @@ import Pusher from "pusher-js"; import React from "react"; import { __PusherContext } from "../PusherProvider"; import { renderHook } from "@testing-library/react-hooks"; -import { renderHookWithProvider } from "../../testUtils"; +import { renderHookWithProvider } from "../testUtils"; import { useChannel } from "../useChannel"; describe("useChannel()", () => { diff --git a/src/index.ts b/src/index.ts index 21f13af..9ded415 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +// hooks for prod export * from "./PusherProvider"; export * from "./usePusher"; export * from "./useChannel"; @@ -5,3 +6,7 @@ export * from "./usePresenceChannel"; export * from "./useEvent"; export * from "./useClientTrigger"; export * from "./useTrigger"; + +// test utils +import * as testUtils from "./testUtils"; +export { testUtils }; diff --git a/src/testUtils.tsx b/src/testUtils.tsx new file mode 100644 index 0000000..19ecc38 --- /dev/null +++ b/src/testUtils.tsx @@ -0,0 +1,46 @@ +import { act, renderHook } from "@testing-library/react-hooks"; + +import Pusher from "pusher-js"; +import { PusherMock } from "pusher-js-mock"; +import React from "react"; +import { __PusherContext } from "./PusherProvider"; + +/** + * Flushes async promises in mocks + */ +export const actAndFlushPromises = async () => + await act(async () => await new Promise(setImmediate)); + +/** + * Does a bit of setup for us so we don't have to repeat ourselves + * @param hook the hook you want to render, i.e. () => useHook() + * @param clientConfig the client config passed to PusherMock + */ +export async function renderHookWithProvider( + hook: () => T, + clientConfig: Record = {} +) { + const client = new PusherMock("key", clientConfig) as unknown; + const wrapper: React.FC = ({ children }) => ( + <__PusherContext.Provider value={{ client: client as Pusher }}> + {children} + + ); + const result = renderHook(hook, { wrapper }); + await actAndFlushPromises(); + return result; +} + +/** + * Generates basic Pusher config with authorizer + * @param id the id for the client + * @param info the info object for the client + */ +export const makeAuthPusherConfig = (id: string = "my-id", info: any = {}) => ({ + authorizer: () => ({ + authorize: ( + socketId: string, + callback: (errored: boolean, info: any) => void + ) => callback(socketId === "errored", { id, info }) + }) +});