|
1 | | -import createContext from './createContext' |
2 | | -import useHookAdvanced from './useHookAdvanced' |
| 1 | +import React from 'react' |
| 2 | +import { render, cleanup } from 'react-testing-library' |
| 3 | +import invariant from 'invariant' |
| 4 | +import uuid from 'uuid-v4' |
3 | 5 |
|
4 | | -export { cleanup } from 'react-testing-library' |
| 6 | +export const useHook = (hook, ...props) => { |
| 7 | + const context = { |
| 8 | + id: uuid(), |
| 9 | + resolveComponent: (Component) => Component, |
| 10 | + rendered: false, |
| 11 | + props |
| 12 | + } |
5 | 13 |
|
6 | | -export const useHook = (hook) => useHookAdvanced(hook, createContext()) |
| 14 | + const HookHarness = () => { |
| 15 | + context.currentValue = hook(...context.props) |
| 16 | + return <div data-testid={context.id} /> |
| 17 | + } |
| 18 | + |
| 19 | + const renderHook = () => { |
| 20 | + const { queryByTestId, rerender } = render(context.resolveComponent(<HookHarness />)) |
| 21 | + const container = queryByTestId(context.id) |
| 22 | + |
| 23 | + invariant(container !== null, 'Failed to render wrapper component') |
| 24 | + |
| 25 | + context.rendered = true |
| 26 | + context.rerender = () => rerender(context.resolveComponent(<HookHarness />)) |
| 27 | + } |
| 28 | + |
| 29 | + const getCurrentValue = () => { |
| 30 | + if (!context.rendered) { |
| 31 | + renderHook() |
| 32 | + } else { |
| 33 | + context.rerender() |
| 34 | + } |
| 35 | + return context.currentValue |
| 36 | + } |
| 37 | + |
| 38 | + const setProps = (...newProps) => { |
| 39 | + context.props = newProps |
| 40 | + } |
| 41 | + |
| 42 | + const addContextProvider = (ContextProvider, contextProps) => { |
| 43 | + const Provider = ContextProvider.Provider || ContextProvider |
| 44 | + const { resolveComponent } = context |
| 45 | + const updateContext = (newContextProps) => { |
| 46 | + contextProps = newContextProps |
| 47 | + } |
| 48 | + context.resolveComponent = (Component) => ( |
| 49 | + <Provider {...contextProps}>{resolveComponent(Component)}</Provider> |
| 50 | + ) |
| 51 | + return { updateContext } |
| 52 | + } |
| 53 | + |
| 54 | + const flushEffects = (minTimes = 1, maxTimes = Math.max(minTimes + 1, 100)) => { |
| 55 | + invariant( |
| 56 | + minTimes > 0 && minTimes <= maxTimes, |
| 57 | + `minTimes (${minTimes}) must me a positive number that is less than maxTimes (${maxTimes})` |
| 58 | + ) |
| 59 | + invariant(maxTimes > 0, `maxTimes (${maxTimes}) must me a positive number`) |
| 60 | + |
| 61 | + let lastValue |
| 62 | + let currentValue |
| 63 | + let flushCount = 0 |
| 64 | + |
| 65 | + while ((currentValue !== lastValue || flushCount < minTimes) && flushCount < maxTimes) { |
| 66 | + lastValue = currentValue |
| 67 | + currentValue = getCurrentValue() |
| 68 | + flushCount++ |
| 69 | + } |
| 70 | + |
| 71 | + invariant( |
| 72 | + flushCount < Math.max(minTimes, maxTimes), |
| 73 | + `Hook values have not resolved after recalculating ${maxTimes} times` |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + getCurrentValue, |
| 79 | + getCurrentValues: getCurrentValue, |
| 80 | + flushEffects, |
| 81 | + setProps, |
| 82 | + addContextProvider |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export { cleanup } |
0 commit comments