1
1
import { RenderOptions , RenderResult } from "@testing-library/react" ;
2
- import { ReactWrapper } from "enzyme" ;
3
2
import { ReactElement } from "react" ;
4
3
5
4
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -9,34 +8,27 @@ interface PureFunctionComponent<P = {}> {
9
8
export type SetupComponentType = React . ComponentType < any > | PureFunctionComponent ;
10
9
11
10
// Given a C component type, extracts the props of it:
12
- export type FullProps < C extends SetupComponentType > = React . ComponentProps < C >
11
+ export type FullProps < C extends SetupComponentType > = React . ComponentProps < C > ;
13
12
14
- export type RenderEnzyme <
15
- Component extends SetupComponentType ,
16
- Props extends Partial < FullProps < Component > >
17
- > = (
18
- // By using the spread operator in this type, we leverage the optionality of the entire argument itself.
19
- // eg: If the caller needs no more required props, we don't require they provide test props. But if
20
- // they DO still need props, we require a parameter in the function signature.
21
- // So the syntax `renderWrapper()` is valid if there are no required props,
22
- // otherwise we force `renderWrapper({ ...missingReqs })`
23
- ...testProps : ConditionallyRequiredTestProps < Component , Props >
24
- ) => RenderEnzymeReturn < Component > ;
25
-
26
- // Just like RenderEnzyme, but RenderRtl also allows callers to do `renderRtl.options({...})(props)`
13
+ // RenderRtl allows callers to do `renderRtl.options({...})(props)`
27
14
// In order to support both signature types, we extend the function's base type and add the options
28
15
// attribute for those "in the know" ;)
29
16
export type RenderRtl <
30
17
Component extends SetupComponentType ,
31
- Props extends Partial < FullProps < Component > >
18
+ Props extends Partial < FullProps < Component > > ,
32
19
> = {
20
+ // By using the spread operator in this type, we leverage the optionality of the entire argument itself.
21
+ // eg: If the caller needs no more required props, we don't require they provide test props. But if
22
+ // they DO still need props, we require a parameter in the function signature.
23
+ // So the syntax `renderView()` is valid if there are no required props,
24
+ // otherwise we force `renderView({ ...missingReqs })`
33
25
( ...testProps : ConditionallyRequiredTestProps < Component , Props > ) : RenderRtlReturn < Component > ;
34
26
options : ( options : RenderOptions ) => RenderRtl < Component , Props > ;
35
27
} ;
36
28
37
29
type ConditionallyRequiredTestProps <
38
30
Component extends SetupComponentType ,
39
- Props extends Partial < FullProps < Component > >
31
+ Props extends Partial < FullProps < Component > > ,
40
32
> =
41
33
// This is where the real magic happens. At type-interpretation time, the compiler can infer from the
42
34
// component's prop type and from the passed props into the `setup*` function if there are any missing
@@ -63,10 +55,6 @@ type RequiredKeys<T> = {
63
55
[ K in keyof T ] -?: Record < string , unknown > extends { [ P in K ] : T [ K ] } ? never : K ;
64
56
} [ keyof T ] ;
65
57
66
- interface RenderEnzymeReturn < Component extends SetupComponentType >
67
- extends BaseRenderReturn < Component > {
68
- wrapper : ReactWrapper < FullProps < Component > , React . ComponentState > ;
69
- }
70
58
interface RenderRtlReturn < Component extends SetupComponentType >
71
59
extends BaseRenderReturn < Component > {
72
60
view : RenderResult ;
@@ -85,10 +73,10 @@ interface BaseRenderReturn<Component extends SetupComponentType> {
85
73
*/
86
74
export type RemainingPropsAndTestOverrides <
87
75
ComponentType extends SetupComponentType ,
88
- BaseProps extends Partial < FullProps < ComponentType > >
76
+ BaseProps extends Partial < FullProps < ComponentType > > ,
89
77
> = RemainingProps < ComponentType , BaseProps > & Partial < FullProps < ComponentType > > ;
90
78
91
79
type RemainingProps <
92
80
ComponentType extends SetupComponentType ,
93
- BaseProps extends Partial < FullProps < ComponentType > >
81
+ BaseProps extends Partial < FullProps < ComponentType > > ,
94
82
> = Omit < FullProps < ComponentType > , keyof BaseProps > ;
0 commit comments