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

0.6.0 • Public • Published

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


Version License Support me on Ko-fi


Simple Example

import { CleanupManager } from '@glyph-cat/cleanup-manager'
import { HookTester } from '@glyph-cat/react-test-utils'
import { useState } from 'react'

const cleanupManager = new CleanupManager()
afterEach(() => { cleanupManager.run() })

test('Example', async () => {
  
  const tester = new HookTester({
    useHook: () => useState(0),
    actions: {
      increaseCounter(hookData) {
        const [, setCounter] = hookData
        setCounter((c: number) => c + 1)
      },
    },
    values: {
      value(hookData) {
        const [counter] = hookData
        return counter
      },
    },
  }, cleanupManager)

  // Trigger one action
  tester.action('increaseCounter')

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

  // Trigger multiple async actions in the same render
  await tester.actionAsync('increaseCounter', 'increaseCounter')

  // Get render count
  expect(tester.renderCount).toBe(2)

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

})

Full Examples


Support This Project


Package Sidebar

Install

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

Weekly Downloads

7

Version

0.6.0

License

MIT

Unpacked Size

17.1 kB

Total Files

12

Last publish

Collaborators

  • glyph-cat