Skip to content

Commit 0bc0282

Browse files
committed
Renamed render to be use
1 parent 7a2fd6d commit 0bc0282

File tree

5 files changed

+75
-69
lines changed

5 files changed

+75
-69
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# react-hooks-testing-library

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "react-hooks-testing-library",
33
"version": "0.1.0",
4-
"description": "Utilities for testing React hooks",
4+
"description": "Simple component wrapper for testing React hooks",
55
"main": "lib/index.js",
66
"scripts": {
77
"build": "babel --out-dir lib src",
8-
"test": "jest --coverage --colors"
8+
"test": "jest --coverage"
99
},
1010
"author": "Michael Peyper",
1111
"license": "MIT",
@@ -28,7 +28,7 @@
2828
"react-dom": "^16.7.0-alpha.0"
2929
},
3030
"peerDependencies": {
31-
"react-dom": "^16.7.0-alpha.0"
31+
"react": "^16.7.0-alpha.0"
3232
},
3333
"jest": {
3434
"collectCoverageFrom": [

src/index.js

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1 @@
1-
import React from 'react'
2-
import { render, cleanup } from 'react-testing-library'
3-
import invariant from 'invariant'
4-
import uuid from 'uuid-v4'
5-
6-
const renderHook = (hook, context) => (props) => {
7-
8-
const containerId = uuid()
9-
10-
const HookHarness = (props) => {
11-
context.result = hook(props)
12-
return <div data-testid={containerId} />
13-
}
14-
15-
const { queryByTestId, rerender } = render(context.resolveComponent({ children: <HookHarness {...props} /> }))
16-
17-
const container = queryByTestId(containerId)
18-
19-
invariant(container !== null, 'You must render children when wrapping the hook')
20-
21-
context.rerender = (newProps) => rerender(context.resolveComponent({ children: <HookHarness {...newProps} /> }))
22-
context.flushEffects = () => rerender(context.resolveComponent({ children: <HookHarness {...props} /> }))
23-
24-
return context.result
25-
}
26-
27-
const updateHook = (context) => (props) => {
28-
invariant(context.rerender, 'You must render the hook before it can be updated')
29-
30-
context.rerender(props)
31-
32-
return context.result
33-
}
34-
35-
const flushHookEffects = (context) => () => {
36-
invariant(context.rerender, 'You must render the hook before effects can be flushed')
37-
38-
context.flushEffects()
39-
40-
return context.result
41-
}
42-
43-
const wrapHook = (hook, context) => (wrap) => {
44-
invariant(typeof wrap === 'function', 'wrap must be provided a function')
45-
const { resolveComponent } = context
46-
return useHookAdvanced(hook, { ...context, resolveComponent: (props) => wrap({ children: resolveComponent(props) }) })
47-
}
48-
49-
const useHookAdvanced = (hook, context) => {
50-
return {
51-
render: renderHook(hook, context),
52-
update: updateHook(context),
53-
flushEffects: flushHookEffects(context),
54-
wrap: wrapHook(hook, context)
55-
}
56-
}
57-
58-
export const useHook = (hook) => useHookAdvanced(hook, { resolveComponent: ({ children }) => children })
59-
60-
export { cleanup }
1+
export * from './useHook'

src/useHook.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react'
2+
import { render, cleanup } from 'react-testing-library'
3+
import invariant from 'invariant'
4+
import uuid from 'uuid-v4'
5+
6+
const use = (hook, context) => (props) => {
7+
8+
const HookHarness = (props) => {
9+
context.result = hook(props)
10+
return <div data-testid={context.id} />
11+
}
12+
13+
const { queryByTestId, rerender } = render(context.resolveComponent({ children: <HookHarness {...props} /> }))
14+
15+
const container = queryByTestId(context.id)
16+
17+
invariant(container !== null, 'You must render children when wrapping the hook')
18+
19+
context.rerender = (newProps) => rerender(context.resolveComponent({ children: <HookHarness {...newProps} /> }))
20+
context.flushEffects = () => context.rerender(props)
21+
22+
return context.result
23+
}
24+
25+
const update = (context) => (props) => {
26+
invariant(context.rerender, 'You must render the hook before it can be updated')
27+
28+
context.rerender(props)
29+
30+
return context.result
31+
}
32+
33+
const flushEffects = (context) => () => {
34+
invariant(context.rerender, 'You must render the hook before effects can be flushed')
35+
36+
context.flushEffects()
37+
38+
return context.result
39+
}
40+
41+
const wrap = (hook, context) => (wrap) => {
42+
invariant(typeof wrap === 'function', 'wrap must be provided a function')
43+
const { resolveComponent } = context
44+
return useHookAdvanced(hook, { ...context, resolveComponent: (props) => wrap({ children: resolveComponent(props) }) })
45+
}
46+
47+
const useHookAdvanced = (hook, context) => {
48+
return {
49+
use: use(hook, context),
50+
update: update(context),
51+
flushEffects: flushEffects(context),
52+
wrap: wrap(hook, context)
53+
}
54+
}
55+
56+
export const useHook = (hook) => {
57+
const context = {
58+
id: uuid(),
59+
resolveComponent: ({ children }) => children
60+
}
61+
return useHookAdvanced(hook, context)
62+
}
63+
64+
export { cleanup }

test/integration.test.js renamed to test/useHook.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ describe('useHook tests', () => {
55
afterEach(cleanup)
66

77
test('should handle useState hooks', () => {
8-
const { render, update } = useHook(() => useState('foo'))
8+
const { use, update } = useHook(() => useState('foo'))
99

10-
const [value1, setValue] = render()
10+
const [value1, setValue] = use()
1111

1212
expect(value1).toBe('foo')
1313

@@ -21,13 +21,13 @@ describe('useHook tests', () => {
2121
test('should handle useContext hooks', () => {
2222
const TestContext = createContext('foo')
2323

24-
const { render } = useHook(() => useContext(TestContext)).wrap(({ children }) => (
24+
const { use } = useHook(() => useContext(TestContext)).wrap(({ children }) => (
2525
<TestContext.Provider value='bar'>
2626
{children}
2727
</TestContext.Provider>
2828
))
2929

30-
const value = render()
30+
const value = use()
3131

3232
expect(value).toBe('bar')
3333
})
@@ -36,14 +36,14 @@ describe('useHook tests', () => {
3636

3737
const sideEffect = { [1]: false, [2]: false }
3838

39-
const { render, flushEffects, update } = useHook(({ id }) => useEffect(() => {
39+
const { use, flushEffects, update } = useHook(({ id }) => useEffect(() => {
4040
sideEffect[id] = true
4141
return () => {
4242
sideEffect[id] = false
4343
}
4444
}, [id]))
4545

46-
render({ id: 1 })
46+
use({ id: 1 })
4747

4848
expect(sideEffect[1]).toBe(false)
4949
expect(sideEffect[2]).toBe(false)

0 commit comments

Comments
 (0)