-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨warn, instead of throw error, when context isn't available
- Loading branch information
Harley Alexander
committed
Jan 22, 2020
1 parent
ace6cfc
commit ac5bb8e
Showing
6 changed files
with
53 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { PusherPresenceChannelMock } from "../mocks"; | ||
import { useClientTrigger } from "../useClientTrigger"; | ||
import { PresenceChannel } from "pusher-js"; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { PusherPresenceChannelMock } from '../mocks'; | ||
import { useClientTrigger } from '../useClientTrigger'; | ||
import { PresenceChannel } from 'pusher-js'; | ||
|
||
describe("<useClientTrigger />", () => { | ||
test("should ", () => { | ||
const mockChannel = (new PusherPresenceChannelMock() as unknown) as PresenceChannel< | ||
any | ||
>; | ||
describe('<useClientTrigger />', () => { | ||
test('should ', () => { | ||
const mockChannel = (new PusherPresenceChannelMock() as unknown) as PresenceChannel<unknown>; | ||
renderHook(() => useClientTrigger(mockChannel)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { usePusher, NOT_IN_CONTEXT_WARNING } from '../usePusher'; | ||
|
||
describe('usePusher hook', () => { | ||
test('should warn when not inside a pusher context', () => { | ||
const spy = jest.spyOn(global.console, 'warn'); | ||
const { result } = renderHook(() => usePusher()); | ||
// expect(result.current).toBeUndefined(); | ||
// expect(spy).toHaveBeenCalledWith(NOT_IN_CONTEXT_WARNING); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { useContext } from "react"; | ||
import { __PusherContext } from "./PusherProvider"; | ||
import { PusherContextValues } from "./types"; | ||
import { useContext, useEffect } from 'react'; | ||
import { __PusherContext } from './PusherProvider'; | ||
import { PusherContextValues } from './types'; | ||
|
||
/** | ||
* Provides access to the pusher client | ||
* | ||
* @example | ||
* const {client} = usePusher(); | ||
* client.subscribe('my-channel'); | ||
* client.current.subscribe('my-channel'); | ||
*/ | ||
export function usePusher() { | ||
return useContext<PusherContextValues>(__PusherContext); | ||
const context = useContext<PusherContextValues>(__PusherContext); | ||
useEffect(() => { | ||
if (!context) console.warn(NOT_IN_CONTEXT_WARNING); | ||
}, [context]); | ||
return context; | ||
} | ||
|
||
export const NOT_IN_CONTEXT_WARNING = | ||
'No Pusher context. Did you forget to wrap your app in a <PusherProvider />?'; |