-
Notifications
You must be signed in to change notification settings - Fork 231
/
createTestHarness.tsx
45 lines (39 loc) 路 1.09 KB
/
createTestHarness.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React, { Suspense } from 'react'
import { RendererProps, WrapperComponent } from '../types/react'
import { isPromise } from './promises'
function TestComponent<TProps, TResult>({
hookProps,
callback,
setError,
setValue
}: RendererProps<TProps, TResult> & { hookProps?: TProps }) {
try {
// coerce undefined into TProps, so it maintains the previous behaviour
setValue(callback(hookProps as TProps))
} catch (err: unknown) {
if (isPromise(err)) {
throw err
} else {
setError(err as Error)
}
}
return null
}
function createTestHarness<TProps, TResult>(
rendererProps: RendererProps<TProps, TResult>,
Wrapper?: WrapperComponent<TProps>,
suspense: boolean = true
) {
const testHarness = (props?: TProps) => {
let component = <TestComponent hookProps={props} {...rendererProps} />
if (Wrapper) {
component = <Wrapper {...(props as TProps)}>{component}</Wrapper>
}
if (suspense) {
component = <Suspense fallback={null}>{component}</Suspense>
}
return component
}
return testHarness
}
export { createTestHarness }