Code Monkey home page Code Monkey logo

test-preact-hooks's Introduction

test-preact-hooks ⚓️

Simple testing for preact hooks

This is a fork of the awesome test-react-hooks library migrated to preact - check it out

Example usage can be found at this sandbox

Edit examples

Contents

Get Started

To install either :

yarn add test-preact-hooks -D or npm i test-preact-hooks --save-dev

test-preact-hooks needs a dom to work, if you're running jest you should be good to go. For anything else consult your testing framework of choice.

Example

import { createTestProxy, cleanUp } from "test-preact-hooks";
import { useState } from "preact/hooks";

// Cleans up the dom container that's created during testing
// For jest users just add to setupFilesAfterEnv
afterEach(() => cleanUp());

// Create your hook
const useCounter = (initial = 0, inc = 1) => {
  const [count, setCount] = useState(initial);
  const inc = () => setCount(count + inc);
  return {
    count,
    inc,
  };
};

// Proxy of your hook, use it like you would in a component
// Internally calls render for the hook and act on everything else
const [prxCounter] = createTestProxy(useCounter);

it("will increment by one", () => {
  {
    const { count, inc } = prxCounter();
    expect(count).toBe(0);
    inc();
  }

  {
    const { count } = prxCounter();
    expect(count).toBe(1);
  }
});

it("start with a new initial amount", () => {
  {
    const { count, inc } = prxCounter(4);
    expect(count).toBe(4);
    inc();
  }

  {
    const { count } = prxCounter(4);
    expect(count).toBe(5);
  }
});

it("will increment by a new amount", () => {
  {
    const { count, inc } = prxCounter(0, 2);
    expect(count).toBe(0);
    inc();
  }

  {
    const { count } = prxCounter(0, 2);
    expect(count).toBe(2);
  }
});

Api

createTestProxy

createTestProxy<THook, TProps = any>(hook: THook,options: UseProxyOptions<TProps> = {}) => [THook, HookControl<TProps>]

Creates a proxy of the hook passed in for testing.

Arguments

  • hook : hook to be tested

  • options : optional options to render the hook with

    /**
     * Options for createTestProxy
     *
     * @export
     * @interface UseProxyOptions
     * @template TProps
     */
    export interface UseProxyOptions<TProps> {
      /**
       * Component to wrap the test component in
       *
       * @type {ComponentType<TProps>}
       */
      wrapper?: ComponentType<TProps>;
    
      /**
       * Initial  props to render the wrapper component with
       */
      props?: TProps;
    
      /**
       * Toggle if result of hook should be wrapped in a proxy - set to true to check for strict equality
       */
      shallow?: boolean;
    }

Result

Tuple with the first element being a proxy hook and it's control object

[THook, HookControl<TProps>]

  • THook - A proxy of the hook argument, each call to the hook will call render

  • HookControl<TProps> - Control object for the proxy hook

    /**
     * Control object for the proxy hook
     *
     * @export
     * @interface HookControl
     * @template TProps
     */
    export interface HookControl<TProps> {
      /**
       * Unmounts the test component
       * useful when testing the cleanup of useEffect or useLayoutEffect
       *
       * @memberof HookControl
       */
      unmount: () => void;
      /**
       * Updates the props to be used in the wrapper component
       * Does not cause a rerender, call the proxy hook to force that
       */
      props: TProps;
      /**
       * The container of the test component
       */
      readonly container: HTMLElement;
      /**
       * A promise that will resolve on update
       * Use when waiting for async effects to run
       */
      waitForNextUpdate: () => Promise<void>;
    }

If you chose to use shallow: true the result of the proxy will not be wrapped in a proxy. This can cause instability with complex hooks but can be used to check for strict equality (for example if a hook should return an exact object copy for useMemo or useEffect comparison).

cleanup

cleanUp():void

Function to be called after your tests to clean up the container created during tests and unmount the hook.

act

act(callback: () => void):void

Re-exported from preact/test-utils

Use this if your updating the dom outside the hook.

For an example on correct usage check out dom event example

test-preact-hooks's People

Contributors

andrew-r-nf avatar andrew-w-ross avatar dependabot-preview[bot] avatar dependabot[bot] avatar ignusg avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.