11import { ComponentType , mount , render , shallow } from "enzyme" ;
22import * as React from "react" ;
33
4+ /**
5+ * A class to provide a simple interface for setting up your React component unit tests
6+ */
47export default class Wrapper <
58 C extends React . ComponentType < any > ,
69 P extends React . ComponentProps < C > = React . ComponentProps < C >
710> {
8- // Properties that are implemented in classes that extend Wrapper
11+ // region Properties that are implemented in classes that extend Wrapper
12+
13+ /**
14+ * The component that will wrap the component you're testing.
15+ *
16+ * This is useful adding provider components to wrap your components with.
17+ */
918 protected WrappingComponent : ComponentType < any > | undefined = undefined ;
1019
11- // Properties that remain throughout instance lifecycle
20+ // endregion
21+
22+ // region Properties that remain throughout instance lifecycle
23+
24+ /**
25+ * The default children for all test scenarios for the current test suite.
26+ *
27+ * This is set by the `.withDefaultChildren()` method.
28+ */
1229 protected defaultChildren : React . ReactNode ;
30+
31+ /**
32+ * The default props for all test scenarios for the current test suite.
33+ *
34+ * This is set by the `.withDefaultProps()` method.
35+ */
1336 protected defaultProps : Partial < P > = { } ;
1437
15- // Scenario-specific properties that are cleared whenever shallow/mount/render are called
38+ // endregion
39+
40+ // region Scenario-specific properties that are cleared whenever shallow/mount/render are called
41+
42+ /**
43+ * The children specific to the test scenario.
44+ *
45+ * This is set by the `.withChildren()` method and cleared after `.mount()`, `.render()` or `.shallow()` are called.
46+ */
1647 protected scenarioChildren : React . ReactNode ;
48+
49+ /**
50+ * The props specific to the test scenario.
51+ *
52+ * This is set by the `.withProps()` method and cleared after `.mount()`, `.render()` or `.shallow()` are called.
53+ */
1754 protected scenarioProps : Partial < P > = { } ;
1855
19- // Properties to be accessed via getters after mounting
56+ // endregion
57+
58+ // region Properties to be accessed via getters after mounting
59+
60+ /**
61+ * The merged props to be used during the lifecycle of the test scenario.
62+ *
63+ * This is set by the `.mount()`, `.render()` and `.shallow()` methods so that you can access the
64+ * props in your test via the `.props` getter.
65+ */
2066 protected mergedProps : Partial < P > = { } ;
2167
68+ // endregion
69+
70+ /**
71+ * @param Component The React component to test
72+ */
2273 constructor ( protected readonly Component : C ) { }
2374
75+ /**
76+ * Returns the props for the current test scenario lifecycle
77+ */
2478 public get props ( ) {
2579 return this . mergedProps ;
2680 }
2781
82+ /**
83+ * Sets the default children to be used in all test scenarios for the current test suite.
84+ *
85+ * This method can be chained with other methods.
86+ *
87+ * @param children The children to set
88+ */
2889 public withDefaultChildren = ( children : React . ReactNode ) => {
2990 this . defaultChildren = children ;
3091
3192 return this ;
3293 } ;
3394
95+ /**
96+ * Sets the default props to be used in all test scenarios for the current test suite.
97+ *
98+ * This method can be chained with other methods.
99+ *
100+ * @param props The props to set
101+ */
34102 public withDefaultProps = ( props : Partial < P > ) => {
35103 this . defaultProps = props ;
36104
37105 return this ;
38106 } ;
39107
108+ /**
109+ * Sets the children to be used by the current test scenario.
110+ *
111+ * This method can be chained with other methods.
112+ *
113+ * @param children The children to set
114+ */
40115 public withChildren = ( children : React . ReactNode ) => {
41116 this . scenarioChildren = children ;
42117
43118 return this ;
44119 } ;
45120
121+ /**
122+ * Sets the props to be used by the current test scenario.
123+ *
124+ * This method can be chained with other methods.
125+ *
126+ * @param props The props to set
127+ */
46128 public withProps = ( props : Partial < P > ) => {
47129 this . scenarioProps = props ;
48130
49131 return this ;
50132 } ;
51133
134+ /**
135+ * Mounts the component using Enzyme's `mount()` function.
136+ *
137+ * Returns the `ReactWrapper` instance from `mount()`.
138+ */
52139 public mount = ( ) => {
53140 this . beforeMount ( ) ;
54141
@@ -62,6 +149,11 @@ export default class Wrapper<
62149 return wrapper ;
63150 } ;
64151
152+ /**
153+ * Mounts the component using Enzyme's `render()` function.
154+ *
155+ * Returns the `Cheerio` wrapper instance from `render()`.
156+ */
65157 public render = ( ) => {
66158 this . beforeMount ( ) ;
67159
@@ -81,6 +173,11 @@ export default class Wrapper<
81173 return wrapper ;
82174 } ;
83175
176+ /**
177+ * Mounts the component using Enzyme's `shallow()` function.
178+ *
179+ * Returns the `ShallowWrapper` instance from `shallow()`.
180+ */
84181 public shallow = ( ) => {
85182 this . beforeMount ( ) ;
86183
@@ -100,10 +197,18 @@ export default class Wrapper<
100197 return wrapper ;
101198 } ;
102199
200+ /**
201+ * This is called before `.mount()`, `.render()` and `.shallow()` are called.
202+ *
203+ * You can use this to perform additional functionality when extending this class.
204+ */
103205 protected beforeMount = ( ) => {
104206 // Implement this method when extending to define properties to pass to `WrappingComponent`
105207 } ;
106208
209+ /**
210+ * Defines the merged props for the current test scenario lifecycle
211+ */
107212 protected defineProps = ( ) => {
108213 this . mergedProps = {
109214 ...this . defaultProps ,
@@ -114,6 +219,9 @@ export default class Wrapper<
114219 return this . mergedProps ;
115220 } ;
116221
222+ /**
223+ * Resets the scenario-specific children and props for the instance
224+ */
117225 protected reset ( ) {
118226 this . scenarioChildren = undefined ;
119227 this . scenarioProps = { } ;
0 commit comments