@glyph-cat/react-test-utils
TypeScript icon, indicating that this package has built-in type declarations

0.1.3-experimental.0 • Public • Published

Helper functions for writing tests for React with Jest and react-test-renderer.


Version License

Designed for React Open in Visual Studio Code Support me on Ko-fi


Simple Example

import { useState } from 'react'
import {
  createCleanupRef,
  createHookInterface,
} from '@glyph-cat/react-test-utils'

const cleanupRef = createCleanupRef()
afterEach(() => { cleanupRef.run() })

test('createHookInterface', () => {
  const hookInterface = createHookInterface({
    useHook: () => useState(0),
    actions: {
      increaseCounter({ hookData }) {
        const [, setCounter] = hookData
        setCounter((c: number) => c + 1)
      },
    },
    values: {
      value({ hookData }) {
        const [counter] = hookData
        return counter
      },
    },
  }, cleanupRef)

  // Trigger one action
  hookInterface.actions('increaseCounter')

  // Trigger multiple actions in the same render
  hookInterface.actions('increaseCounter', 'increaseCounter')

  // Trigger multiple asynchronous actions
  // No longer guaranteed that all actions will run in the same render cycle
  hookInterface.actionsAsync('increaseCounter', 'increaseCounter')

  // Get render count
  expect(hookInterface.getRenderCount()).toBe(2)

  // Get value
  expect(hookInterface.get('value')).toBe(3)

})

Full Examples


Support This Project


Package Sidebar

Install

npm i @glyph-cat/react-test-utils

Weekly Downloads

0

Version

0.1.3-experimental.0

License

MIT

Unpacked Size

26.4 kB

Total Files

12

Last publish

Collaborators

  • glyph-cat