-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd39736
commit ac9ee8f
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { ErrorBoundary } from 'react-error-boundary'; | ||
import { act, create, ReactTestRenderer } from 'react-test-renderer'; | ||
|
||
type Result<T> = | ||
| { | ||
__type: 'error'; | ||
current: Error; | ||
reset: () => void; | ||
} | ||
| { | ||
__type: 'result'; | ||
current?: T; | ||
}; | ||
|
||
type TestComponentProps<THookReturn> = { | ||
executor(): THookReturn; | ||
resultRef: Result<THookReturn>; | ||
}; | ||
|
||
function TestComponentHookRenderer<THookFn extends (...args: never) => unknown>({ | ||
executor, | ||
resultRef, | ||
}: TestComponentProps<ReturnType<THookFn>>) { | ||
resultRef.current = executor(); | ||
return null; | ||
} | ||
|
||
function TestComponent<THookFn extends (...args: never) => unknown>({ | ||
executor, | ||
resultRef, | ||
}: TestComponentProps<ReturnType<THookFn>>) { | ||
return ( | ||
<ErrorBoundary | ||
fallbackRender={({ error, resetErrorBoundary }) => { | ||
resultRef.__type = 'error'; | ||
resultRef.current = error; | ||
(resultRef as Extract<typeof resultRef, { __type: 'error' }>).reset = resetErrorBoundary; | ||
return null; | ||
}} | ||
onReset={() => { | ||
resultRef.__type = 'result'; | ||
delete resultRef.current; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delete (resultRef as any).reset; | ||
}} | ||
> | ||
<TestComponentHookRenderer executor={executor} resultRef={resultRef} /> | ||
</ErrorBoundary> | ||
); | ||
} | ||
|
||
export function renderHook<THookReturn>(executor: () => THookReturn): { | ||
rerenderHook(nextExecutor: () => THookReturn): void; | ||
result: Readonly<Result<THookReturn>>; | ||
} { | ||
const result = { __type: 'result' } as Result<THookReturn>; | ||
let testRenderer: ReactTestRenderer; | ||
act(() => { | ||
testRenderer = create(<TestComponent executor={executor} resultRef={result} />); | ||
}); | ||
return { | ||
rerenderHook(nextExecutor) { | ||
act(() => { | ||
testRenderer.update(<TestComponent executor={nextExecutor} resultRef={result} />); | ||
}); | ||
}, | ||
result, | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.