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