Skip to content

Commit 7923d70

Browse files
committed
Moved utilities into seperate files
1 parent 909f4cb commit 7923d70

File tree

9 files changed

+89
-68
lines changed

9 files changed

+89
-68
lines changed

src/createContext.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import uuid from 'uuid-v4'
2+
3+
export default () => {
4+
const context = {
5+
id: uuid(),
6+
resolveComponent: (Component) => Component
7+
}
8+
9+
context.update = (newValues) => {
10+
Object.entries(newValues).forEach(([key, value]) => (context[key] = value))
11+
return context
12+
}
13+
14+
return context
15+
}

src/flushEffects.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import invariant from 'invariant'
2+
3+
export default (context) => () => {
4+
invariant(context.rerender, 'You must use the hook before effects can be flushed')
5+
6+
context.flushEffects()
7+
8+
return context.result
9+
}

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export * from './useHook'
1+
import createContext from './createContext'
2+
import useHookAdvanced from './useHookAdvanced'
3+
4+
export { cleanup } from 'react-testing-library'
5+
6+
export const useHook = (hook) => useHookAdvanced(hook, createContext())

src/update.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import invariant from 'invariant'
2+
3+
export default (context) => (props) => {
4+
invariant(context.rerender, 'You must use the hook before it can be updated')
5+
6+
context.rerender(props)
7+
8+
return context.result
9+
}

src/use.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
import { render } from 'react-testing-library'
3+
import invariant from 'invariant'
4+
5+
export default (hook, context) => (props) => {
6+
const HookHarness = (props) => {
7+
context.update({ result: hook(props) })
8+
return <div data-testid={context.id} />
9+
}
10+
11+
const { queryByTestId, rerender } = render(context.resolveComponent(<HookHarness {...props} />))
12+
13+
const container = queryByTestId(context.id)
14+
15+
invariant(container !== null, 'Failed to render wrapper component')
16+
17+
context.update({
18+
rerender: (newProps) => rerender(context.resolveComponent(<HookHarness {...newProps} />)),
19+
flushEffects: () => context.rerender(props)
20+
})
21+
22+
return context.result
23+
}

src/useHook.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/useHookAdvanced.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import use from './use'
2+
import withContextProvider from './withContextProvider'
3+
import update from './update'
4+
import flushEffects from './flushEffects'
5+
6+
export default (hook, context) => {
7+
return {
8+
use: use(hook, context),
9+
withContextProvider: withContextProvider(hook, context),
10+
update: update(context),
11+
flushEffects: flushEffects(context)
12+
}
13+
}

src/withContextProvider.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react'
2+
import useHookAdvanced from './useHookAdvanced'
3+
4+
export default (hook, context) => (ContextProvider, props) => {
5+
const { resolveComponent } = context
6+
return useHookAdvanced(
7+
hook,
8+
context.update({
9+
resolveComponent: (Component) => (
10+
<ContextProvider {...props}>{resolveComponent(Component)}</ContextProvider>
11+
)
12+
})
13+
)
14+
}
File renamed without changes.

0 commit comments

Comments
 (0)