Helper functions for writing tests for React with Jest and react-test-renderer.
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
- Ko-fi:
ko-fi.com/glyphcat
- BTC:
bc1q5qp6a972l8m0k26ln9deuhup0nmldf86ndu5we