npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-proxy-hook

v0.4.2

Published

Helps you test a React Hook

Downloads

20

Readme

react-proxy-hook

Helps you test a React Hook

npm version

Requirement ⚠️

To use react-proxy-hook, you must use [email protected]. React Hooks is currently at RFC stage.

Installation

$ npm i -D react-proxy-hook

Usage

react-proxy-hook is a wrapper around Kent C. Dodd's react-testing-library. It was inspired by the setup function that Kent wrote in his YouTube video. It is presented here as a simple React Hooks function wrapper.

Hooks can not be tested as standalone functions as they will not work unless they are called from withing a rendered component. react-proxy-hook gets around this limitation by proxying an actual component.

Maybe should have been called a HOH (Higher Order Hook)?

Look at how easy testing a Hook can be!

Method 1: testHook

Method 1 requires that you place the call to your cutom Hook inside of a callback function. You then destructure (or not) your data into a let that you previously defined.

import { testHook, cleanup } from 'react-proxy-hook';
import useCounter from '../use-counter';

afterEach(cleanup);

test('useCounter', () => {
  let count, increment;
  testHook(() => ({ count, increment } = useCounter({ initialCount: 2 })));

  expect(count).toBe(2);
  increment();
  expect(count).toBe(3);
});

Using Method 1 (suggested), there are no restrictions on destructuring.

I've made a PR into react-testing-library for this, so hopefully this will be in merged soon.

See the test for Method 1 running live on CodeSandbox.

Edit React Codesandbox

Method 2: proxyHook

You test your Hook just like you would use it in an actual component. Just pass your actual hook to proxyHook and it will return a proxied version of your hook.

import { proxyHook, cleanup } from 'react-proxy-hook';
import useCounter from '../use-counter';

const useCounterProxy = proxyHook(useCounter);

afterEach(cleanup);

test('useCounter', () => {
  const counterData = useCounterProxy();
  counterData.increment();
  expect(counterData.count).toBe(1);
  counterData.decrement();
  expect(counterData.count).toBe(0);
});

See the test for Method 2 running live on CodeSandbox.

Edit demo app on CodeSandbox

Testing objects

To test with a returned object (ex: { foo: 'bar' } ) use the dot notation.

const returnedVal = useFooProxy();
expect(returnedVal.foo).toBe('bar');

Testing arrays

To test with a returned array (ex: [0] ) use bracket notation.

const returnedVal = useFooProxy();
expect(returnedVal[0]).toBe(0);

Testing other types

To test with other returned types (i.e. string, numbers, null, undefined), use they key "value".

const returnedVal = useFooProxy();
expect(returnedVal.value).toBe('foo');

Limitations

You must not deconstruct the returned value in your test. For example, the following wo't work as count will still be 0.

const { count, increment } = useCounterProxy(0);
increment();
expect(count).toBe(1);

License

MIT Licensed


Obviously, none of this would not be possible without the tireless work of Kent C. Dodds. Thank you! 🙏