1.0.1 • Published 1 year ago

@saasquatch/universal-hooks-testing-library v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Universal Hooks Testing Library

This library provides an interface to test hooks written with Universal Hooks. You simply plug an implementation in and continue testing as usual. The API is based off of React Hooks Testing Library.

Usage

First, you plug in an implementation. Here's an example with React:

import * as React from "react";
import * as ReactTestLib from "@testing-library/react-hooks";

setImplementation(React);
setTestImplementation(ReactTestLib);

Or with haunted:

import * as haunted from "haunted";
import * as hauntedTestingLib from "@saasquatch/haunted-hooks-testing-library";

setImplementation(haunted);
setTestImplementation(hauntedTestingLib);

Then, use it like usual.

import { useState } from "@saasquatch/universal-hooks";
import { act, renderHook, setTestImplementation } from "@saasquatch/universal-hooks-testing-library";

test("example", () => {
  function useHook() {
    const [counter, setCounter] = useState(0);
    const inc = setCounter((c) => c + 1);
    return { counter, inc };
  }

  let result;
  act(() => {
    ({ result } = renderHook(useHook));
  });
  expect(result.current.counter).toBe(0);

  act(() => {
    result.current.inc();
  });
  expect(result.current.counter).toBe(1);
});